Class: NitroKit::ProgressiveImage

Inherits:
Component
  • Object
show all
Includes:
Phlex::Rails::Helpers::Routes
Defined in:
app/components/nitro_kit/progressive_image.rb

Constant Summary collapse

SIZES =
%i[sm md lg].freeze
PIXELS =
{ sm: 320, md: 720, lg: 1_440 }.freeze
PLACEHOLDER_PIXELS =
48

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attachment:, alt: nil, size: :md, decorative: false, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ ProgressiveImage

Returns a new instance of ProgressiveImage.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/components/nitro_kit/progressive_image.rb', line 11

def initialize(
  attachment:,
  alt: nil,
  size: :md,
  decorative: false,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @attachment = validate_attachment!(attachment)
  @size = validate_choice!(:size, size, SIZES)
  @decorative = validate_boolean!(:decorative, decorative)
  @attached = attachment&.attached? || false
  @alt = validate_alt!(alt)

  validate_attached_contract! if @attached

  super(
    component: :progressive_image,
    attributes: {
      id:,
      aria: { busy: @attached ? true : nil },
      data: {
        state: @attached ? "loading" : "empty",
        controller: @attached ? "nk--progressive-image" : nil
      }.compact
    },
    html:,
    aria:,
    data:,
    size:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#altObject (readonly)

Returns the value of attribute alt.



48
49
50
# File 'app/components/nitro_kit/progressive_image.rb', line 48

def alt
  @alt
end

#attachmentObject (readonly)

Returns the value of attribute attachment.



48
49
50
# File 'app/components/nitro_kit/progressive_image.rb', line 48

def attachment
  @attachment
end

#decorativeObject (readonly)

Returns the value of attribute decorative.



48
49
50
# File 'app/components/nitro_kit/progressive_image.rb', line 48

def decorative
  @decorative
end

#sizeObject (readonly)

Returns the value of attribute size.



48
49
50
# File 'app/components/nitro_kit/progressive_image.rb', line 48

def size
  @size
end

Instance Method Details

#view_templateObject



50
51
52
53
54
55
56
57
58
59
# File 'app/components/nitro_kit/progressive_image.rb', line 50

def view_template
  div(**root_attributes) do
    if @attached
      render_placeholder
      render_image
    end

    render_fallback
  end
end