Context Menu
Displays a menu to the user — such as a set of actions or functions — triggered by a right click.
Usage
Basic
Right click the trigger, or press the ContextMenu key while it has focus (give it tabindex: 0 if it holds nothing focusable): the menu opens at the pointer and takes focus. Arrow keys, Home/End and typing a letter move through the items; Enter or Space picks one, Esc closes. While it is open the page behind is inert and doesn't scroll.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent do
ContextMenuGroup do
ContextMenuItem { "Back" }
ContextMenuItem(disabled: true) { "Forward" }
ContextMenuItem { "Reload" }
end
end
end
With shortcuts
ContextMenuShortcut shows the key combination; shortcut: on ContextMenuItem is a shorthand for it. It only displays the keys: binding them is up to you.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent(class: "w-64") do
ContextMenuGroup do
ContextMenuItem do
plain "Back"
ContextMenuShortcut { "⌘[" }
end
ContextMenuItem(shortcut: "⌘]", disabled: true) { "Forward" }
ContextMenuItem(shortcut: "⌘R") { "Reload" }
end
ContextMenuSeparator()
ContextMenuGroup do
ContextMenuItem(shortcut: "⌘S") { "Save Page As…" }
ContextMenuItem(shortcut: "⇧⌘I") { "Developer Tools" }
end
end
end
With groups
ContextMenuGroup groups items; a ContextMenuLabel inside it names the group for assistive tech.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent(class: "w-48") do
ContextMenuGroup do
ContextMenuLabel { "File" }
ContextMenuItem { "New File" }
ContextMenuItem { "Open File" }
end
ContextMenuSeparator()
ContextMenuGroup do
ContextMenuLabel { "Edit" }
ContextMenuItem { "Undo" }
ContextMenuItem { "Redo" }
end
end
end
Links
Items render as divs; href: turns one into a link that navigates when picked.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent do
ContextMenuItem(href: "#links") { "History" }
ContextMenuItem(href: "#links") { "Settings" }
end
end
Checkboxes
ContextMenuCheckboxItem toggles aria-checked and keeps the menu open. Listen to ruby-ui--context-menu:change; event.detail.checked has the new state.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent(class: "w-64") do
ContextMenuCheckboxItem(checked: true) { "Show Bookmarks Bar" }
ContextMenuCheckboxItem { "Show Full URLs" }
ContextMenuCheckboxItem(shortcut: "⌘⇧D") { "Show Developer Tools" }
end
end
Radio group
ContextMenuRadioItems in a ContextMenuRadioGroup: checking one unchecks the rest. In ruby-ui--context-menu:change, event.detail.value carries the item's value: argument.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent(class: "w-48") do
ContextMenuRadioGroup do
ContextMenuLabel(inset: true) { "People" }
ContextMenuRadioItem(value: "pedro", checked: true) { "Pedro Duarte" }
ContextMenuRadioItem(value: "colm") { "Colm Tuite" }
end
end
end
Destructive
variant: :destructive marks an item that deletes or can't be undone.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent do
ContextMenuGroup do
ContextMenuItem { "Edit" }
ContextMenuItem { "Share" }
end
ContextMenuSeparator()
ContextMenuGroup do
ContextMenuItem(variant: :destructive) { "Delete" }
end
end
end
Inset
inset: indents an item or label (pl-8), e.g. to line it up with items that start with an icon.
ContextMenu do
ContextMenuTrigger(class: "flex h-[150px] w-[300px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click here" }
ContextMenuContent(class: "w-52") do
ContextMenuGroup do
ContextMenuItem(inset: true) { "Copy" }
ContextMenuItem(inset: true) { "Cut" }
end
ContextMenuSeparator()
ContextMenuCheckboxItem(checked: true) { "Show Bookmarks Bar" }
ContextMenuSeparator()
ContextMenuGroup do
ContextMenuLabel(inset: true) { "More Tools" }
ContextMenuItem(inset: true) { "Developer Tools" }
end
end
end
Side
side: (top, right, bottom, left) and align: (start, center, end) place the menu relative to the pointer; side_offset: and align_offset: set the gap. The defaults are right, start, 0 and 4.
div(class: "grid gap-4 sm:grid-cols-2") do
ContextMenu do
ContextMenuTrigger(class: "flex h-[100px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click (top)" }
ContextMenuContent(side: :top) do
ContextMenuItem { "Back" }
ContextMenuItem { "Forward" }
ContextMenuItem { "Reload" }
end
end
ContextMenu do
ContextMenuTrigger(class: "flex h-[100px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click (right)" }
ContextMenuContent(side: :right) do
ContextMenuItem { "Back" }
ContextMenuItem { "Forward" }
ContextMenuItem { "Reload" }
end
end
ContextMenu do
ContextMenuTrigger(class: "flex h-[100px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click (bottom)" }
ContextMenuContent(side: :bottom) do
ContextMenuItem { "Back" }
ContextMenuItem { "Forward" }
ContextMenuItem { "Reload" }
end
end
ContextMenu do
ContextMenuTrigger(class: "flex h-[100px] items-center justify-center rounded-md border border-dashed text-sm") { "Right click (left)" }
ContextMenuContent(side: :left) do
ContextMenuItem { "Back" }
ContextMenuItem { "Forward" }
ContextMenuItem { "Reload" }
end
end
end
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component ContextMenu
Manual installation
1
Add RubyUI::ContextMenu to app/components/ruby_ui/context_menu/context_menu.rb
# frozen_string_literal: true
module RubyUI
# The root. Placement lives on ContextMenuContent (side:, align:, offsets).
class ContextMenu < Base
def initialize(**attrs)
if attrs.key?(:options)
raise ArgumentError, "ContextMenu(options:) was removed; use ContextMenuContent(side:, align:, side_offset:, align_offset:)"
end
super
end
def view_template(&)
div(**attrs, &)
end
private
def default_attrs
{
data: {
slot: "context-menu",
controller: "ruby-ui--context-menu"
}
}
end
end
end
2
Add RubyUI::ContextMenuCheckboxItem to app/components/ruby_ui/context_menu/context_menu_checkbox_item.rb
# frozen_string_literal: true
module RubyUI
# A toggle in the menu (role menuitemcheckbox). Activating it flips
# aria-checked, keeps the menu open and fires ruby-ui--context-menu:change
# with {checked} in the detail.
class ContextMenuCheckboxItem < Base
ITEM = [
"group/context-menu-checkbox-item relative flex cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none",
"focus:bg-accent focus:text-accent-foreground",
"data-disabled:pointer-events-none data-disabled:opacity-50 data-inset:pl-8",
"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
].freeze
def initialize