Menu
ui-sdkDisplays a dropdown menu or a context menu of actions and options
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="circle-user-round" class="text-muted-foreground"/>
Ibn Battuta
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>My Account</sa-menu-label>
<sa-menu-separator/>
<sa-menu-group>
<sa-menu-item shortcut="mod+shift+p">
<sa-icon name="user"/>
Profile
</sa-menu-item>
<sa-menu-item shortcut="mod+b">
<sa-icon name="luggage"/>
My Bookings
</sa-menu-item>
<sa-menu-item shortcut="mod+,">
<sa-icon name="settings"/>
Settings
</sa-menu-item>
</sa-menu-group>
<sa-menu-separator/>
<sa-menu-item variant="MenuItemVariant.Destructive" shortcut="mod+shift+q">
<sa-icon name="log-out"/>
Log out
</sa-menu-item>
</sa-menu-content>
</sa-menu>Usage
Wrap <sa-menu-trigger> and <sa-menu-content> inside <sa-menu>. The trigger renders an <sa-button> that opens the menu, and the content holds the menu's items.
Add actions using <sa-menu-item>, and organize them with <sa-menu-label>, <sa-menu-separator> and <sa-menu-group>. To style an item as dangerous, set variant to MenuItemVariant.Destructive.
<sa-menu>
<sa-menu-trigger>Open Menu</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>My Account</sa-menu-label>
<sa-menu-separator/>
<sa-menu-item shortcut="mod+shift+p">
<sa-icon name="user"/>
Profile
</sa-menu-item>
<sa-menu-item variant="MenuItemVariant.Destructive">
<sa-icon name="log-out"/>
Log out
</sa-menu-item>
</sa-menu-content>
</sa-menu>Set shortcut on an item to display a keyboard shortcut at the end of the item, such as mod+shift+p. mod is displayed as Command on a Mac and as Ctrl elsewhere. The shortcut is only a hint, so your page must handle the shortcuts itself.
Built on ui-sdk
<sa-menu> renders ui-sdk's Menu web component, <ui-menu>. For all its properties, methods and events, see ui-sdk's Menu docs.
Examples
Checkbox items
Use <sa-menu-checkbox-item> for options that can be turned on and off independently. Set checked="true" on the items that are checked when the page loads. The menu stays open while people toggle the items.
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="sliders-horizontal" class="text-muted-foreground"/>
Trip Filters
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Show Categories</sa-menu-label>
<sa-menu-separator/>
<sa-menu-checkbox-item checked="true">Flights</sa-menu-checkbox-item>
<sa-menu-checkbox-item checked="true">Accommodation</sa-menu-checkbox-item>
<sa-menu-checkbox-item>Car Rental</sa-menu-checkbox-item>
</sa-menu-content>
</sa-menu>Radio group
For a set of options where only one can be selected, place <sa-menu-radio-item> elements in an <sa-menu-radio-group>. The item whose value matches the group's value is selected when the page loads. The menu stays open while people select an item.
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="funnel" class="text-muted-foreground"/>
Booking Status
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Filter by Status</sa-menu-label>
<sa-menu-separator/>
<sa-menu-radio-group value="confirmed">
<sa-menu-radio-item value="confirmed">Confirmed</sa-menu-radio-item>
<sa-menu-radio-item value="pending">Pending</sa-menu-radio-item>
<sa-menu-radio-item value="cancelled">Cancelled</sa-menu-radio-item>
</sa-menu-radio-group>
</sa-menu-content>
</sa-menu>Submenus
To add a submenu, place an <sa-menu-sub> with an <sa-menu-sub-trigger> and an <sa-menu-sub-content> in the menu. A submenu opens when the pointer rests on its trigger, or when people press →, and closes when they press ←.
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="ellipsis" class="text-muted-foreground"/>
Trip Actions
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-item>
<sa-icon name="ticket"/>
View Tickets
</sa-menu-item>
<sa-menu-item>
<sa-icon name="calendar-plus"/>
Add to Calendar
</sa-menu-item>
<sa-menu-separator/>
<sa-menu-sub>
<sa-menu-sub-trigger>
<sa-icon name="map-pin"/>
Add Destination
</sa-menu-sub-trigger>
<sa-menu-sub-content class="w-44">
<sa-menu-item>Paris</sa-menu-item>
<sa-menu-item>Bangkok</sa-menu-item>
<sa-menu-item>Kyoto</sa-menu-item>
<sa-menu-item>Cape Town</sa-menu-item>
</sa-menu-sub-content>
</sa-menu-sub>
<sa-menu-separator/>
<sa-menu-item variant="MenuItemVariant.Destructive">
<sa-icon name="trash"/>
Cancel Trip
</sa-menu-item>
</sa-menu-content>
</sa-menu>Inset
Set inset="true" on an item, a label or a submenu trigger to indent its text by the width of the icon column. Use it to line up text-only items with items that have a leading icon or a check indicator.
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="eye" class="text-muted-foreground"/>
Trip View
</sa-menu-trigger>
<sa-menu-content class="w-56">
<!-- These items have a leading icon, so their label starts past the icon column -->
<sa-menu-group>
<sa-menu-item>
<sa-icon name="map"/>
Map
</sa-menu-item>
<sa-menu-item>
<sa-icon name="list"/>
List
</sa-menu-item>
<sa-menu-item>
<sa-icon name="calendar"/>
Calendar
</sa-menu-item>
</sa-menu-group>
<sa-menu-separator/>
<!-- inset="true" indents the label and items so their text lines up with the icon'd items above -->
<sa-menu-label inset="true">Sort by</sa-menu-label>
<sa-menu-group>
<sa-menu-item inset="true">Price</sa-menu-item>
<sa-menu-item inset="true">Rating</sa-menu-item>
<sa-menu-item inset="true">Duration</sa-menu-item>
</sa-menu-group>
</sa-menu-content>
</sa-menu>Links
To use a link as a menu item, set as-child="true" on <sa-menu-item> and place an <a> element inside it. The <a> becomes the item, styled like the other items, and takes an href or the ASP.NET Core routing attributes, such as asp-page and asp-route-*.
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="compass" class="text-muted-foreground"/>
Quick Links
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Navigate</sa-menu-label>
<sa-menu-separator/>
<sa-menu-group>
<!-- Link to a Razor Page with a route value -->
<sa-menu-item as-child="true">
<a asp-page="/Booking/Details" asp-route-id="TRP-4821">
<sa-icon name="luggage"/>
View Booking
</a>
</sa-menu-item>
<!-- Link to an MVC controller action -->
<sa-menu-item as-child="true">
<a asp-controller="Search" asp-action="Index">
<sa-icon name="search"/>
Search Trips
</a>
</sa-menu-item>
<sa-menu-item as-child="true">
<a asp-controller="User" asp-action="Profile">
<sa-icon name="circle-user-round"/>
My Profile
</a>
</sa-menu-item>
</sa-menu-group>
<sa-menu-separator/>
<!-- External link via a raw href -->
<sa-menu-item as-child="true">
<a href="https://help.voyager.travel" target="_blank" rel="noopener noreferrer">
<sa-icon name="life-buoy"/>
Help Center
<sa-icon name="external-link" class="ml-auto size-4"/>
</a>
</sa-menu-item>
</sa-menu-content>
</sa-menu>as-child also works with a <button>, such as a form's submit button. Clicking the item clicks the button, but choosing the item with Enter or Space only raises the item's select event, so the button doesn't submit its form from the keyboard.
Handling actions
Give each item a value, and listen for the action event on the <sa-menu>. The event's detail.value holds the value of the item that was selected with the mouse or the keyboard. The menu closes after your event listener executes.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu id="--trip-actions">
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="ellipsis" class="text-muted-foreground"/>
Trip Actions
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Trip #TRV-987</sa-menu-label>
<sa-menu-separator/>
<sa-menu-item value="copy-link">
<sa-icon name="link"/>
Copy Trip Link
</sa-menu-item>
<sa-menu-item value="duplicate">
<sa-icon name="copy"/>
Duplicate Trip
</sa-menu-item>
<sa-menu-item value="print">
<sa-icon name="printer"/>
Print Itinerary
</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--trip-action-output">
Choose an action from the menu.
</p>
</sa-stack>
<script>
(() => {
const messages = {
'copy-link': 'Trip link copied to clipboard',
duplicate: 'Trip duplicated',
print: 'Sending itinerary to the printer...',
};
// The menu raises "action" with the chosen item's value, for the mouse and the
// keyboard alike, and closes once the listener has run.
document.getElementById('--trip-actions').addEventListener('action', (event) => {
document.getElementById('--trip-action-output').textContent = messages[event.detail.value];
});
})();
</script>The menu's events don't bubble, so listen on the element that raises them. An item raises select, a checkbox item checkedchange, a radio group valuechange, and the menu action, openchange and dismissattempt.
Keeping the menu open
Choosing a plain item closes the menu, while checkbox and radio items keep it open. To change this for an item, set close-on-select. Set it to false on a plain item that people may choose several times in a row, or to true on a checkbox or radio item to apply the choice and close the menu.
The example below listens for the select event of the Refresh availability item, which keeps the menu open.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="settings-2" class="text-muted-foreground"/>
Booking Options
</sa-menu-trigger>
<sa-menu-content class="w-64">
<sa-menu-label>Availability</sa-menu-label>
<sa-menu-separator/>
<!-- A plain item closes the menu by default; close-on-select="false" keeps it
open so you can choose it repeatedly. -->
<sa-menu-item id="--refresh-availability" close-on-select="false">
<sa-icon name="refresh-cw"/>
Refresh availability
</sa-menu-item>
<sa-menu-separator/>
<sa-menu-label>Preferences</sa-menu-label>
<!-- Checkbox items stay open by default... -->
<sa-menu-checkbox-item checked="true">Include breakfast</sa-menu-checkbox-item>
<!-- ...while close-on-select="true" applies the choice and dismisses the menu. -->
<sa-menu-checkbox-item close-on-select="true">Flexible dates (apply & close)</sa-menu-checkbox-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--booking-options-output">Refreshed 0 times.</p>
</sa-stack>
<script>
(() => {
let count = 0;
// An item raises "select" when it's chosen, by the mouse or the keyboard
document.getElementById('--refresh-availability').addEventListener('select', () => {
count++;
document.getElementById('--booking-options-output').textContent =
'Refreshed ' + count + ' time' + (count === 1 ? '' : 's') + ' — the menu stayed open.';
});
})();
</script>Reacting to checkbox changes
Toggling a checkbox item raises a checkedchange event on the item, with the item's new state in event.detail.value. To handle every item of a group with one listener, listen on the <sa-menu-group> in the capture phase, and use event.target to find the item that changed.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="sliders-horizontal" class="text-muted-foreground"/>
Trip Filters
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Show Categories</sa-menu-label>
<sa-menu-separator/>
<!-- One "checkedchange" listener on the group catches every item -->
<sa-menu-group id="--trip-filters">
<sa-menu-checkbox-item checked="true">Flights</sa-menu-checkbox-item>
<sa-menu-checkbox-item checked="true">Accommodation</sa-menu-checkbox-item>
<sa-menu-checkbox-item>Car Rental</sa-menu-checkbox-item>
</sa-menu-group>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--trip-filters-output"></p>
</sa-stack>
<script>
(() => {
const group = document.getElementById('--trip-filters');
const output = document.getElementById('--trip-filters-output');
// ui-sdk's "checkedchange" fires on the toggled item and doesn't bubble, so the group
// listens in the capture phase; event.detail.value is the item's new checked state.
// Seed the active set from whatever is checked on load.
const active = new Set(
[...group.querySelectorAll('[default-checked]')].map((el) => el.textContent.trim()),
);
const render = () => {
output.textContent = active.size ? 'Showing: ' + [...active].join(', ') : 'No categories selected';
};
// The menu stays open while toggling; the event fires for mouse and keyboard alike.
group.addEventListener('checkedchange', (event) => {
const label = event.target.textContent.trim();
event.detail.value ? active.add(label) : active.delete(label);
render();
}, { capture: true });
render();
})();
</script>Reacting to radio changes
Selecting a radio item raises a valuechange event on the <sa-menu-radio-group>, with the value of the selected item in event.detail.value.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="funnel" class="text-muted-foreground"/>
Booking Status
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Filter by Status</sa-menu-label>
<sa-menu-separator/>
<!-- Listen once on the group, which fires valuechange with the selected value -->
<sa-menu-radio-group id="--booking-status" value="confirmed">
<sa-menu-radio-item value="confirmed">Confirmed</sa-menu-radio-item>
<sa-menu-radio-item value="pending">Pending</sa-menu-radio-item>
<sa-menu-radio-item value="cancelled">Cancelled</sa-menu-radio-item>
</sa-menu-radio-group>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--booking-status-output"></p>
</sa-stack>
<script>
(() => {
const labels = { confirmed: 'Confirmed', pending: 'Pending', cancelled: 'Cancelled' };
const output = document.getElementById('--booking-status-output');
const show = (value) => {
output.textContent = 'Showing: ' + (labels[value] ?? value) + ' bookings';
};
// A single listener on the group gets the selected value from event.detail.value.
// The menu stays open.
document.getElementById('--booking-status').addEventListener('valuechange', (event) => {
show(event.detail.value);
});
show('confirmed'); // reflect the group's initial value
})();
</script>Opening a menu from elsewhere on the page
When several elements open the same menu, such as an actions button on each row of a list, give the <sa-menu> an id. Then place the triggers anywhere on the page, with their for attribute set to that id. The menu opens next to the trigger that opened it.
To find out which trigger opened the menu, listen for the openchange event on the <sa-menu>. Its detail.value is true when the menu opens, and detail.event.target is the element that was pressed. The example below reads the booking from a data-booking attribute on each trigger.
<sa-item-group>
@foreach (var (id, trip) in new[] { ("TRV-101", "Lisbon city break"), ("TRV-102", "Kyoto in autumn"), ("TRV-103", "Patagonia trek") })
{
<sa-item variant="ItemVariant.Outline" size="ItemSize.Small">
<sa-item-content>
<sa-item-title>@trip</sa-item-title>
<sa-item-description>Booking @id</sa-item-description>
</sa-item-content>
<sa-item-actions>
<!-- Each row's trigger opens the one menu below, named by its id -->
<sa-menu-trigger for="--booking-actions" variant="ButtonVariant.Ghost" size="ButtonSize.IconSmall"
aria-label="Actions for @id" data-booking="@id">
<sa-icon name="ellipsis"/>
</sa-menu-trigger>
</sa-item-actions>
</sa-item>
}
</sa-item-group>
<sa-menu id="--booking-actions">
<sa-menu-content align="MenuAlign.End" class="w-48">
<sa-menu-label id="--booking-actions-label">Booking</sa-menu-label>
<sa-menu-separator/>
<sa-menu-item value="Viewing">View Booking</sa-menu-item>
<sa-menu-item value="Rebooking">Change Dates</sa-menu-item>
<sa-menu-item value="Cancelling" variant="MenuItemVariant.Destructive">Cancel Booking</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground mt-3 text-sm" id="--booking-actions-output">Choose an action for a booking.</p>
<script>
(() => {
const menu = document.getElementById('--booking-actions');
const bookingOf = (event) => event.detail.event?.target.closest('[data-booking]')?.dataset.booking;
let booking;
// The trigger that opened the menu tells which booking the actions are for
menu.addEventListener('openchange', (event) => {
if (event.detail.value) {
booking = bookingOf(event) ?? booking;
document.getElementById('--booking-actions-label').textContent = 'Booking ' + booking;
}
});
menu.addEventListener('action', (event) => {
document.getElementById('--booking-actions-output').textContent = event.detail.value + ' ' + booking + '.';
});
})();
</script>Context menus
To open the menu when people right-click or long-press an area, use an <sa-menu-context-trigger> in place of the <sa-menu-trigger>. The menu opens at the pointer. The area can receive focus, so people can also open the menu by pressing Shift+F10 or the context menu key. Set disabled="true" to display the browser's own context menu instead.
<sa-menu id="--itinerary-menu">
<sa-menu-context-trigger class="flex h-36 w-72 items-center justify-center rounded-lg border border-dashed text-sm">
Right-click or long-press the itinerary
</sa-menu-context-trigger>
<sa-menu-content class="w-52">
<sa-menu-item value="Added a stop" shortcut="mod+n">Add Stop</sa-menu-item>
<sa-menu-item value="Duplicated the day" shortcut="mod+d">Duplicate Day</sa-menu-item>
<sa-menu-item value="Printed" shortcut="mod+p">Print Itinerary</sa-menu-item>
<sa-menu-separator/>
<sa-menu-checkbox-item checked="true">Show Travel Times</sa-menu-checkbox-item>
<sa-menu-separator/>
<sa-menu-item value="Cleared the day" variant="MenuItemVariant.Destructive">Clear Day</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground mt-3 text-sm" id="--itinerary-output">Nothing chosen yet.</p>
<script>
document.getElementById('--itinerary-menu').addEventListener('action', (event) => {
document.getElementById('--itinerary-output').textContent = event.detail.value + '.';
});
</script>Opening with a keyboard shortcut
Set shortcut on <sa-menu-trigger> to open the menu from anywhere on the page with a key combination, such as mod+j.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu>
<!-- The shortcut opens the menu from anywhere on the page -->
<sa-menu-trigger variant="ButtonVariant.Outline" shortcut="mod+j">
<sa-icon name="zap" class="text-muted-foreground"/>
Quick Actions
</sa-menu-trigger>
<sa-menu-content class="w-52">
<sa-menu-item>New Booking</sa-menu-item>
<sa-menu-item>Find Traveler</sa-menu-item>
<sa-menu-item>Check Flight Status</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm">
Press <sa-kbd>Ctrl</sa-kbd> <sa-kbd>J</sa-kbd>, or <sa-kbd>⌘</sa-kbd> <sa-kbd>J</sa-kbd> on a Mac.
</p>
</sa-stack>Presenting the menu as a sheet
On a phone, a menu is easier to use as a sheet that slides up from the bottom of the screen. Set presentation on <sa-menu> to MenuPresentation.Sheet to always present the menu as a sheet, or to MenuPresentation.Auto to present it as a sheet on small touch screens only. In a sheet, a submenu replaces the sheet's items, and its trigger is displayed above them. Press the trigger to go back.
A context menu always opens at the pointer, whatever its presentation.
<sa-menu presentation="MenuPresentation.Sheet">
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="share-2" class="text-muted-foreground"/>
Share Trip
</sa-menu-trigger>
<sa-menu-content>
<sa-menu-item>
<sa-icon name="link"/>
Copy Trip Link
</sa-menu-item>
<sa-menu-item>
<sa-icon name="mail"/>
Email Itinerary
</sa-menu-item>
<sa-menu-sub>
<sa-menu-sub-trigger>
<sa-icon name="users"/>
Share With
</sa-menu-sub-trigger>
<sa-menu-sub-content>
<sa-menu-item>Travel Companions</sa-menu-item>
<sa-menu-item>Travel Agent</sa-menu-item>
</sa-menu-sub-content>
</sa-menu-sub>
<sa-menu-separator/>
<sa-menu-item variant="MenuItemVariant.Destructive">
<sa-icon name="link-2-off"/>
Stop Sharing
</sa-menu-item>
</sa-menu-content>
</sa-menu>Pending actions
An action may take a while, such as a request to the server. To keep the menu open until the action finishes, pass a promise to event.detail.waitUntil() in your action or select listener. The item displays a spinner until the promise settles. When the promise resolves, the menu closes. When it rejects, the menu stays open, so people can try again.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-menu id="--sync-menu">
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="calendar-sync" class="text-muted-foreground"/>
Calendar
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-item value="sync">Sync Trip to Calendar</sa-menu-item>
<sa-menu-item value="share">Share With Travel Agent</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--sync-output">Choose an action.</p>
</sa-stack>
<script>
(() => {
const output = document.getElementById('--sync-output');
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
document.getElementById('--sync-menu').addEventListener('action', (event) => {
// The item stays pending, with the menu open, until the promise settles
if (event.detail.value === 'sync') {
event.detail.waitUntil(wait(1500).then(() => { output.textContent = 'Trip synced.'; }));
} else {
// A rejection keeps the menu open, so the action can be tried again
event.detail.waitUntil(wait(1500).then(() => {
output.textContent = 'The travel agent is offline. Try again.';
throw new Error('Offline');
}));
}
});
})();
</script>Positioning the menu
By default, the menu opens below its trigger, lined up with the trigger's start edge. Set side and align on <sa-menu-content> to open it elsewhere. When the menu doesn't fit, it flips to the other side or shifts along its trigger.
To adjust the position, set the following attributes on <sa-menu-content>:
| Attribute | Description |
|---|---|
side-offset | The gap between the menu and its trigger, in pixels. The default is 8. |
align-offset | How far the menu shifts along its trigger, in pixels. The default is 0. |
collision-padding | The space the menu keeps from the edges of the window, in pixels. The default is 8. |
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">Seat Options</sa-menu-trigger>
<!-- A 16px gap below the trigger, shifted 12px along it, and 24px kept clear of the window's edges -->
<sa-menu-content side-offset="16" align-offset="12" collision-padding="24" class="w-48">
<sa-menu-item>Window Seat</sa-menu-item>
<sa-menu-item>Aisle Seat</sa-menu-item>
<sa-menu-item>Extra Legroom</sa-menu-item>
</sa-menu-content>
</sa-menu>Interacting with the page behind the menu
By default, a press outside an open menu closes the menu without reaching the element that was pressed. To change this, set modal on <sa-menu> to one of the following:
| Value | Description |
|---|---|
MenuModal.Pointer | A press outside the menu closes the menu without reaching the element that was pressed. The default. |
MenuModal.Modal | The page behind the menu can't be scrolled or used, and focus stays in the menu. |
MenuModal.NonModal | A press outside the menu closes the menu and reaches the element that was pressed. |
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<sa-group>
<!-- The page behind can't be scrolled or used while the menu is open -->
<sa-menu modal="MenuModal.Modal">
<sa-menu-trigger variant="ButtonVariant.Outline">Modal</sa-menu-trigger>
<sa-menu-content class="w-44">
<sa-menu-item>Economy</sa-menu-item>
<sa-menu-item>Business</sa-menu-item>
<sa-menu-item>First</sa-menu-item>
</sa-menu-content>
</sa-menu>
<!-- A press outside closes the menu and reaches what it hit -->
<sa-menu modal="MenuModal.NonModal">
<sa-menu-trigger variant="ButtonVariant.Outline">Non-modal</sa-menu-trigger>
<sa-menu-content class="w-44">
<sa-menu-item>Economy</sa-menu-item>
<sa-menu-item>Business</sa-menu-item>
<sa-menu-item>First</sa-menu-item>
</sa-menu-content>
</sa-menu>
<sa-button id="--modal-counter" variant="ButtonVariant.Secondary">Passengers: 1</sa-button>
</sa-group>
<p class="text-muted-foreground text-sm">Open a menu, then press Passengers.</p>
</sa-stack>
<script>
(() => {
let passengers = 1;
const counter = document.getElementById('--modal-counter');
counter.addEventListener('click', () => { counter.textContent = 'Passengers: ' + ++passengers; });
})();
</script>Preventing the menu from closing
Set dismissible="false" on <sa-menu> so that pressing Esc, pressing outside the menu, or moving focus out of it doesn't close the menu. The trigger and choosing a plain item still close it. When the menu stays open, it raises a dismissattempt event, which you can use to tell people how to close it.
<sa-stack gap="StackGap.Small" align="StackAlign.Center">
<!-- Escape and presses outside don't close it; the trigger and Apply Filters do -->
<sa-menu id="--filter-menu" dismissible="false">
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="list-filter" class="text-muted-foreground"/>
Filter Hotels
</sa-menu-trigger>
<sa-menu-content class="w-56">
<sa-menu-label>Amenities</sa-menu-label>
<sa-menu-checkbox-item>Free Breakfast</sa-menu-checkbox-item>
<sa-menu-checkbox-item>Pool</sa-menu-checkbox-item>
<sa-menu-checkbox-item>Airport Shuttle</sa-menu-checkbox-item>
<sa-menu-separator/>
<sa-menu-item>Apply Filters</sa-menu-item>
</sa-menu-content>
</sa-menu>
<p class="text-muted-foreground text-sm" id="--filter-output">Choose some amenities.</p>
</sa-stack>
<script>
document.getElementById('--filter-menu').addEventListener('dismissattempt', () => {
document.getElementById('--filter-output').textContent = 'Apply the filters to close the menu.';
});
</script>Keyboard navigation
By default, the arrow keys wrap from the last item to the first. Set loop="false" on <sa-menu> to stop at the first and last items.
Typing moves to the item whose text starts with the typed letters. When an item contains more than its name, such as a description, set label on the item to the text that typing matches. label doesn't change the item's accessible name. Use aria-label for that.
<!-- The arrow keys stop at the first and last items instead of wrapping -->
<sa-menu loop="false">
<sa-menu-trigger variant="ButtonVariant.Outline">Travel Status</sa-menu-trigger>
<sa-menu-content class="w-64">
<!-- Typing matches the label, not the description beside it -->
<sa-menu-item label="Available">
<span class="flex flex-col">
<span>Available</span>
<span class="text-muted-foreground text-xs">Bookings come straight to you</span>
</span>
</sa-menu-item>
<sa-menu-item label="On a trip">
<span class="flex flex-col">
<span>On a trip</span>
<span class="text-muted-foreground text-xs">Bookings go to your team</span>
</span>
</sa-menu-item>
<sa-menu-item label="Offline">
<span class="flex flex-col">
<span>Offline</span>
<span class="text-muted-foreground text-xs">Bookings wait until you return</span>
</span>
</sa-menu-item>
</sa-menu-content>
</sa-menu>Submenu delay
A submenu opens when the pointer has rested on its trigger for 100 milliseconds. To change the delay, set delay on <sa-menu>, in milliseconds. A delay on an <sa-menu-sub> sets the delay for the submenus inside that submenu.
<!-- The pointer rests on Destinations for half a second before its submenu opens -->
<sa-menu delay="500">
<sa-menu-trigger variant="ButtonVariant.Outline">Explore</sa-menu-trigger>
<sa-menu-content class="w-48">
<sa-menu-item>Deals</sa-menu-item>
<!-- Inside Destinations, the region submenus open at once -->
<sa-menu-sub delay="0">
<sa-menu-sub-trigger>Destinations</sa-menu-sub-trigger>
<sa-menu-sub-content class="w-44">
<sa-menu-sub>
<sa-menu-sub-trigger>Europe</sa-menu-sub-trigger>
<sa-menu-sub-content>
<sa-menu-item>Lisbon</sa-menu-item>
<sa-menu-item>Prague</sa-menu-item>
</sa-menu-sub-content>
</sa-menu-sub>
<sa-menu-sub>
<sa-menu-sub-trigger>Asia</sa-menu-sub-trigger>
<sa-menu-sub-content>
<sa-menu-item>Kyoto</sa-menu-item>
<sa-menu-item>Hanoi</sa-menu-item>
</sa-menu-sub-content>
</sa-menu-sub>
</sa-menu-sub-content>
</sa-menu-sub>
<sa-menu-item>Travel Guides</sa-menu-item>
</sa-menu-content>
</sa-menu>Moving focus after the menu closes
When the menu closes, focus returns to its trigger. When an action takes people somewhere else, such as a field to fill in, set final-focus on <sa-menu-content> to a CSS selector for the element to focus. Set it to none to leave focus where your action put it.
<sa-stack gap="StackGap.Small">
<sa-menu>
<sa-menu-trigger variant="ButtonVariant.Outline">
<sa-icon name="user-plus" class="text-muted-foreground"/>
Add Traveler
</sa-menu-trigger>
<!-- Closing the menu moves focus to the name field rather than back to the trigger -->
<sa-menu-content final-focus="#--traveler-name" class="w-44">
<sa-menu-item>Adult</sa-menu-item>
<sa-menu-item>Child</sa-menu-item>
<sa-menu-item>Infant</sa-menu-item>
</sa-menu-content>
</sa-menu>
<sa-input id="--traveler-name" aria-label="Traveler name" placeholder="Traveler name"/>
</sa-stack>Accessibility
The menu follows the WAI-ARIA menu button pattern. The trigger has aria-haspopup="menu" and aria-expanded, the content has role="menu", and each item is a menuitem, menuitemcheckbox or menuitemradio, with aria-checked kept up to date. A disabled item has aria-disabled="true". It can receive focus, but can't be chosen.
Press Enter, Space or ↓ on the trigger to open the menu and focus the first item, or ↑ to focus the last item. In the menu, the arrow keys move between the items, Home and End move to the first and last items, and typing moves to the next item that starts with the typed letters. → opens a submenu, and ← closes it. Enter and Space choose the focused item, and Esc and Tab close the menu.
Give the trigger visible text, or an aria-label when it only contains an icon.
API Reference
<sa-menu>
Wraps a trigger and the menu's content, rendered as a <ui-menu> element that takes the id you give it. It raises the action, openchange and dismissattempt events.
Prop
Type
<sa-menu-trigger>
Renders an <sa-button> inside a <ui-menu-trigger> element. Inside an <sa-menu>, it opens that menu. Elsewhere on the page, it opens the menu that its for attribute names.
Prop
Type
<sa-menu-context-trigger>
Renders a <ui-menu-context-trigger> element that opens the menu at the pointer when it's right-clicked or long-pressed. Place it inside an <sa-menu>, in place of an <sa-menu-trigger>. It has tabindex="0" unless you set a tabindex.
Prop
Type
<sa-menu-content>
Renders a <ui-menu-content> element, which holds the menu's items.
Prop
Type
<sa-menu-item>
Renders a <ui-menu-item> element that performs an action. Choosing it raises a select event on the item, and an action event on the menu with the item's value.
Prop
Type
<sa-menu-checkbox-item>
Renders a <ui-menu-checkbox-item> element with a check indicator. Toggling it raises a checkedchange event on the item.
Prop
Type
<sa-menu-radio-group>
Renders a <ui-menu-radio-group> element, in which only one radio item can be selected. Selecting an item raises a valuechange event on the group.
Prop
Type
<sa-menu-radio-item>
Renders a <ui-menu-radio-item> element with a check indicator. It's selected when its value matches the group's value.
Prop
Type
<sa-menu-label>
Renders a heading for a section of items. Inside an <sa-menu-group>, it renders a <ui-menu-group-label> element, which labels the group.
Prop
Type
<sa-menu-separator>
Renders a <ui-menu-separator> element, a line between sections of items.
<sa-menu-group>
Renders a <ui-menu-group> element, which groups related items.
<sa-menu-sub>
Renders a <ui-menu-sub> element, which wraps a submenu's trigger and content.
Prop
Type
<sa-menu-sub-trigger>
Renders a <ui-menu-sub-trigger> element, an item with a trailing arrow that opens its submenu.
Prop
Type
<sa-menu-sub-content>
Renders a <ui-menu-sub-content> element, which holds the submenu's items.
Prop
Type