-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathavatar.ex
More file actions
136 lines (117 loc) · 3.42 KB
/
avatar.ex
File metadata and controls
136 lines (117 loc) · 3.42 KB
1
2
3
4
5
6
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
51
52
53
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
80
81
82
83
84
85
86
87
88
89
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
defmodule AtomicWeb.Components.Avatar do
@moduledoc false
use AtomicWeb, :component
attr :src, :string, default: nil, doc: "The URL of the image to display."
attr :name, :string, required: true, doc: "The name of the entity associated with the avatar."
attr :auto_generate_initials, :boolean,
default: true,
doc: "Whether to automatically generate the initials from the name."
attr :type, :atom,
values: [:user, :organization, :company],
default: :user,
doc: "The type of entity associated with the avatar."
attr :size, :atom,
values: [:xs, :sm, :md, :lg, :xl],
default: :md,
doc: "The size of the avatar."
attr :color, :atom,
default: :light_zinc,
values: [
:primary,
:secondary,
:info,
:success,
:warning,
:danger,
:zinc,
:light_zinc,
:pure_white,
:white,
:light,
:dark
],
doc: "Avatar color."
attr :class, :string, default: "", doc: "Additional classes to apply to the component."
def avatar(assigns) do
~H"""
<span class={generate_avatar_classes(assigns)}>
<%= if @src do %>
<img src={@src} class={"atomic-avatar--#{assigns.type} h-full w-full object-contain"} />
<% else %>
<%= if @auto_generate_initials do %>
<%= extract_initials(@name) %>
<% else %>
<%= @name %>
<% end %>
<% end %>
</span>
"""
end
attr :items, :list, required: true, doc: "The list of avatars to display."
attr :spacing, :integer,
default: -1,
values: [-3, -2, -1, 0, 1, 2, 3],
doc: "The spacing between avatars."
attr :type, :atom,
values: [:user, :organization, :company],
default: :user,
doc: "The type of entity associated with the avatars."
attr :size, :atom,
values: [:xs, :sm, :md, :lg, :xl],
default: :md,
doc: "The size of the avatars."
attr :color, :atom,
default: :light_zinc,
values: [
:primary,
:secondary,
:info,
:success,
:warning,
:danger,
:zinc,
:light_zinc,
:pure_white,
:white,
:light,
:dark
],
doc: "Avatar color."
attr :wrap, :boolean, default: false, doc: "Whether to wrap the avatars in a flex container."
attr :class, :string, default: "", doc: "Additional classes to apply to the component."
attr :avatar_class, :string,
default: "",
doc: "Additional classes to apply to the individual avatars."
def avatar_group(assigns) do
~H"""
<ul class={"#{generate_avatar_group_spacing_class(@spacing)} #{if @wrap do "flex-wrap" end} #{@class} flex flex-row"}>
<%= for item <- @items do %>
<li>
<.avatar name={item[:name]} src={item[:src]} type={@type} size={@size} color={@color} class={"#{@avatar_class} atomic-avatar-grouped"} auto_generate_initials={Map.get(item, :auto_generate_initials, true)} />
</li>
<% end %>
</ul>
"""
end
defp generate_avatar_classes(assigns) do
[
"atomic-avatar",
assigns.src && "atomic-avatar--src",
"atomic-avatar--#{assigns.color}",
"atomic-avatar--#{assigns.size}",
"atomic-avatar--#{assigns.type}",
assigns.class
]
end
defp generate_avatar_group_spacing_class(spacing) do
%{
-3 => "-space-x-3",
-2 => "-space-x-2",
-1 => "-space-x-1",
0 => "space-x-0",
1 => "space-x-1",
2 => "space-x-2",
3 => "space-x-3"
}[spacing]
end
end