The personal finance app for everyone (by everyone)
Ruby
<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="<%= class_names("inline-flex", html_class) %>">
<% if trigger? %>
<span class="inline-flex min-w-0 items-center" aria-describedby="<%= tooltip_id %>">
<%= trigger %>
</span>
<% elsif 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-ring rounded"
aria-describedby="<%= tooltip_id %>"
aria-label="<%= t("ds.tooltip.trigger_label", default: "More info") %>">
<%= trigger_content || helpers.icon(icon_name, size: size, color: color) %>
</button>
<% else %>
<%# `as: :span` — used when the tooltip is rendered inside an
already-focusable interactive ancestor (`<a>`, `<summary>`, …),
where nested interactive content is forbidden or undesirable.
Keyboard reveal is wired in the controller, which also binds
`focusin/focusout/keydown` on the closest `a`/`summary`
ancestor — because `focusin` only bubbles UP, a listener on
this descendant span would never fire when the ancestor
receives focus. Pass `html_class:` (e.g. size-full) when the
wrapper should fill an icon-only link's hit target. %>
<span class="inline-flex items-center"
aria-describedby="<%= tooltip_id %>">
<%= trigger_content || helpers.icon(icon_name, size: size, color: color) %>
</span>
<% end %>
<%# `fixed` (with `strategy: "fixed"` in the controller) and `w-max` are both
load-bearing:
- `fixed` escapes ancestor `overflow: hidden`, which otherwise crops the
panel. The account page wraps the logo in such a row, and the default
`top` placement puts the panel outside its box. DS::Menu already uses
the fixed strategy for the same reason.
- `w-max` decouples the bubble from its trigger. An `absolute` panel's
shrink-to-fit width is bounded by its nearest *positioned* ancestor, so
on an icon-sized trigger the text collapsed to its longest word.
`max-w-[20rem]` on the content still wraps long copy. %>
<div role="tooltip"
id="<%= tooltip_id %>"
data-DS--tooltip-target="tooltip"
class="<%= class_names("hidden fixed z-50 w-max", panel_classes) %>">
<%# `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="<%= content_classes %>">
<%= tooltip_content %>
</div>
</div>
</span>