The personal finance app for everyone (by everyone)
Ruby
fix(ui): resolve DS Drift Patrol findings (#2745) (#2975)
William Wei Ming819f62019bac
diff --git a/app/components/DS/pill.rb b/app/components/DS/pill.rbindex a51084652f..7d09ed481e 100644--- a/app/components/DS/pill.rb+++ b/app/components/DS/pill.rb@@ -17,7 +17,7 @@ class DS::Pill < DesignSystemComponent}.freezeattr_reader :label, :tone, :style, :size, :show_dot, :dot_only, :title, :icon, :marker, :custom_color,- :truncate, :label_testid, :icon_size+ :truncate, :label_testid, :icon_size, :mono# Generic inline pill primitive. Two modes:#@@ -60,8 +60,10 @@ class DS::Pill < DesignSystemComponent# controller tests that need to target the text node.# - `icon_size:` passes through to the icon helper (default "xs"; the# category badge uses "sm" to keep its established glyph size).+ # - `mono: true` applies `font-mono` for technical labels (queue names,+ # IDs, latencies) where tabular alignment aids scanning.def initialize(label: nil, tone: :violet, style: :soft, size: :sm, show_dot: nil, dot_only: false, title: nil, icon: nil, marker: true, custom_color: nil,- truncate: false, label_testid: nil, icon_size: "xs")+ truncate: false, label_testid: nil, icon_size: "xs", mono: false)resolved_tone = SEMANTIC_TONE_ALIASES.fetch(tone.to_sym, tone.to_sym)@label = label || I18n.t("ds.pill.default_label", default: "Beta")@tone = TONES.include?(resolved_tone) ? resolved_tone : :violet@@ -78,6 +80,7 @@ class DS::Pill < DesignSystemComponent@truncate = truncate@label_testid = label_testid@icon_size = icon_size+ @mono = monoenddef palette@@ -168,6 +171,7 @@ class DS::Pill < DesignSystemComponenttruncate ? "max-w-full min-w-0" : "whitespace-nowrap shrink-0","border leading-none"]+ base << "font-mono" if monoif marker# Marker mode (Beta / Canary / NEW): rounded-md (slight chipdiff --git a/app/components/DS/tooltip.html.erb b/app/components/DS/tooltip.html.erbindex 281f2d8e61..c4a52cc73e 100644--- a/app/components/DS/tooltip.html.erb+++ b/app/components/DS/tooltip.html.erb@@ -1,4 +1,4 @@-<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">+<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 as == :button %><%# Wrap the trigger icon in a focusable `<button>` so AT users (andkeyboard-only users) can land on the tooltip anchor. The Lucide@@ -13,14 +13,14 @@</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>`+ 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 onthis descendant span would never fire when the ancestor- disclosure receives focus. %>+ 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 %>"><%= helpers.icon icon_name, size: size, color: color %>diff --git a/app/components/DS/tooltip.rb b/app/components/DS/tooltip.rbindex 757f77bcfa..d79a937d0c 100644--- a/app/components/DS/tooltip.rb+++ b/app/components/DS/tooltip.rb@@ -1,7 +1,7 @@class DS::Tooltip < ApplicationComponentAS_OPTIONS = %i[button span].freeze- attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color, :tooltip_id, :as+ attr_reader :placement, :offset, :cross_axis, :icon_name, :size, :color, :tooltip_id, :as, :html_class# NOTE: tooltip content must be non-interactive — no buttons, links,# or form controls inside. Tooltips are exposed via `aria-describedby`,@@ -14,11 +14,17 @@ class DS::Tooltip < ApplicationComponent# 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)+ # sits inside an already-focusable interactive ancestor (`<a>`,+ # `<summary>`, …) where nested interactive content is forbidden or+ # undesirable. The Stimulus controller also binds focus handlers on+ # the closest `a`/`summary` ancestor so keyboard focus on that+ # element reveals the tooltip (`focusin` only bubbles upward).+ #+ # `html_class:` is merged onto the outer controller wrapper. Pass+ # sizing/layout utilities (e.g. `size-full items-center justify-center`)+ # when `as: :span` should fill an icon-only link so hover covers the+ # whole hit target, not just the icon glyph.+ def initialize(text: nil, placement: "top", offset: 10, cross_axis: 0, icon: "info", size: "sm", color: "default", as: :button, html_class: nil)raise ArgumentError, "as: must be one of #{AS_OPTIONS.inspect}" unless AS_OPTIONS.include?(as)@text = text@@ -29,6 +35,7 @@ class DS::Tooltip < ApplicationComponent@size = size@color = color@as = as+ @html_class = html_class@tooltip_id = "tooltip-#{SecureRandom.hex(4)}"enddiff --git a/app/components/DS/tooltip_controller.js b/app/components/DS/tooltip_controller.jsindex 5f0a4b3960..a036e64ff1 100644--- a/app/components/DS/tooltip_controller.js+++ b/app/components/DS/tooltip_controller.js@@ -38,19 +38,18 @@ export default class extends Controller {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);+ // already-focusable ancestor (`<summary>`, icon-only `<a>`, …).+ // 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. Bind the same+ // handlers on the closest interactive ancestor (if any) so+ // focusing it reveals the tooltip and Esc still dismisses it.+ this.focusableAncestor = this.element.closest("summary, a");+ if (this.focusableAncestor) {+ this.focusableAncestor.addEventListener("focusin", this.show);+ this.focusableAncestor.addEventListener("focusout", this.hide);+ this.focusableAncestor.addEventListener("keydown", this.handleKeydown);}}@@ -61,11 +60,11 @@ export default class extends Controller {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;+ if (this.focusableAncestor) {+ this.focusableAncestor.removeEventListener("focusin", this.show);+ this.focusableAncestor.removeEventListener("focusout", this.hide);+ this.focusableAncestor.removeEventListener("keydown", this.handleKeydown);+ this.focusableAncestor = null;}}diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erbindex e6e279a6b8..06592517e8 100644--- a/app/views/layouts/application.html.erb+++ b/app/views/layouts/application.html.erb@@ -169,10 +169,21 @@ end %>response served on click would never clear the badge. %><%= link_to insights_path,class: "relative inline-flex items-center justify-center w-9 h-9 pointer-coarse:w-11 pointer-coarse:h-11 rounded-lg text-secondary hover:text-primary hover:bg-container-inset-hover transition-colors focus-ring",- title: t("layouts.application.insights"),aria: { label: t("layouts.application.insights") },data: { turbo_prefetch: false } do %>- <%= icon("lightbulb", size: "sm") %>+ <%# `as: :span` + `html_class:` so the tooltip trigger fills this+ already-focusable link (full hover target). DS::Tooltip+ binds keyboard focus on the closest <a>/<summary> ancestor.+ Unread count stays a hand-rolled inverse chip — DS::Pill's+ filled neutral tone is not theme-swapped and disappears on+ dark nav surfaces. %>+ <%= render DS::Tooltip.new(+ text: t("layouts.application.insights"),+ icon: "lightbulb",+ size: "sm",+ as: :span,+ html_class: "size-full items-center justify-center"+ ) %><% if unread_insights_count.positive? %><span class="absolute top-1 right-1 min-w-4 h-4 px-1 rounded-full bg-inverse text-inverse text-[10px] font-medium flex items-center justify-center pointer-events-none"><%= unread_insights_count > 9 ? "9+" : unread_insights_count %></span><% end %>diff --git a/app/views/settings/background_jobs/show.html.erb b/app/views/settings/background_jobs/show.html.erbindex c3f7e50f14..05f378f42f 100644--- a/app/views/settings/background_jobs/show.html.erb+++ b/app/views/settings/background_jobs/show.html.erb@@ -30,9 +30,13 @@<div class="flex flex-wrap gap-2"><% @console.stats.queues.each do |queue| %>- <span class="rounded-full bg-container-inset px-3 py-1 text-xs text-secondary font-mono">- <%= queue[:name] %>: <%= queue[:size] %> · <%= t(".stats.latency", value: queue[:latency]) %>- </span>+ <%= render DS::Pill.new(+ label: t(".stats.queue_label", name: queue[:name], size: queue[:size], latency: queue[:latency]),+ tone: :neutral,+ marker: false,+ show_dot: false,+ mono: true+ ) %><% end %></div></div>diff --git a/config/locales/views/settings/de.yml b/config/locales/views/settings/de.ymlindex 904931df06..838acd7a4c 100644--- a/config/locales/views/settings/de.yml+++ b/config/locales/views/settings/de.yml@@ -535,7 +535,7 @@ de:busy: Aktivdead: Gescheitertenqueued: Wartend- latency: "%{value}s Latenz"+ queue_label: "%{name}: %{size} · %{latency}s Latenz"retries: Wiederholungenscheduled: Geplantworkers: Workerdiff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex 8024543675..b9f13a1f98 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -23,7 +23,7 @@ en:retries: "Retries"scheduled: "Scheduled"dead: "Dead"- latency: "%{value}s latency"+ queue_label: "%{name}: %{size} · %{latency}s latency"table:type: "Operation"family: "Family"diff --git a/config/locales/views/settings/uk.yml b/config/locales/views/settings/uk.ymlindex 071a31836a..ef51f5f968 100644--- a/config/locales/views/settings/uk.yml+++ b/config/locales/views/settings/uk.yml@@ -22,7 +22,7 @@ uk:retries: "Повтори"scheduled: "Заплановано"dead: "Мертві"- latency: "%{value}с затримка"+ queue_label: "%{name}: %{size} · %{latency}с затримка"table:type: "Операція"family: "Сім'я"diff --git a/config/locales/views/settings/zh-TW.yml b/config/locales/views/settings/zh-TW.ymlindex 94111fe263..6b50cfe05e 100644--- a/config/locales/views/settings/zh-TW.yml+++ b/config/locales/views/settings/zh-TW.yml@@ -23,7 +23,7 @@ zh-TW:retries: 重試scheduled: 已排程dead: 失效- latency: 延遲 %{value} 秒+ queue_label: "%{name}: %{size} · 延遲 %{latency} 秒"table:type: 作業family: 家庭diff --git a/test/components/DS/pill_test.rb b/test/components/DS/pill_test.rbindex 5fbc9a5420..7de395fd9a 100644--- a/test/components/DS/pill_test.rb+++ b/test/components/DS/pill_test.rb@@ -123,4 +123,11 @@ class DS::PillTest < ViewComponent::TestCase# sm maps to w-4 h-4 in the icon helper's size table.assert_selector "svg.w-4.h-4"end++ test "mono: true applies font-mono for technical labels" do+ render_inline(DS::Pill.new(label: "default: 3 · 0.1s latency", tone: :neutral, marker: false, mono: true))++ pill = page.find("span", text: "default: 3 · 0.1s latency")+ assert_includes pill[:class], "font-mono"+ endend