The personal finance app for everyone (by everyone)
Ruby
fix(design-system): DS::Link a11y — distinguishable default, icon-only label, external-link hardening (#1844)
Guillem Arias Faustef2782901d3fa
diff --git a/app/components/DS/link.html.erb b/app/components/DS/link.html.erbindex dae100eaac..f9c4cff5d7 100644--- a/app/components/DS/link.html.erb+++ b/app/components/DS/link.html.erb@@ -10,4 +10,8 @@<% if icon && icon_position == "right" %><%= helpers.icon(icon, size: size, color: icon_color) %><% end %>++ <% if opens_in_new_tab? %>+ <span class="sr-only"><%= t("ds.link.opens_in_new_tab", default: "(opens in new tab)") %></span>+ <% end %><% end %>diff --git a/app/components/DS/link.rb b/app/components/DS/link.rbindex 41ea3f3f28..9ca73aacb1 100644--- a/app/components/DS/link.rb+++ b/app/components/DS/link.rb@@ -5,7 +5,13 @@ class DS::Link < DS::ButtonishVARIANTS = VARIANTS.reverse_merge(default: {- container_classes: "",+ # Underline + `text-link` so the link is distinguishable by more+ # than color alone (WCAG 1.4.1). Focus ring uses the established+ # alpha-ring DS pattern (also used by DS::Toggle, DS::Tooltip,+ # provider_card, form-field) so theming stays centralized.+ container_classes: "text-link underline underline-offset-2 hover:no-underline " \+ "focus-visible:ring-2 focus-visible:ring-alpha-black-300 " \+ "theme-dark:focus-visible:ring-alpha-white-300",icon_classes: "text-secondary"}).freeze@@ -18,12 +24,48 @@ class DS::Link < DS::Buttonishdata = data.merge(turbo_frame: frame)end+ # External link hardening: `target="_blank"` without `rel="noopener"`+ # exposes window.opener to the new tab (reverse-tabnabbing). Always+ # set `noopener noreferrer` when we send the user off-tab. Authors+ # can override by passing `rel:` explicitly.+ if merged_opts[:target].to_s == "_blank"+ merged_opts[:rel] ||= "noopener noreferrer"+ end++ # Icon-only links have no visible text node, so screen readers fall+ # back to announcing the href. Derive a humanized fallback from the+ # icon key so AT users hear *something* meaningful; explicit+ # `aria: { label: }` on the caller still wins. Mirrors DS::Button.+ #+ # When the link also opens in a new tab, fold the cue into the+ # generated `aria-label` itself — `aria-label` overrides the+ # descendant accessible name, so the sr-only "(opens in new tab)"+ # span in the template would otherwise be masked.+ if icon_only? && icon.present?+ aria = (merged_opts[:aria] || {}).symbolize_keys+ if aria[:label].blank? && merged_opts[:"aria-label"].blank?+ label = icon.to_s.tr("-_", " ").humanize+ if merged_opts[:target].to_s == "_blank"+ label = "#{label} #{I18n.t("ds.link.opens_in_new_tab", default: "(opens in new tab)")}"+ end+ aria[:label] = label+ merged_opts[:aria] = aria+ end+ end+merged_opts.merge(class: class_names(container_classes, extra_classes),data: data)end+ # Render an sr-only suffix when the link opens in a new tab so AT+ # users hear "(opens in new tab)" — visual is a separate concern+ # (callers can render a `external-link` icon if they want a glyph).+ def opens_in_new_tab?+ opts[:target].to_s == "_blank"+ end+privatedef container_size_classessuper unless variant == :defaultdiff --git a/config/locales/views/components/en.yml b/config/locales/views/components/en.ymlindex a7c14bd2ba..8c49e04615 100644--- a/config/locales/views/components/en.yml+++ b/config/locales/views/components/en.yml@@ -89,6 +89,8 @@ en:default_label: Previewdialog:close: Close+ link:+ opens_in_new_tab: (opens in new tab)provider_sync_summary:title: Sync summarylast_sync: "Last sync: %{time_ago} ago"