Files
sure/app/components/DS/filled_icon.html.erb
Guillem Arias Fauste 51b0336262 fix(design-system): DS::FilledIcon decorative-vs-meaningful API (#1842)
* fix(design-system): DS::FilledIcon decorative-vs-meaningful API

Closes #1742. `DS::FilledIcon` is mostly used as a decorative
visual indicator next to a textual label (transaction merchant
avatar, recurring-transaction icon, payment-method tile, etc.).
The wrapper was rendering without any aria scaffolding, so screen
readers had to traverse the inner `<svg>` or single-letter `<span>`
with no context.

Two new kwargs:

- `description:` (nil) — when set, the wrapper emits
  `role="img" aria-label="<description>"`. Use this when the
  surrounding DOM does not carry the label (e.g. icon-only badges
  in a grid).

- `aria_hidden:` (auto) — defaults to `true` when `description:` is
  blank (= decorative), `false` when description is present. Pass
  explicitly to override for the rare case where you want the
  visual exposed but the name already lives in adjacent text.

API stays backwards-compatible: existing 33 callsites get
`aria-hidden="true"` by default, which is correct — the visible
text next to the icon already carries the name.

While here: doc the `:text` variant gotcha — only `text.first` is
rendered, so AT users hearing "A" can't infer "Apple". Callers
should pass the full `description:` when relying on this variant.

Out of scope (filed elsewhere if needed):
- Touch-target audit (decorative wrapper, WCAG 2.5.5 doesn't apply).
- `hex_color:` palette soft-validation (would require a token-name
  registry; deferred until #1736 / #1653 land).
- `color-mix(in oklab, ...)` browser-support note for the
  transparent variant — tier-2 concern.

* fix(review): gate role/aria-label when hidden, normalize blank description

CodeRabbit feedback on #1842:
- Avoid emitting role="img" and aria-label alongside aria-hidden="true"
  (dead markup; AT ignores semantics on hidden subtrees).
- Normalize blank description strings to nil via .presence so the
  default aria_hidden fallback treats "" the same as nil.

* fix(review): use description.presence so aria-label drops blank strings

Codex follow-up review 4319747515 caught that the prior fix still
emitted `aria-label=""` when description was a blank string.
`.presence` returns nil for blank — Rails `tag.div` drops the
attribute entirely when the value is nil.
2026-05-20 18:08:58 +02:00

12 lines
470 B
Plaintext

<%= tag.div style: transparent? ? container_styles : nil,
class: container_classes,
role: (!aria_hidden && description.present?) ? "img" : nil,
"aria-label": aria_hidden ? nil : description.presence,
"aria-hidden": aria_hidden ? "true" : nil do %>
<% if icon %>
<%= helpers.icon(icon, size: icon_size, color: "current") %>
<% elsif text %>
<%= tag.span text.first, class: text_classes %>
<% end %>
<% end %>