Class: NitroKit::Avatar

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/avatar.rb

Constant Summary collapse

SIZES =
%i[xs sm md lg].freeze

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(src: nil, alt: "", fallback: nil, size: :md, loading: "lazy", decoding: "async", id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Avatar

Returns a new instance of Avatar.

Raises:

  • (ArgumentError)


7
8
9
10
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
47
48
49
50
# File 'app/components/nitro_kit/avatar.rb', line 7

def initialize(
  src: nil,
  alt: "",
  fallback: nil,
  size: :md,
  loading: "lazy",
  decoding: "async",
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  raise ArgumentError, "alt must be a String" unless alt.is_a?(String)
  unless fallback.nil? || fallback.is_a?(String)
    raise ArgumentError, "fallback must be a String or nil"
  end

  @src = src
  @alt = alt
  @fallback = fallback || initials_for(alt)
  @size = validate_choice!(:size, size, SIZES)
  root_aria = fallback_aria(aria)

  super(
    component: :avatar,
    attributes: {
      id:,
      role: !src? && !alt.empty? ? "img" : nil,
      data: { controller: src? ? "nk--avatar" : nil }.compact
    },
    html:,
    aria: root_aria,
    data:,
    size:,
    desperately_need_a_class:
  )

  @image_attributes = {
    alt:,
    loading:,
    decoding:
  }
end

Instance Attribute Details

#altObject (readonly)

Returns the value of attribute alt.



52
53
54
# File 'app/components/nitro_kit/avatar.rb', line 52

def alt
  @alt
end

#fallbackObject (readonly)

Returns the value of attribute fallback.



52
53
54
# File 'app/components/nitro_kit/avatar.rb', line 52

def fallback
  @fallback
end

#sizeObject (readonly)

Returns the value of attribute size.



52
53
54
# File 'app/components/nitro_kit/avatar.rb', line 52

def size
  @size
end

#srcObject (readonly)

Returns the value of attribute src.



52
53
54
# File 'app/components/nitro_kit/avatar.rb', line 52

def src
  @src
end

Instance Method Details

#view_templateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/components/nitro_kit/avatar.rb', line 54

def view_template
  span(**root_attributes) do
    span(
      **slot_attributes(
        :fallback,
        attributes: src? ? { data: { nk__avatar_target: "fallback" } } : {},
        aria: { hidden: (src? || !alt.empty?) ? true : nil }
      )
    ) { fallback }

    if src?
      img(
        **slot_attributes(
          :image,
          attributes: @image_attributes.merge(
            src:,
            data: {
              nk__avatar_target: "image",
              action: "error->nk--avatar#failed"
            }
          )
        )
      )
    end
  end
end