Context Menu
ContextMenu - Phlex implementation
Examples
Default
Displays a menu to the user triggered by a right click.
Right click this area
<%= render "ui/context_menu" do %>
<%= render "ui/context_menu/trigger" do %>
Right click this area
<% end %>
<%= render "ui/context_menu/content", classes: "w-48" do %>
<%= render "ui/context_menu/item" do %>Edit<% end %>
<%= render "ui/context_menu/item" do %>Duplicate<% end %>
<%= render "ui/context_menu/separator" %>
<%= render "ui/context_menu/item", variant: "destructive" do %>Delete<% end %>
<% end %>
<% end %>
Right click here
<%= render UI::ContextMenu.new do %>
<%= render UI::ContextMenuTrigger.new do %>
Right click here
<% end %>
<%= render UI::ContextMenuContent.new(classes: "w-48") do %>
<%= render UI::ContextMenuItem.new { "Edit" } %>
<%= render UI::ContextMenuItem.new { "Duplicate" } %>
<%= render UI::ContextMenuSeparator.new %>
<%= render UI::ContextMenuItem.new(variant: "destructive") { "Delete" } %>
<% end %>
<% end %>
Right click this box
<%= render UI::ContextMenuComponent.new do %>
<%= render UI::ContextMenuTriggerComponent.new do %>
Right click this box
<% end %>
<%= render UI::ContextMenuContentComponent.new(classes: "w-48") do %>
<%= render(UI::ContextMenuItemComponent.new) { "Edit" } %>
<%= render(UI::ContextMenuItemComponent.new) { "Duplicate" } %>
<%= render UI::ContextMenuSeparatorComponent.new %>
<%= render(UI::ContextMenuItemComponent.new(variant: "destructive")) { "Delete" } %>
<% end %>
<% end %>With Shortcuts
Context menu with keyboard shortcuts.
Right click for file options
<%= render "ui/context_menu" do %>
<%= render "ui/context_menu/trigger" do %>
Right click for file options
<% end %>
<%= render "ui/context_menu/content", classes: "w-56" do %>
<%= render "ui/context_menu/label" do %>File<% end %>
<%= render "ui/context_menu/separator" %>
<%= render "ui/context_menu/item" do %>
New File
<%= render "ui/context_menu/shortcut" do %>⌘N<% end %>
<% end %>
<%= render "ui/context_menu/item" do %>
Save
<%= render "ui/context_menu/shortcut" do %>⌘S<% end %>
<% end %>
<% end %>
<% end %>
Right click here
<%= render UI::ContextMenu.new do %>
<%= render UI::ContextMenuTrigger.new do %>
Right click here
<% end %>
<%= render UI::ContextMenuContent.new(classes: "w-56") do %>
<%= render UI::ContextMenuLabel.new { "File" } %>
<%= render UI::ContextMenuSeparator.new %>
<%= render UI::ContextMenuItem.new do %>
New File
<%= render UI::ContextMenuShortcut.new { "⌘N" } %>
<% end %>
<%= render UI::ContextMenuItem.new do %>
Save
<%= render UI::ContextMenuShortcut.new { "⌘S" } %>
<% end %>
<% end %>
<% end %>
Right click this box
<%= render UI::ContextMenuComponent.new do %>
<%= render UI::ContextMenuTriggerComponent.new do %>
Right click this box
<% end %>
<%= render UI::ContextMenuContentComponent.new(classes: "w-56") do %>
<%= render(UI::ContextMenuLabelComponent.new) { "File" } %>
<%= render UI::ContextMenuSeparatorComponent.new %>
<%= render UI::ContextMenuItemComponent.new do %>
New File
<%= render(UI::ContextMenuShortcutComponent.new) { "⌘N" } %>
<% end %>
<%= render UI::ContextMenuItemComponent.new do %>
Save
<%= render(UI::ContextMenuShortcutComponent.new) { "⌘S" } %>
<% end %>
<% end %>
<% end %>Features
- Custom styling with Tailwind classes
- Keyboard navigation with arrow keys
- Click outside to close
API Reference
Context Menu
Container for context menus triggered by right-click.
Content
Menu items container with animations and positioning.
Item
Individual menu item that can be rendered as a link or div.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| href | String | nil | The href |
| inset | Boolean | false | The inset |
| variant | String | default | Visual style variant |
Label
Non-interactive label for grouping menu items.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| inset | Boolean | false | The inset |
Separator
A visual divider between menu items.
Shortcut
Displays keyboard shortcut text for a menu item.
Trigger
The element that triggers the context menu on right-click.
Accessibility
Adheres to theMenu WAI-ARIA design pattern
Implements the WAI-ARIA Menu pattern with proper roles, states, and keyboard navigation.
Keyboard Shortcuts
| Key | Description |
|---|---|
| Enter | Activates the focused element |
| Escape | Closes the component |
| ArrowDown | Moves focus to next item |
| ArrowUp | Moves focus to previous item |
| Home | Moves focus to first item |
| End | Moves focus to last item |
JavaScript
Stimulus Controller
ui--context-menuValues
| Name | Type | Description |
|---|---|---|
| open | Boolean | Controls open state |
Actions
openclosecloseAllContextMenusteardownKeyboardNavigationfindCurrentItemIndextrackHoveredItempositionContextMenu