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
|
# File 'lib/klods/components/avatar.rb', line 10
def avatar(props = nil)
props = (props || {}).transform_keys(&:to_s)
src = props.delete("src")
name = props.delete("name")
size = (props.delete("size") || "medium").to_s
= props.delete("class")
has_initials = src.nil? && !name.nil?
cls = Core.class_names(
"klods-avatar",
(size != "medium") ? "klods-avatar--#{size}" : nil,
has_initials ? "klods-avatar--initials" : nil,
Core.resolve_class()
)
content = if src
Core.el("img", {"src" => src, "alt" => name.to_s, "class" => "klods-avatar__img"})
elsif name
initials = name.strip.split(/\s+/).first(2).map { |w| w[0]&.upcase || "" }.join
Core.el("span", {"aria-hidden" => "true"}, initials)
else
px = AVATAR_SIZE_PX[size] || 20
svg = %(<svg width="#{px}" height="#{px}" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">#{USER_SVG_INNER}</svg>)
Core.el("span", {"aria-hidden" => "true", "class" => "klods-icon"}, Core.raw(svg))
end
span_attrs = props.merge("class" => cls.empty? ? nil : cls).compact
if has_initials
span_attrs["role"] = "img"
span_attrs["aria-label"] = name
end
Core.el("span", span_attrs, content)
end
|