Context menu
In code: ContextMenu
A menu on right-click (or a long press on a phone). Use the same <MenuItem>s inside. Always offer the same actions another way too.
Preview
- Price list.pdf
- Floor plan.png
Right-click a file, or press and hold
Use
import {
ContextMenu,
ContextMenuContent,
ContextMenuTrigger,
List,
ListItem,
MenuItem,
MenuSeparator,
Stack,
toast,
} from '@/components/ui';
import { Copy, File, Pencil, Trash2 } from 'lucide-react';
<Stack gap={2}>
<List aria-label="Files">
{['Price list.pdf', 'Floor plan.png'].map((file) => (
<ContextMenu key={file}>
<ContextMenuTrigger render={<ListItem />}>
<File />
<span className="ui-grow">{file}</span>
</ContextMenuTrigger>
<ContextMenuContent>
<MenuItem onClick={() => toast.show({ title: `Renaming ${file}` })}><Pencil />Rename</MenuItem>
<MenuItem onClick={() => toast.show({ title: `${file} copied` })}><Copy />Duplicate</MenuItem>
<MenuSeparator />
<MenuItem danger onClick={() => toast.show({ title: `${file} deleted` })}><Trash2 />Delete</MenuItem>
</ContextMenuContent>
</ContextMenu>
))}
</List>
<p className="ui-small ui-muted">Right-click a file, or press and hold</p>
</Stack>Options
No options of its own.
It also takes the 21 attributes of the element or UI SDK part it draws: className, aria-label, events and the rest.
Parts
What goes inside one.
Without React
With the CSS in parts (/ui/css/), it needs these, besides the ones every page loads:
components/button.csscomponents/popups.csscomponents/icons.csscomponents/menu-items-tick.csscomponents/prose.cssparts/listbox.cssparts/menubar.cssIn the examples
Small screens made with it and the rest of the kit. They work: click, type and choose in them.
- Linen tote
- Enamel mug
- Wall calendarFeatured
- Sticker sheet
On top of the page: states, do and don’t
The same menu on right-click or long press, with the same MenuItems. The trigger takes focus, and the menu key or Shift+F10 opens the menu. Always offer the actions another way too.
States. Closed, opening (fades and rises), open, closing (fades before what it changed appears: afterClose). While open, focus is inside and Escape closes it; when it closes, focus goes back to what opened it.
Do
- Name every dialog, drawer and popover (
DialogTitle,DrawerTitle, oraria-label). - Use
AlertDialogfor anything that cannot be undone, with the action named on its button ("Delete 3 files"). - Use a
Draweron a phone for what aPopoverdoes on a computer.
Don't
- Don't open a dialog from a dialog; change what the open one shows instead.
- Don't put the only way to do something in a right-click menu or a tooltip.
- Don't use a toast for something the person must not miss: that is an
Alerton the page.