Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(ds): elevate dropdown overlays and stabilize selection check gutter (#2161)

Guillem Arias Fauste1742f4ef1e3e
diff --git a/app/components/DS/menu.html.erb b/app/components/DS/menu.html.erb
index c77895cbdd..07828a01c4 100644
--- a/app/components/DS/menu.html.erb
+++ b/app/components/DS/menu.html.erb
@@ -6,7 +6,7 @@
<% end %>
<div id="<%= menu_id %>" data-DS--menu-target="content" class="px-2 lg:px-0 max-w-full hidden z-50" role="menu">
- <%= tag.div class: "mx-auto min-w-[200px] shadow-border-xs bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
+ <%= tag.div class: "mx-auto min-w-[200px] shadow-border-lg bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
<%= tag.div class: class_names("py-1" => !no_padding) do %>
<% items.each do |item| %>
<%= item %>
diff --git a/app/components/DS/menu_controller.js b/app/components/DS/menu_controller.js
index 0c766909f7..fcf50e5939 100644
--- a/app/components/DS/menu_controller.js
+++ b/app/components/DS/menu_controller.js
@@ -119,7 +119,14 @@ export default class extends Controller {
}
#menuItems() {
- return Array.from(this.contentTarget.querySelectorAll('[role="menuitem"]'));
+ // Include selectable roles (menuitemradio/menuitemcheckbox) so roving focus
+ // and keyboard handling work for single/multi-select menus, not just plain
+ // action items.
+ return Array.from(
+ this.contentTarget.querySelectorAll(
+ '[role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"]',
+ ),
+ );
}
#focusFirstMenuItem() {
diff --git a/app/components/DS/menu_item.html.erb b/app/components/DS/menu_item.html.erb
index 1e401ddf2f..96a432c561 100644
--- a/app/components/DS/menu_item.html.erb
+++ b/app/components/DS/menu_item.html.erb
@@ -5,6 +5,11 @@
<% else %>
<div class="px-1" role="none">
<%= wrapper do %>
+ <% if selectable? %>
+ <span class="flex items-center justify-center w-5 h-5 shrink-0" aria-hidden="true">
+ <% if selected %><%= helpers.icon("check", color: destructive? ? :destructive : :default) %><% end %>
+ </span>
+ <% end %>
<% if icon %>
<%= helpers.icon(icon, color: destructive? ? :destructive : :default) %>
<% end %>
diff --git a/app/components/DS/menu_item.rb b/app/components/DS/menu_item.rb
index 7e55968c21..a6e07b4144 100644
--- a/app/components/DS/menu_item.rb
+++ b/app/components/DS/menu_item.rb
@@ -1,14 +1,14 @@
class DS::MenuItem < DesignSystemComponent
VARIANTS = %i[link button divider].freeze
- attr_reader :variant, :text, :icon, :href, :method, :destructive, :confirm, :frame, :roving, :opts
+ attr_reader :variant, :text, :icon, :href, :method, :destructive, :confirm, :frame, :roving, :selected, :opts
# `roving: true` (default) emits `tabindex="-1"` and `role="menuitem"` — correct
# for `DS::Menu`, which provides arrow-key roving and announces `role="menu"`.
# `roving: false` omits both so items stay in the normal Tab order — required
# inside `DS::Popover`, which has no roving handler and is not a `role="menu"`
# container.
- def initialize(variant:, text: nil, icon: nil, href: nil, method: :post, destructive: false, confirm: nil, frame: nil, roving: true, **opts)
+ def initialize(variant:, text: nil, icon: nil, href: nil, method: :post, destructive: false, confirm: nil, frame: nil, roving: true, selected: nil, **opts)
@variant = variant.to_sym
@text = text
@icon = icon
@@ -18,6 +18,7 @@ class DS::MenuItem < DesignSystemComponent
@confirm = confirm
@frame = frame
@roving = roving
+ @selected = selected
@opts = opts
raise ArgumentError, "Invalid variant: #{@variant}" unless VARIANTS.include?(@variant)
end
@@ -29,10 +30,18 @@ class DS::MenuItem < DesignSystemComponent
# `tabindex: 0` can't downgrade keyboard/AT semantics.
html_opts = roving ? merged_opts.except(:role, :tabindex) : merged_opts
+ # Merge the contract's ARIA (e.g. aria-checked for selectable rows) with any
+ # caller-supplied aria so neither clobbers the other; the contract wins per-key.
+ attrs = menuitem_attrs
+ if attrs[:aria] && html_opts[:aria]
+ html_opts = html_opts.merge(aria: html_opts[:aria].merge(attrs[:aria]))
+ attrs = attrs.except(:aria)
+ end
+
if variant == :button
- button_to href, method: method, class: container_classes, **html_opts, **menuitem_attrs, &block
+ button_to href, method: method, class: container_classes, **html_opts, **attrs, &block
elsif variant == :link
- link_to href, class: container_classes, **html_opts, **menuitem_attrs, &block
+ link_to href, class: container_classes, **html_opts, **attrs, &block
else
nil
end
@@ -49,9 +58,25 @@ class DS::MenuItem < DesignSystemComponent
method == :delete || destructive
end
+ # A selectable item participates in a single-select list (e.g. a period or
+ # category picker). When set (true/false) the item reserves a fixed-width
+ # leading check gutter so row text stays aligned whether or not it is the
+ # current selection. Leave nil for plain action items (no gutter).
+ def selectable?
+ !selected.nil?
+ end
+
private
def menuitem_attrs
- roving ? { role: "menuitem", tabindex: "-1" } : {}
+ return {} unless roving
+
+ # Selectable rows are a single-select group: announce selection state to AT
+ # via menuitemradio + aria-checked, not only the visual check gutter.
+ if selectable?
+ { role: "menuitemradio", tabindex: "-1", aria: { checked: selected ? "true" : "false" } }
+ else
+ { role: "menuitem", tabindex: "-1" }
+ end
end
def container_classes
diff --git a/app/components/DS/popover.html.erb b/app/components/DS/popover.html.erb
index 2b7e45d15e..7c73246da7 100644
--- a/app/components/DS/popover.html.erb
+++ b/app/components/DS/popover.html.erb
@@ -21,7 +21,7 @@
<% end %>
<div id="<%= panel_id %>" data-DS--popover-target="content" class="px-2 lg:px-0 max-w-full hidden z-50">
- <%= tag.div class: "mx-auto min-w-[200px] shadow-border-xs bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
+ <%= tag.div class: "mx-auto min-w-[200px] shadow-border-lg bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
<%= header %>
<%= tag.div class: class_names("py-1" => !no_padding) do %>
diff --git a/app/views/category/dropdowns/_row.html.erb b/app/views/category/dropdowns/_row.html.erb
index 067286e244..9099fd2151 100644
--- a/app/views/category/dropdowns/_row.html.erb
+++ b/app/views/category/dropdowns/_row.html.erb
@@ -20,7 +20,9 @@
data: { turbo_frame: "category_dropdown" },
class: "flex w-full items-center gap-1.5 cursor-pointer focus:outline-none" do %>
- <%= icon("check") if is_selected %>
+ <span class="flex items-center justify-center w-5 h-5 shrink-0">
+ <% if is_selected %><%= icon("check") %><% end %>
+ </span>
<% if category.subcategory? %>
<%= icon("corner-down-right", size: "sm") %>
diff --git a/test/components/DS/menu_item_test.rb b/test/components/DS/menu_item_test.rb
new file mode 100644
index 0000000000..944d3d47e8
--- /dev/null
+++ b/test/components/DS/menu_item_test.rb
@@ -0,0 +1,32 @@
+require "test_helper"
+
+class DS::MenuItemTest < ViewComponent::TestCase
+ test "selectable item reserves a fixed-width check gutter and shows the check when selected" do
+ render_inline(DS::MenuItem.new(variant: :link, text: "30D", href: "/", selected: true))
+
+ assert_selector "span.shrink-0.w-5", count: 1
+ assert_selector "span.shrink-0.w-5 svg", count: 1
+ # Selection is exposed to assistive tech, not only visually.
+ assert_selector "a[role='menuitemradio'][aria-checked='true']"
+ assert_text "30D"
+ end
+
+ test "selectable item keeps the reserved gutter (no glyph) when not selected" do
+ render_inline(DS::MenuItem.new(variant: :link, text: "90D", href: "/", selected: false))
+
+ # Gutter still present so text stays aligned with the selected row...
+ assert_selector "span.shrink-0.w-5", count: 1
+ # ...but no check glyph is drawn.
+ assert_no_selector "span.shrink-0.w-5 svg"
+ assert_selector "a[role='menuitemradio'][aria-checked='false']"
+ end
+
+ test "plain action item (selected: nil) renders no check gutter" do
+ render_inline(DS::MenuItem.new(variant: :link, text: "Edit", href: "/", icon: "pencil-line"))
+
+ assert_no_selector "span.shrink-0.w-5"
+ assert_selector "a[role='menuitem']"
+ assert_no_selector "[aria-checked]"
+ assert_text "Edit"
+ end
+end
diff --git a/test/components/previews/menu_component_preview.rb b/test/components/previews/menu_component_preview.rb
index 6fffb7ca31..002aa79695 100644
--- a/test/components/previews/menu_component_preview.rb
+++ b/test/components/previews/menu_component_preview.rb
@@ -18,6 +18,18 @@ class MenuComponentPreview < ViewComponent::Preview
end
end
+ # Single-select list. `selected:` reserves a fixed-width leading check gutter
+ # so the selected row's text stays aligned with every other row.
+ def selectable
+ render DS::Menu.new(variant: "button") do |menu|
+ menu.with_button(text: "30D", variant: "secondary")
+ menu.with_item(variant: "link", text: "7D", href: "#", selected: false)
+ menu.with_item(variant: "link", text: "30D", href: "#", selected: true)
+ menu.with_item(variant: "link", text: "90D", href: "#", selected: false)
+ menu.with_item(variant: "link", text: "Year to Date", href: "#", selected: false)
+ end
+ end
+
private
def menu_contents(menu)
menu.with_item(variant: "link", text: "Link", href: "#", icon: "plus")