[Feature] Allow DropdownMenuItem to render as a div via `as:`

Merged
JA
5mo ago
+69-5

Description

Adds an as: option to RubyUI::DropdownMenuItem (default :a, option :div).

By default a DropdownMenuItem renders as <a href>. When an item needs to host its own interactive element — a dialog trigger, a form, etc. — wrapping it in the item's anchor produces a <button> (or <form>) nested inside an <a>. That is invalid HTML and a keyboard/accessibility problem (nested interactive elements). With as: :div the item renders as <div role="menuitem">, so the interactive child lives in a valid div > trigger > button structure while the item keeps its styling, role="menuitem", keyboard navigation and close-on-click behavior.

This mirrors the as: option recently added to TabsTrigger.

Behavior

  • Default is unchanged: DropdownMenuItem still renders <a href="#">, so existing items are unaffected (backward compatible).
  • DropdownMenuItem(as: :div) renders <div role="menuitem"> with the same classes/data-*/role and no href.
  • href is tied to the rendered element (unless @as == :div), so an unexpected/mistyped as: value still falls back to a valid <a href> rather than an anchor missing its href.

Usage

DropdownMenu do
  DropdownMenuTrigger(class: "w-full") { Button(variant: :outline) { "Open" } }
  DropdownMenuContent do
    DropdownMenuItem(href: "#") { "Edit" }
    DropdownMenuItem(as: :div) do
      Dialog do
        DialogTrigger(class: "w-full text-left") { "Delete" }
        DialogContent { "…" }
      end
    end
  end
end

Changes

  • gem/lib/ruby_ui/dropdown_menu/dropdown_menu_item.rb — add as: option, conditional element rendering, and href tied to the rendered element.
  • gem/test/ruby_ui/dropdown_menu_item_test.rb — new tests: renders as <a href> by default, renders as <div role="menuitem"> without href when as: :div, and an unexpected as: value still renders a valid <a href>.
  • docs/app/views/docs/dropdown_menu.rb — add a "Non-navigational item" example.
  • mcp/data/registry.json — rebuilt to reflect the component change.

How to test

cd gem
bundle exec rake test TEST=test/ruby_ui/dropdown_menu_item_test.rb
bundle exec rake   # full suite + standardrb

🤖 Generated with Claude Code


Summary by cubic

Adds an as: option to DropdownMenuItem so items can render as a <div> for non-navigational content (e.g. dialog or form triggers) without nesting interactive elements. Default remains <a href>, so this is backward compatible.

  • New Features
    • DropdownMenuItem(as: :div) renders <div role="menuitem"> and omits href.
    • Keeps styling, role="menuitem", keyboard navigation, and close-on-click.
    • href only applies to <a>; unexpected as: values fall back to a valid <a href>.
    • Added a docs example and tests.