Class: Jekyll::BeforeAfterTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/before_after_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ BeforeAfterTag

Returns a new instance of BeforeAfterTag.



20
21
22
23
# File 'lib/jekyll/before_after_tag.rb', line 20

def initialize(tag_name, markup, tokens)
  super
  @attributes = Jekyll.parse_ba_attrs(markup)
end

Instance Method Details

#render(context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll/before_after_tag.rb', line 25

def render(context)
  config   = context.registers[:site]&.config&.dig("before_after") || {}
  before   = @attributes["before"]   || ""
  after    = @attributes["after"]    || ""
  b_label  = @attributes["before_label"] || config["before_label"] || "Before"
  a_label  = @attributes["after_label"]  || config["after_label"]  || "After"
  start    = (@attributes["start"]   || config["start"]   || 50).to_i
  orient   = @attributes["orientation"] || config["orientation"] || "horizontal"
  alt_b    = @attributes["alt_before"] || ""
  alt_a    = @attributes["alt_after"]  || ""
  srcset_b = @attributes["srcset_before"] || ""
  srcset_a = @attributes["srcset_after"]  || ""
  sizes    = @attributes["sizes"] || "(max-width: 768px) 100vw, 100vw"

  before_img = build_img(before, alt_b, srcset_b, sizes)
  after_img  = build_img(after,  alt_a, srcset_a, sizes)

  script_block = emit_script(context)

  <<~HTML
    <div class="before-after" data-orientation="#{orient}" data-start="#{start}">
      <div class="before-after__before">
        #{before_img}
        #{b_label.empty? ? "" : %(<span class="before-after__label before-after__label--before">#{b_label}</span>)}
      </div>
      <div class="before-after__after">
        #{after_img}
        #{a_label.empty? ? "" : %(<span class="before-after__label before-after__label--after">#{a_label}</span>)}
      </div>
      <div class="before-after__handle"
           role="slider"
           aria-label="Image comparison slider"
           aria-valuenow="#{start}"
           aria-valuemin="0"
           aria-valuemax="100"
           tabindex="0">
        <div class="before-after__handle-line"></div>
        <div class="before-after__handle-knob">
          <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
            <path d="M8 5l-5 7 5 7M16 5l5 7-5 7" stroke="currentColor" stroke-width="2"
                  stroke-linecap="round" stroke-linejoin="round" fill="none"/>
          </svg>
        </div>
      </div>
    </div>
    #{script_block}
  HTML
end