Demo
Controlled context menu
Documented in Context Menu.
Controlled context menu
A context menu whose open state lives in the application, opened from code at an anchorPoint or by right-click at the pointer, with collision-aware placement and a right-to-left switch that mirrors the menu and its submenu.
Last change:
Demo.tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { ContextMenu, Menu, MenuItem, Submenu, type ContextMenuPoint } from '@salmonui/buttons/menu';
export default function ContextMenuControlledDemo() {
const [open, setOpen] = useState(false);
const [anchorPoint, setAnchorPoint] = useState<ContextMenuPoint | undefined>(undefined);
const [rtl, setRtl] = useState(false);
const [last, setLast] = useState('nothing yet');
return (
<div dir={rtl ? 'rtl' : 'ltr'}>
<div className="demo-toolbar">
<Button
variant="outline"
onClick={(event) => {
// Open from code at the button's corner (viewport coordinates).
const rect = event.currentTarget.getBoundingClientRect();
setAnchorPoint({ x: rtl ? rect.right : rect.left, y: rect.bottom });
setOpen(true);
}}
>
Open from code
</Button>
<Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
Right to left
</Checkbox>
{/* English status text keeps its own direction inside the mirrored demo. */}
<span dir="ltr">
open = <output>{String(open)}</output>
</span>
</div>
<ContextMenu
tabIndex={0}
role="group"
aria-label="Board"
style={{ display: 'grid', placeItems: 'end', padding: 16, minBlockSize: 200, border: '1px dashed var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}
open={open}
anchorPoint={anchorPoint}
onBeforeOpen={() => setAnchorPoint(undefined)}
onOpenChange={(next, event) => {
setOpen(next);
setLast(`${next ? 'opened' : 'closed'} (${event.reason})`);
}}
onAction={(event) => setLast(`action ${event.id}`)}
menu={
<Menu aria-label="Board actions">
<MenuItem id="add-card">Add card</MenuItem>
<MenuItem id="add-list">Add list</MenuItem>
<Submenu label="Move board">
<MenuItem id="move-top">To the top</MenuItem>
<MenuItem id="move-archive">To the archive</MenuItem>
</Submenu>
<MenuItem id="close" onAction={() => setOpen(false)}>
Close menu
</MenuItem>
</Menu>
}
>
<span>Right-click near this corner of the page</span>
</ContextMenu>
<p className="demo-status" dir="ltr">
Last change: <output>{last}</output>
</p>
</div>
);
}
- Open from code sets
anchorPoint(viewport coordinates) andopen={true}, so code opens the menu at any point and closes it by settingopen={false}. Every close goes throughonOpenChange(false, …), so theopenreadout follows Escape, outside presses and actions. - A right-click clears
anchorPointinonBeforeOpen, so the menu opens at the pointer. Right-click near the bottom or the inline end of the window: the menu flips or shifts to stay in the viewport. - Right to left sets
dir="rtl"on the wrapper: the menu extends to the left of the point and Move board opens its submenu to the left (ArrowLeft opens it from the keyboard).