File tree 4 files changed +47
-4
lines changed
4 files changed +47
-4
lines changed Original file line number Diff line number Diff line change 1
1
defmodule PetalComponents.Alert do
2
2
use Phoenix.Component
3
+ alias PetalComponents.Helpers
3
4
4
5
attr ( :color , :string ,
5
6
default: "info" ,
@@ -23,10 +24,18 @@ defmodule PetalComponents.Alert do
23
24
assigns =
24
25
assigns
25
26
|> assign ( :classes , alert_classes ( assigns ) )
27
+ |> assign ( :heading_id , Helpers . uniq_id ( assigns . heading || "alert-heading" ) )
28
+ |> assign ( :label_id , Helpers . uniq_id ( assigns . label || "alert-label" ) )
26
29
27
30
~H"""
28
31
<%= unless label_blank? ( @ label , @ inner_block ) do %>
29
- < div { @ rest } class = { @ classes } >
32
+ < div
33
+ { @ rest }
34
+ class = { @ classes }
35
+ role = "dialog "
36
+ aria-labelledby = { ( @ heading && @ heading_id ) || @ label_id }
37
+ aria-describedby = { @ label_id }
38
+ >
30
39
<%= if @ with_icon do %>
31
40
< div class = "pc-alert__icon-container " >
32
41
< . get_icon color = { @ color } />
@@ -37,12 +46,12 @@ defmodule PetalComponents.Alert do
37
46
< div class = "pc-alert__inner " >
38
47
< div >
39
48
<%= if @ heading do %>
40
- < div class = "pc-alert__heading " >
49
+ < h2 id = { @ heading_id } class = "pc-alert__heading " >
41
50
<%= @ heading %>
42
- </ div >
51
+ </ h2 >
43
52
<% end %>
44
53
45
- < div class = "pc-alert__label " >
54
+ < div id = { @ label_id } class = "pc-alert__label " >
46
55
<%= render_slot ( @ inner_block ) || @ label %>
47
56
</ div >
48
57
</ div >
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ defmodule PetalComponents.Avatar do
19
19
<%= if src_blank? ( @ src ) && ! @ name do %>
20
20
< div
21
21
{ @ rest }
22
+ role = "img "
23
+ aria-label = "user avatar "
22
24
class = { [
23
25
"pc-avatar--with-placeholder-icon" ,
24
26
"pc-avatar--#{ @ size } " ,
@@ -32,6 +34,8 @@ defmodule PetalComponents.Avatar do
32
34
< div
33
35
{ @ rest }
34
36
style = { maybe_generate_random_color ( @ random_color , @ name ) }
37
+ role = "img "
38
+ aria-label = "user avatar "
35
39
class = { [
36
40
"pc-avatar--with-placeholder-initials" ,
37
41
"pc-avatar--#{ @ size } " ,
Original file line number Diff line number Diff line change @@ -3,6 +3,34 @@ defmodule PetalComponents.Helpers do
3
3
For any helper functions used across multiple components. Ideally we keep this empty - components should be copy-pastable.
4
4
"""
5
5
6
+ @ doc """
7
+ Generates a unique HTML ID based on the given string.
8
+
9
+ ## Parameters
10
+
11
+ - string: The input string (e.g., heading or label)
12
+ - prefix: An optional prefix for the ID (default: "c")
13
+
14
+ ## Examples
15
+
16
+ iex> PetalComponents.Helpers.uniq_id("My Heading")
17
+ "c-my-heading-1234"
18
+
19
+ iex> PetalComponents.Helpers.uniq_id("My Label", "custom")
20
+ "custom-my-label-5678"
21
+ """
22
+ def uniq_id ( string , prefix \\ "c" ) do
23
+ slug =
24
+ string
25
+ |> String . downcase ( )
26
+ |> String . replace ( ~r/ [^\w -]+/ , "-" )
27
+ # Limit slug length
28
+ |> String . slice ( 0 , 20 )
29
+
30
+ unique = System . unique_integer ( [ :positive ] ) |> Integer . to_string ( 36 )
31
+ "#{ prefix } -#{ slug } -#{ unique } "
32
+ end
33
+
6
34
@ doc """
7
35
Builds a class string from a given input list by joining them together
8
36
Original file line number Diff line number Diff line change @@ -89,6 +89,8 @@ defmodule PetalComponentsWeb.A11yLive do
89
89
</ . accordion >
90
90
91
91
< . alert with_icon color = "info " label = "Info alert " />
92
+ < . alert with_icon color = "info " label = "Info alert with heading " heading = "Info " />
93
+ < . alert with_icon color = "danger " label = "Error alert " />
92
94
93
95
< . avatar src = { @ avatar_src } />
94
96
You can’t perform that action at this time.
0 commit comments