Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(design-system): DS::Tooltip a11y — focusable trigger, keyboard parity, Esc dismiss (#1845)

Guillem Arias Faustee30ccd94afe5
diff --git a/app/components/DS/tooltip.html.erb b/app/components/DS/tooltip.html.erb
index a10c1147f1..fe0c043661 100644
--- a/app/components/DS/tooltip.html.erb
+++ b/app/components/DS/tooltip.html.erb
@@ -1,8 +1,39 @@
<span data-controller="DS--tooltip" data-DS--tooltip-placement-value="<%= placement %>" data-DS--tooltip-offset-value="<%= offset %>" data-DS--tooltip-cross-axis-value="<%= cross_axis %>" class="inline-flex">
- <%= helpers.icon icon_name, size: size, color: color %>
+ <% if as == :button %>
+ <%# Wrap the trigger icon in a focusable `<button>` so AT users (and
+ keyboard-only users) can land on the tooltip anchor. The Lucide
+ icon itself carries `aria-hidden`, so the button's accessible
+ name comes from `aria-label`, and the tooltip text is exposed
+ via `aria-describedby`. %>
+ <button type="button"
+ class="inline-flex items-center cursor-default focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-alpha-black-300 theme-dark:focus-visible:ring-alpha-white-300 rounded"
+ aria-describedby="<%= tooltip_id %>"
+ aria-label="<%= t("ds.tooltip.trigger_label", default: "More info") %>">
+ <%= helpers.icon icon_name, size: size, color: color %>
+ </button>
+ <% else %>
+ <%# `as: :span` — used when the tooltip is rendered inside an
+ already-focusable interactive ancestor (e.g. `<summary>`),
+ where the HTML spec forbids nested interactive content.
+ Keyboard reveal is wired in the controller, which (in addition
+ to listening on the outer span for the standalone case) also
+ binds `focusin/focusout/keydown` on the closest `<summary>`
+ ancestor — because `focusin` only bubbles UP, a listener on
+ this descendant span would never fire when the ancestor
+ disclosure receives focus. %>
+ <span class="inline-flex items-center"
+ aria-describedby="<%= tooltip_id %>">
+ <%= helpers.icon icon_name, size: size, color: color %>
+ </span>
+ <% end %>
- <div role="tooltip" data-DS--tooltip-target="tooltip" class="hidden absolute z-50 bg-inverse text-sm px-1.5 py-1 rounded-md">
- <div class="text-inverse font-normal max-w-[200px]">
+ <div role="tooltip"
+ id="<%= tooltip_id %>"
+ data-DS--tooltip-target="tooltip"
+ class="hidden absolute z-50 bg-inverse text-sm px-1.5 py-1 rounded-md">
+ <%# `max-w-[20rem]` scales with the root font-size so AT users with
+ a larger base font don't see the tooltip clipped. %>
+ <div class="text-inverse font-normal max-w-[20rem]">
<%= tooltip_content %>
</div>
</div>
diff --git a/app/components/DS/tooltip.rb b/app/components/DS/tooltip.rb
index 7189c4f35c..757f77bcfa 100644
--- a/app/components/DS/tooltip.rb
+++ b/app/components/DS/tooltip.rb
@@ -1,7 +1,26 @@
class DS::Tooltip < ApplicationComponent
- attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color
+ AS_OPTIONS = %i[button span].freeze
+
+ attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color, :tooltip_id, :as
+
+ # NOTE: tooltip content must be non-interactive — no buttons, links,
+ # or form controls inside. Tooltips are exposed via `aria-describedby`,
+ # which announces the content as a description but does not expose
+ # interactive descendants to AT. Use a popover/menu primitive when
+ # the surface needs to host actions.
+ #
+ # `as:` controls the trigger element.
+ # :button (default) — renders `<button type="button">`, focusable on
+ # its own. Use for tooltips placed in standalone, non-interactive
+ # surrounding markup.
+ # :span — renders `<span>` with no `tabindex`. Use when the tooltip
+ # sits inside an already-focusable interactive ancestor (most
+ # commonly `<summary>`, where the HTML spec forbids nested
+ # interactive content). The ancestor's focus still triggers the
+ # tooltip because `focusin` bubbles up to the Stimulus controller.
+ def initialize(text: nil, placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default", as: :button)
+ raise ArgumentError, "as: must be one of #{AS_OPTIONS.inspect}" unless AS_OPTIONS.include?(as)
- def initialize(text: nil, placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default")
@text = text
@placement = placement
@offset = offset
@@ -9,6 +28,8 @@ class DS::Tooltip < ApplicationComponent
@icon_name = icon
@size = size
@color = color
+ @as = as
+ @tooltip_id = "tooltip-#{SecureRandom.hex(4)}"
end
def tooltip_content
diff --git a/app/components/DS/tooltip_controller.js b/app/components/DS/tooltip_controller.js
index 8f198baedc..5f0a4b3960 100644
--- a/app/components/DS/tooltip_controller.js
+++ b/app/components/DS/tooltip_controller.js
@@ -29,11 +29,44 @@ export default class extends Controller {
addEventListeners() {
this.element.addEventListener("mouseenter", this.show);
this.element.addEventListener("mouseleave", this.hide);
+ // Keyboard parity: keyboard users hit the trigger via Tab + focus,
+ // not hover. Without these the tooltip never appears for them.
+ this.element.addEventListener("focusin", this.show);
+ this.element.addEventListener("focusout", this.hide);
+ // Esc-to-dismiss matches the WAI-ARIA Authoring Practices for the
+ // tooltip pattern.
+ this.element.addEventListener("keydown", this.handleKeydown);
+
+ // `as: :span` renders a non-focusable trigger inside an
+ // already-focusable ancestor (typically `<summary>`). When the
+ // ancestor receives keyboard focus the `focusin` event fires on
+ // *it* and bubbles UP to the document — it never reaches a
+ // descendant span. Without a listener on the ancestor itself,
+ // the tooltip stays hidden for keyboard users on in-summary rows.
+ // Bind the same handlers on the closest `<summary>` (if any) so
+ // focusing the disclosure reveals the tooltip and Esc still
+ // dismisses it.
+ this.summaryAncestor = this.element.closest("summary");
+ if (this.summaryAncestor) {
+ this.summaryAncestor.addEventListener("focusin", this.show);
+ this.summaryAncestor.addEventListener("focusout", this.hide);
+ this.summaryAncestor.addEventListener("keydown", this.handleKeydown);
+ }
}
removeEventListeners() {
this.element.removeEventListener("mouseenter", this.show);
this.element.removeEventListener("mouseleave", this.hide);
+ this.element.removeEventListener("focusin", this.show);
+ this.element.removeEventListener("focusout", this.hide);
+ this.element.removeEventListener("keydown", this.handleKeydown);
+
+ if (this.summaryAncestor) {
+ this.summaryAncestor.removeEventListener("focusin", this.show);
+ this.summaryAncestor.removeEventListener("focusout", this.hide);
+ this.summaryAncestor.removeEventListener("keydown", this.handleKeydown);
+ this.summaryAncestor = null;
+ }
}
show = () => {
@@ -47,6 +80,12 @@ export default class extends Controller {
this.stopAutoUpdate();
};
+ handleKeydown = (event) => {
+ if (event.key === "Escape" && !this.tooltipTarget.classList.contains("hidden")) {
+ this.hide();
+ }
+ };
+
startAutoUpdate() {
if (!this._cleanup) {
const reference = this.element.querySelector("[data-icon]");
diff --git a/app/components/UI/account/activity_date.html.erb b/app/components/UI/account/activity_date.html.erb
index 6fd158d532..e788bfa4bc 100644
--- a/app/components/UI/account/activity_date.html.erb
+++ b/app/components/UI/account/activity_date.html.erb
@@ -21,7 +21,7 @@
<div class="flex items-center gap-4">
<div class="flex items-center gap-2">
<span class="font-medium privacy-sensitive"><%= end_balance_money.format %></span>
- <%= render DS::Tooltip.new(text: t(".balance_tooltip"), placement: "left", size: "sm") %>
+ <%= render DS::Tooltip.new(text: t(".balance_tooltip"), placement: "left", size: "sm", as: :span) %>
</div>
<%= helpers.icon "chevron-down", class: "group-open:rotate-180" %>
</div>
diff --git a/app/views/brex_items/_brex_item.html.erb b/app/views/brex_items/_brex_item.html.erb
index 4b97ceb84a..150d267f78 100644
--- a/app/views/brex_items/_brex_item.html.erb
+++ b/app/views/brex_items/_brex_item.html.erb
@@ -46,7 +46,7 @@
</div>
<% elsif brex_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: brex_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: brex_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/ibkr_items/_ibkr_item.html.erb b/app/views/ibkr_items/_ibkr_item.html.erb
index 511550b5ec..905eda0dd4 100644
--- a/app/views/ibkr_items/_ibkr_item.html.erb
+++ b/app/views/ibkr_items/_ibkr_item.html.erb
@@ -32,7 +32,7 @@
</div>
<% elsif ibkr_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: ibkr_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: ibkr_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/indexa_capital_items/_indexa_capital_item.html.erb b/app/views/indexa_capital_items/_indexa_capital_item.html.erb
index e6f608e33d..7f90af7bd0 100644
--- a/app/views/indexa_capital_items/_indexa_capital_item.html.erb
+++ b/app/views/indexa_capital_items/_indexa_capital_item.html.erb
@@ -34,7 +34,7 @@
</div>
<% elsif indexa_capital_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: indexa_capital_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: indexa_capital_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/lunchflow_items/_lunchflow_item.html.erb b/app/views/lunchflow_items/_lunchflow_item.html.erb
index a1aec313ba..0b0fecc76b 100644
--- a/app/views/lunchflow_items/_lunchflow_item.html.erb
+++ b/app/views/lunchflow_items/_lunchflow_item.html.erb
@@ -35,7 +35,7 @@
</div>
<% elsif lunchflow_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: lunchflow_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: lunchflow_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/mercury_items/_mercury_item.html.erb b/app/views/mercury_items/_mercury_item.html.erb
index 0159551e8b..6485da5488 100644
--- a/app/views/mercury_items/_mercury_item.html.erb
+++ b/app/views/mercury_items/_mercury_item.html.erb
@@ -35,7 +35,7 @@
</div>
<% elsif mercury_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: mercury_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: mercury_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/simplefin_items/_simplefin_item.html.erb b/app/views/simplefin_items/_simplefin_item.html.erb
index c6e90e810d..d09d9b2026 100644
--- a/app/views/simplefin_items/_simplefin_item.html.erb
+++ b/app/views/simplefin_items/_simplefin_item.html.erb
@@ -52,7 +52,7 @@
<% if has_warnings %>
<div class="flex items-center gap-2 mt-1">
<% if stats["accounts_skipped"].to_i > 0 %>
- <%= render DS::Tooltip.new(text: t(".accounts_skipped_tooltip"), icon: "alert-triangle", size: "sm", color: "warning") %>
+ <%= render DS::Tooltip.new(text: t(".accounts_skipped_tooltip"), icon: "alert-triangle", size: "sm", color: "warning", as: :span) %>
<span class="text-xs text-warning"><%= t(".accounts_skipped_label", count: stats["accounts_skipped"].to_i) %></span>
<% end %>
@@ -63,14 +63,15 @@
text: (ago ? t(".rate_limited_ago", time: ago) : t(".rate_limited_recently")),
icon: "clock",
size: "sm",
- color: "warning"
+ color: "warning",
+ as: :span
) %>
<% end %>
<% if stats["total_errors"].to_i > 0 || (stats["errors"].is_a?(Array) && stats["errors"].any?) %>
<% tooltip_text = simplefin_error_tooltip(stats) %>
<% if tooltip_text.present? %>
- <%= render DS::Tooltip.new(text: tooltip_text, icon: "alert-octagon", size: "sm", color: "warning") %>
+ <%= render DS::Tooltip.new(text: tooltip_text, icon: "alert-octagon", size: "sm", color: "warning", as: :span) %>
<% end %>
<% end %>
</div>
@@ -119,7 +120,7 @@
</div>
<% elsif simplefin_item.sync_error.present? && !duplicate_only_errors %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: simplefin_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: simplefin_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% elsif duplicate_only_errors %>
diff --git a/app/views/snaptrade_items/_snaptrade_item.html.erb b/app/views/snaptrade_items/_snaptrade_item.html.erb
index f36712ec24..636d09dbea 100644
--- a/app/views/snaptrade_items/_snaptrade_item.html.erb
+++ b/app/views/snaptrade_items/_snaptrade_item.html.erb
@@ -42,7 +42,7 @@
</div>
<% elsif snaptrade_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: snaptrade_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: snaptrade_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/app/views/sophtron_items/_sophtron_item.html.erb b/app/views/sophtron_items/_sophtron_item.html.erb
index a8c71d9003..d91450a863 100644
--- a/app/views/sophtron_items/_sophtron_item.html.erb
+++ b/app/views/sophtron_items/_sophtron_item.html.erb
@@ -41,7 +41,7 @@
</div>
<% elsif sophtron_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
- <%= render DS::Tooltip.new(text: sophtron_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %>
+ <%= render DS::Tooltip.new(text: sophtron_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
diff --git a/config/locales/views/components/en.yml b/config/locales/views/components/en.yml
index 8c49e04615..11d896222c 100644
--- a/config/locales/views/components/en.yml
+++ b/config/locales/views/components/en.yml
@@ -89,6 +89,8 @@ en:
default_label: Preview
dialog:
close: Close
+ tooltip:
+ trigger_label: More info
link:
opens_in_new_tab: (opens in new tab)
provider_sync_summary:
diff --git a/test/components/previews/tooltip_component_preview.rb b/test/components/previews/tooltip_component_preview.rb
index 68bd6c320b..a6345ef0be 100644
--- a/test/components/previews/tooltip_component_preview.rb
+++ b/test/components/previews/tooltip_component_preview.rb
@@ -6,7 +6,8 @@ class TooltipComponentPreview < ViewComponent::Preview
# @param icon text
# @param size select [xs, sm, md, lg, xl, 2xl]
# @param color select [default, white, success, warning, destructive, current]
- def default(text: "This is helpful information", placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default")
+ # @param as select [button, span]
+ def default(text: "This is helpful information", placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default", as: "button")
render DS::Tooltip.new(
text: text,
placement: placement,
@@ -14,7 +15,8 @@ class TooltipComponentPreview < ViewComponent::Preview
cross_axis: cross_axis,
icon: icon,
size: size,
- color: color
+ color: color,
+ as: as.to_sym
)
end