Class: NitroKit::AvatarStack

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

Constant Summary collapse

SIZES =
Avatar::SIZES

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(label:, size: :md, max: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ AvatarStack

Returns a new instance of AvatarStack.

Raises:

  • (ArgumentError)


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
# File 'app/components/nitro_kit/avatar_stack.rb', line 10

def initialize(
  label:,
  size: :md,
  max: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  unless label.is_a?(String) && label.present?
    raise ArgumentError, "AvatarStack label: must be a non-blank String"
  end
  raise ArgumentError, "aria must be a Hash" unless aria.is_a?(Hash)
  if aria.keys.any? { |key| key.to_s.downcase.tr("_", "-") == "label" }
    raise ArgumentError, "AvatarStack group label is owned by label:"
  end
  unless max.nil? || (max.is_a?(Integer) && max.positive?)
    raise ArgumentError, "max must be a positive Integer or nil"
  end

  @size = validate_choice!(:size, size, SIZES)
  @max = max
  @avatars = []
  @overflow = nil

  super(
    component: :avatar_stack,
    attributes: { id:, role: "group" },
    html:,
    aria: aria.merge(label:),
    data:,
    size:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



47
48
49
# File 'app/components/nitro_kit/avatar_stack.rb', line 47

def max
  @max
end

#sizeObject (readonly)

Returns the value of attribute size.



47
48
49
# File 'app/components/nitro_kit/avatar_stack.rb', line 47

def size
  @size
end

Instance Method Details

#avatar(src: nil, alt: "", fallback: nil, loading: "lazy", decoding: "async", id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/components/nitro_kit/avatar_stack.rb', line 60

def avatar(
  src: nil,
  alt: "",
  fallback: nil,
  loading: "lazy",
  decoding: "async",
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  ensure_collecting!

  @avatars << Avatar.new(
    src:,
    alt:,
    fallback:,
    size:,
    loading:,
    decoding:,
    id:,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
  nil
end

#overflow(count, label: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/components/nitro_kit/avatar_stack.rb', line 90

def overflow(
  count,
  label: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  ensure_collecting!
  raise ArgumentError, "AvatarStack accepts at most one overflow" if @overflow
  if max
    raise ArgumentError, "AvatarStack max: already owns the overflow indicator"
  end
  unless count.is_a?(Integer) && count.positive?
    raise ArgumentError, "count must be a positive Integer"
  end
  raise ArgumentError, "label must be a String or nil" unless label.nil? || label.is_a?(String)
  raise ArgumentError, "aria must be a Hash" unless aria.is_a?(Hash)
  if aria.keys.any? { |key| key.to_s.downcase.tr("_", "-") == "label" }
    raise ArgumentError, "AvatarStack overflow label is owned by label:"
  end

  @overflow = Overflow.new(
    count:,
    label:,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class
  )
  nil
end

#view_template(&block) ⇒ Object



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

def view_template(&block)
  collect_declarations(&block)

  visible, hidden = partition_avatars

  span(**root_attributes) do
    visible.each { |avatar| render_in_slot(avatar, :avatar) }
    render_overflow(overflow_declaration(hidden))
  end
end