Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(design-system): DS::Dialog a11y — role, aria-modal, aria-labelledby, heading_level (#1846)

Guillem Arias Fauste7a0cafd6ba75
diff --git a/app/components/DS/dialog.html.erb b/app/components/DS/dialog.html.erb
index e6892b1ce0..97bac18d64 100644
--- a/app/components/DS/dialog.html.erb
+++ b/app/components/DS/dialog.html.erb
@@ -1,5 +1,23 @@
<%= wrapper_element do %>
- <%= tag.dialog class: "w-full h-full bg-transparent text-primary theme-dark:backdrop:bg-alpha-black-900 backdrop:bg-overlay pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)] #{(drawer? || responsive?) ? "lg:p-3" : "lg:p-1"}", **merged_opts do %>
+ <%# `role="dialog"` + `aria-modal="true"` is redundant with `<dialog>`
+ in modern browsers, but Safari and screen readers older than the
+ 2024 WAI-ARIA Mapping still benefit from the explicit role.
+ `aria-labelledby` is always emitted with the stable `title_id`. When
+ the header slot rendered an auto-title (the common case), AT
+ resolves the reference and announces the title. When no title is
+ rendered (`custom_header: true`, body-only dialogs), the dangling
+ reference is silently ignored per the WAI-ARIA spec, and callers
+ can supply `aria-label` / `aria-labelledby` via `**opts` (last-wins)
+ for an explicit accessible name. The conditional `if has_auto_title?`
+ gate was a no-op here: the `renders_one :header` slot lambda is
+ evaluated lazily at slot-render time (after the `<dialog>` opening
+ tag attributes are computed), so the flag was never `true` when
+ this attribute was read. %>
+ <%= tag.dialog class: "w-full h-full bg-transparent text-primary theme-dark:backdrop:bg-alpha-black-900 backdrop:bg-overlay pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)] #{(drawer? || responsive?) ? "lg:p-3" : "lg:p-1"}",
+ role: "dialog",
+ "aria-modal": "true",
+ "aria-labelledby": title_id,
+ **merged_opts do %>
<%= tag.div class: dialog_outer_classes do %>
<%= tag.div class: dialog_inner_classes, data: { DS__dialog_target: "content" } do %>
<div class="grow py-4 space-y-4 flex flex-col <%= "overflow-auto" if scrollable %>">
diff --git a/app/components/DS/dialog.rb b/app/components/DS/dialog.rb
index 1027f6db02..fe26113397 100644
--- a/app/components/DS/dialog.rb
+++ b/app/components/DS/dialog.rb
@@ -2,7 +2,11 @@ class DS::Dialog < DesignSystemComponent
renders_one :header, ->(title: nil, subtitle: nil, custom_header: false, **opts, &block) do
content_tag(:header, class: "px-4 flex flex-col gap-2", **opts) do
title_div = content_tag(:div, class: "flex items-center justify-between gap-2") do
- title = content_tag(:h2, title, class: class_names("font-medium text-primary", drawer? ? "text-lg" : "")) if title
+ # `id: title_id` lets the host `<dialog>` reference the title via
+ # `aria-labelledby` so AT users hear the title when focus lands
+ # in the dialog. `content_tag("h#{heading_level}", ...)` builds an
+ # h2/h3/etc based on the caller's `heading_level:`.
+ title = content_tag("h#{heading_level}", title, id: title_id, class: class_names("font-medium text-primary", drawer? ? "text-lg" : "")) if title
close_icon = close_button unless custom_header
safe_join([ title, close_icon ].compact)
end
@@ -33,7 +37,7 @@ class DS::Dialog < DesignSystemComponent
end
end
- attr_reader :variant, :auto_open, :reload_on_close, :width, :disable_frame, :content_class, :disable_click_outside, :opts, :responsive, :scrollable
+ attr_reader :variant, :auto_open, :reload_on_close, :width, :disable_frame, :content_class, :disable_click_outside, :opts, :responsive, :scrollable, :heading_level, :title_id
VARIANTS = %w[modal drawer].freeze
WIDTHS = {
@@ -42,8 +46,13 @@ class DS::Dialog < DesignSystemComponent
lg: "lg:max-w-[700px]",
full: "lg:max-w-full"
}.freeze
+ VALID_HEADING_LEVELS = (1..6).freeze
+
+ def initialize(variant: "modal", auto_open: true, reload_on_close: false, width: "md", frame: nil, disable_frame: false, content_class: nil, disable_click_outside: false, responsive: false, scrollable: true, heading_level: 2, **opts)
+ unless heading_level.is_a?(Integer) && VALID_HEADING_LEVELS.cover?(heading_level)
+ raise ArgumentError, "heading_level must be an Integer between 1 and 6, got: #{heading_level.inspect}"
+ end
- def initialize(variant: "modal", auto_open: true, reload_on_close: false, width: "md", frame: nil, disable_frame: false, content_class: nil, disable_click_outside: false, responsive: false, scrollable: true, **opts)
@variant = variant.to_sym
@auto_open = auto_open
@reload_on_close = reload_on_close
@@ -54,6 +63,8 @@ class DS::Dialog < DesignSystemComponent
@disable_click_outside = disable_click_outside
@responsive = responsive
@scrollable = scrollable
+ @heading_level = heading_level
+ @title_id = "dialog-title-#{SecureRandom.hex(4)}"
@opts = opts
end