Menu for anchored actions and small forms
Menu in Cratis Components opens a panel anchored to a trigger you render. Use it for row actions, overflow menus, or a small form that belongs next to the control that opens it. The panel portals out of clipping and scrolling parents, stays inside the viewport, and has no PrimeReact dependency.
Basic usage
Section titled “Basic usage”Render your own trigger and spread the props the menu hands you onto it. The children are a function that receives close.
import { Button } from '@cratis/components/Common';import { Menu, MenuItem } from '@cratis/components/Menu';
export function RowActions({ onRename, onDelete }: { onRename: () => void; onDelete: () => void }) { return ( <Menu label='Row actions' trigger={(props) => <Button {...props}>Actions</Button>} align='end' > {() => ( <> <MenuItem onSelect={onRename}>Rename</MenuItem> <MenuItem onSelect={onDelete}>Delete</MenuItem> </> )} </Menu> );}Import the stylesheet once, either the aggregate @cratis/components/styles or the area sheet @cratis/components/Menu/styles. Follow the styling setup for token defaults and the shared base needed with per-area stylesheets.
Placement
Section titled “Placement”| Prop | Values | Effect |
|---|---|---|
side |
'below' (default), 'right' |
Which edge of the trigger the panel opens against. |
align |
'start' (default), 'end' |
Which physical edge of the trigger the panel lines up with. |
width |
pixels | A fixed panel width. Without it the panel fits its content. |
align is physical, not logical: below the trigger 'start' is the left edge and 'end' the right edge, beside it they are the top and bottom edges. They do not flip in right-to-left layouts, so choose the value for the layout direction you render in.
The panel is positioned from the trigger’s bounding box and clamped to the viewport. A below panel flips above the trigger when it does not fit below and does fit above. The position is recalculated when the panel’s content changes size and when the window resizes or anything scrolls.
Closing and focus
Section titled “Closing and focus”The menu closes on Escape, on a pointer press outside the panel and trigger, and when you call close. Escape and close() return focus to the trigger; an outside press leaves focus where the press put it. Call close(false) when focus is moving somewhere else. A MenuItem calls its onSelect and then closes the menu. Focus only returns to the trigger when it has not already moved elsewhere, so an item that opens a dialog keeps the dialog’s focus. A press inside content the panel portals out, such as a dropdown or a nested Menu, counts as inside.
Keyboard
Section titled “Keyboard”With the default role='menu', focus moves to the first item when the menu opens. Up and Down move between enabled items and wrap at the ends, Home and End jump to the first and last, and Tab closes the menu.
Menu items
Section titled “Menu items”MenuItem is for the default role='menu' panel. It renders a role="menuitem" button and forwards other attributes (id, aria-*, data-*, className) to it. In role='dialog' mode use regular buttons and content instead, because menuitem is only valid inside a menu.
A form in the panel
Section titled “A form in the panel”Set role='dialog' when the panel holds a form or other content. The trigger then announces aria-haspopup="dialog", arrow keys are left to the content, and Tab cycles within the panel. Focus moves to the first focusable element on open.
<Menu label='Rename' role='dialog' width={260} trigger={(props) => <Button {...props}>Rename</Button>}> {close => ( <form onSubmit={event => { event.preventDefault(); close(); }}> <input aria-label='Name' /> <button type='submit'>Save</button> </form> )}</Menu>Controlled use
Section titled “Controlled use”The menu is uncontrolled by default. Pass open and onOpenChange to control it.
const [open, setOpen] = useState(false);
<Menu label='Actions' open={open} onOpenChange={setOpen} trigger={(props) => <Button {...props}>Actions</Button>}> {() => <MenuItem onSelect={() => console.log('picked')}>Pick</MenuItem>}</Menu>Accessibility
Section titled “Accessibility”The trigger props carry aria-haspopup, aria-expanded and aria-controls. The panel has role="menu" (or role="dialog") and an accessible name from label. A MenuItem renders role="menuitem".
Styling
Section titled “Styling”The panel uses --cratis-* tokens (--cratis-surface-overlay, --cratis-surface-border, --cratis-surface-hover, --cratis-border-radius, --cratis-primary-color) and stacks above an enclosing dialog. Pass className to add your own class to the panel.