Skip to content

Commit 521324a

Browse files
committed
better alert & avatar a11y
1 parent 3e9c4ef commit 521324a

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

lib/petal_components/alert.ex

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule PetalComponents.Alert do
22
use Phoenix.Component
3+
alias PetalComponents.Helpers
34

45
attr(:color, :string,
56
default: "info",
@@ -23,10 +24,18 @@ defmodule PetalComponents.Alert do
2324
assigns =
2425
assigns
2526
|> 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"))
2629

2730
~H"""
2831
<%= 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+
>
3039
<%= if @with_icon do %>
3140
<div class="pc-alert__icon-container">
3241
<.get_icon color={@color} />
@@ -37,12 +46,12 @@ defmodule PetalComponents.Alert do
3746
<div class="pc-alert__inner">
3847
<div>
3948
<%= if @heading do %>
40-
<div class="pc-alert__heading">
49+
<h2 id={@heading_id} class="pc-alert__heading">
4150
<%= @heading %>
42-
</div>
51+
</h2>
4352
<% end %>
4453
45-
<div class="pc-alert__label">
54+
<div id={@label_id} class="pc-alert__label">
4655
<%= render_slot(@inner_block) || @label %>
4756
</div>
4857
</div>

lib/petal_components/avatar.ex

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ defmodule PetalComponents.Avatar do
1919
<%= if src_blank?(@src) && !@name do %>
2020
<div
2121
{@rest}
22+
role="img"
23+
aria-label="user avatar"
2224
class={[
2325
"pc-avatar--with-placeholder-icon",
2426
"pc-avatar--#{@size}",
@@ -32,6 +34,8 @@ defmodule PetalComponents.Avatar do
3234
<div
3335
{@rest}
3436
style={maybe_generate_random_color(@random_color, @name)}
37+
role="img"
38+
aria-label="user avatar"
3539
class={[
3640
"pc-avatar--with-placeholder-initials",
3741
"pc-avatar--#{@size}",

lib/petal_components/helpers.ex

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@ defmodule PetalComponents.Helpers do
33
For any helper functions used across multiple components. Ideally we keep this empty - components should be copy-pastable.
44
"""
55

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+
634
@doc """
735
Builds a class string from a given input list by joining them together
836

lib/petal_components_web/a11y_live.ex

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ defmodule PetalComponentsWeb.A11yLive do
8989
</.accordion>
9090
9191
<.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" />
9294
9395
<.avatar src={@avatar_src} />
9496

0 commit comments

Comments
 (0)