DropdownMenu
A menu of actions opened from a control of your own.
Use DropdownMenu for commands: a preset's Save and Copy, a panel's options, a module's Reset. DropdownMenu.Trigger takes the control that opens it, such as a Button, and DropdownMenu.Content the items. The menu opens beside the trigger and closes when an item is chosen, on Esc or Tab, or on a press outside it.
import { Button, DropdownMenu, Menu } from '@soundor/components';
import { useTheme } from '@soundor/components/theme';
import { Pressable, Text, View } from '@soundor/react';
import { useState } from 'react';
export default function Demo() {
const theme = useTheme();
const [last, setLast] = useState('Nothing yet');
const [snap, setSnap] = useState(true);
const run = (name: string) => () => setLast(name);
return (
<View style={{ alignItems: 'center', gap: 14 }}>
<View style={{ flexDirection: 'row', gap: 12 }}>
<DropdownMenu>
<DropdownMenu.Trigger>
<Button>Preset</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<Menu.Item onPress={run('Save')}>Save</Menu.Item>
<Menu.Item onPress={run('Save As')}>Save As…</Menu.Item>
<Menu.Divider />
<Menu.Item onPress={run('Copy')}>Copy Preset</Menu.Item>
<Menu.Item disabled>Paste Preset</Menu.Item>
<Menu.Divider />
<Menu.Item onPress={run('Initialize')}>Initialize</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>
{/* A trigger of your own: anything that takes onPress and a ref. */}
<DropdownMenu>
<DropdownMenu.Trigger>
<Pressable
accessibilityLabel="View options"
style={({ hovered, focusVisible }) => ({
width: 30,
height: 30,
borderRadius: theme.radius.radiusMd,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: hovered ? theme.color.surface : 'transparent',
outlineWidth: focusVisible ? theme.size.focusRing : 0,
outlineColor: theme.color.focus,
outlineOffset: 2,
})}
>
<Text style={{ color: theme.color.textSecondary, fontSize: 16 }}>
⋯
</Text>
</Pressable>
</DropdownMenu.Trigger>
<DropdownMenu.Content side="right" align="end">
<Menu.Label>View</Menu.Label>
<Menu.Item checked={snap} onPress={() => setSnap(!snap)}>
Snap to grid
</Menu.Item>
<Menu.Item onPress={run('Zoom to fit')}>Zoom to fit</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>
</View>
<Text
style={{
fontFamily: theme.font.mono,
fontSize: 11,
color: theme.color.textSecondary,
}}
>
Last action: {last}
</Text>
</View>
);
}dropdown-menu.tsx, running in the web runtime.SourceThe example's View menu prefers the right side, aligned to the trigger's end. Near an edge, shared popup placement can flip it to the opposite side or clamp it to the safe area. Placement uses Soundor logical view coordinates and follows a moving trigger even when its size stays the same.
import { Button, DropdownMenu, Menu } from '@soundor/components';
<DropdownMenu>
<DropdownMenu.Trigger>
<Button>Options</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<Menu.Item onPress={reset}>Reset</Menu.Item>
<Menu.Divider />
<Menu.Item onPress={copy}>Copy Value</Menu.Item>
<Menu.Item onPress={paste} disabled={!canPaste}>
Paste Value
</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>;Items
The same items go in a DropdownMenu and a ContextMenu:
Menu.Item: an action. Its label is its text, andonPressruns it. It can also havedisabled(shown dimmed and skipped by the keyboard),icon(a small glyph before the label),checked(a tick, for an option that is on) andhint(read after the label by a screen reader). Aniconmay also be a function of the row's state (active,checked,disabledandcolor, the label's colour), so a glyph of your own follows the label as the row is highlighted.checkedStyle="highlight"showscheckedas a Select shows its chosen option, the row filled with the selection accent and no tick: use it on every item of a group of choices, such as a filter type.Menu.Divider: a line between groups.Menu.Label: a heading over a group. A screen reader reads it; the keyboard passes over it.Menu.Sub: a row that opens another menu beside it. See Submenus.
onPress runs after the menu has closed and given focus back. An action can therefore open a Modal, which then returns focus to the trigger, or remove the menu's owner from the editor.
Items are ordinary React elements, so a component of yours can render them, and so can a condition or a map. Their order on screen is their order for the keyboard and for typing a label's first letters:
function PresetActions({ preset }: { preset: Preset }) {
return (
<>
<Menu.Label>Preset</Menu.Label>
<Menu.Item onPress={() => save(preset)}>Save</Menu.Item>
{preset.user && (
<Menu.Item onPress={() => remove(preset)}>Delete</Menu.Item>
)}
</>
);
}
<DropdownMenu.Content>
<PresetActions preset={preset} />
<Menu.Divider />
{recent.map((name) => (
<Menu.Item key={name} onPress={() => load(name)}>
{name}
</Menu.Item>
))}
</DropdownMenu.Content>;Menu is only the items' namespace: there is no <Menu> to render. A Menu.Item outside a menu's content throws, saying so.
Submenus
Menu.Sub puts a menu inside a menu: a Menu.SubTrigger, the row that opens it (its label is its text, and a chevron follows it), and a Menu.SubContent with the rows it opens. The submenu takes the same parts as any menu, Menu.Sub included, so submenus nest to any depth, in a DropdownMenu and in a ContextMenu alike.
import { Button, DropdownMenu, Menu } from '@soundor/components';
import { useTheme } from '@soundor/components/theme';
import { Text, View, useViewSize } from '@soundor/react';
import { useState } from 'react';
const filterTypes = [
'Bell',
'Low Shelf',
'High Shelf',
'Low Cut',
'High Cut',
'Notch',
'Band Pass',
] as const;
const slopes = ['6 dB/oct', '12 dB/oct', '24 dB/oct', '48 dB/oct'];
const names = ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B'];
// Five octaves of root notes: longer than the editor, so the submenu scrolls.
const roots = Array.from(
{ length: 60 },
(_, i) => `${names[i % 12]}${Math.floor(i / 12) + 1}`,
);
// Rest the pointer on a row with a chevron, or press →, to open its submenu.
// On the way to it, the pointer may cross the rows below.
export default function Demo() {
const theme = useTheme();
// As wide as the editor, less the frame's padding: Output at its right edge.
const view = useViewSize();
const [type, setType] = useState<string>('Bell');
const [slope, setSlope] = useState('12 dB/oct');
const [root, setRoot] = useState('C3');
const [last, setLast] = useState('Nothing yet');
const run = (name: string) => () => setLast(name);
return (
<View style={{ gap: 14, width: Math.max(0, view.width - 40) }}>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
gap: 12,
}}
>
<View style={{ flexDirection: 'row', gap: 12 }}>
{/* One level: the band menu's filter types. */}
<DropdownMenu>
<DropdownMenu.Trigger>
<Button>Band</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<Menu.Item onPress={run('Reset band')}>Reset</Menu.Item>
<Menu.Sub>
<Menu.SubTrigger>Filter type</Menu.SubTrigger>
<Menu.SubContent>
{filterTypes.map((name) => (
<Menu.Item
key={name}
checked={name === type}
onPress={() => setType(name)}
>
{name}
</Menu.Item>
))}
</Menu.SubContent>
</Menu.Sub>
<Menu.Item onPress={run('Solo band')}>Solo</Menu.Item>
<Menu.Divider />
<Menu.Item onPress={run('Delete band')}>Delete</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>
{/* Two levels: a submenu with one of its own. */}
<DropdownMenu>
<DropdownMenu.Trigger>
<Button>Filter</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<Menu.Sub>
<Menu.SubTrigger>Shape</Menu.SubTrigger>
<Menu.SubContent>
<Menu.Item onPress={run('Analog')}>Analog</Menu.Item>
<Menu.Item onPress={run('Digital')}>Digital</Menu.Item>
<Menu.Sub>
<Menu.SubTrigger>Slope</Menu.SubTrigger>
<Menu.SubContent>
{slopes.map((name) => (
<Menu.Item
key={name}
checked={name === slope}
onPress={() => setSlope(name)}
>
{name}
</Menu.Item>
))}
</Menu.SubContent>
</Menu.Sub>
</Menu.SubContent>
</Menu.Sub>
{/* A long submenu: held to the editor's height, it scrolls. */}
<Menu.Sub>
<Menu.SubTrigger>Key tracking root</Menu.SubTrigger>
<Menu.SubContent>
{roots.map((name) => (
<Menu.Item
key={name}
checked={name === root}
onPress={() => setRoot(name)}
>
{name}
</Menu.Item>
))}
</Menu.SubContent>
</Menu.Sub>
<Menu.Item onPress={run('Bypass filter')}>Bypass</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>
</View>
{/* At the editor's right edge: the submenu flips to the left. */}
<DropdownMenu>
<DropdownMenu.Trigger>
<Button>Output</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content align="end">
<Menu.Sub>
<Menu.SubTrigger>Channels</Menu.SubTrigger>
<Menu.SubContent>
<Menu.Item onPress={run('Stereo')}>Stereo</Menu.Item>
<Menu.Item onPress={run('Mid/Side')}>Mid/Side</Menu.Item>
<Menu.Item onPress={run('Mono')}>Mono</Menu.Item>
</Menu.SubContent>
</Menu.Sub>
<Menu.Item onPress={run('Mute output')}>Mute</Menu.Item>
</DropdownMenu.Content>
</DropdownMenu>
</View>
<Text
style={{
fontFamily: theme.font.mono,
fontSize: 11,
color: theme.color.textSecondary,
}}
>
Filter: {type}, {slope}, root {root} · Last action: {last}
</Text>
</View>
);
}submenu.tsx, running in the web runtime.Source<DropdownMenu.Content>
<Menu.Item onPress={reset}>Reset</Menu.Item>
<Menu.Sub>
<Menu.SubTrigger>Filter type</Menu.SubTrigger>
<Menu.SubContent>
{filterTypes.map((type) => (
<Menu.Item
key={type}
checked={type === band.type}
onPress={() => setType(type)}
>
{type}
</Menu.Item>
))}
</Menu.SubContent>
</Menu.Sub>
<Menu.Item onPress={remove}>Delete</Menu.Item>
</DropdownMenu.Content>The submenu opens when the pointer rests on its trigger for openDelay (150 ms), at once on a press, and with focus on its first row from →, Enter or Space. The trigger stays highlighted while its submenu is open, wherever the pointer is. A submenu leaves room for a tick before its rows when one of them has checked, as a menu does: its parent's rows are not affected.
It opens on the right of the menu, its first row level with the trigger. Short of room there, it flips to the left; it is shifted to stay inside the editor, and a long one scrolls. side="left" on Menu.Sub prefers the left (for an editor that reads right to left): the chevron points left, and ← opens the submenu and → closes it.
On the way to a submenu
A pointer heading from the trigger to its submenu usually crosses other rows on the way. Those rows do not take over while the pointer is still aiming at the submenu: it stays open as long as the pointer moves inside the triangle from its last point on the trigger to the submenu's near edge, keeps making progress towards it, and does not move back. Then a quick move to the submenu reaches it, even diagonally across the rows below.
The row under the pointer is highlighted, and the submenu closes, when the pointer:
- leaves the triangle, as a slow move down the menu does,
- moves back, away from the submenu, by more than
aimTolerance(4 px), or - stops, or creeps, for
restDelay(100 ms).
The same holds for a submenu flipped to the left. Leaving the menu altogether, onto the editor, keeps the submenu open.
Prop on Menu.Sub | Default | What it is |
|---|---|---|
openDelay | 150 ms | How long the pointer rests on the trigger before the submenu opens. |
restDelay | 100 ms | How long the pointer may stop or creep on its way before the row under it takes over. |
aimTolerance | 4 px | Slack around the submenu's near edge, and how far the pointer may move back before it counts. |
Closing
Choosing a row in a submenu closes the whole menu, then runs it, as in a flat menu. ← or Esc closes only the innermost submenu and gives focus back to its trigger; Tab closes them all. A press outside the menus closes them all, and a press on another row of the parent closes the submenu and does what that row does. Moving to another row of the parent with the keyboard closes the submenu too.
The trigger is a menuitem whose expanded state says whether its submenu is open. The submenu is a menu named after its trigger, unless Menu.SubContent has an accessibilityLabel. Soundor has no "has popup" or "labelled by" relation to give them: assistive technology learns of the submenu from expanded and its name. Theme the trigger's chevron with the Menu slot chevron, and the trigger while its submenu is open with itemOpen; a submenu's surface and rows take the menu's own slots and styles.
Triggers
DropdownMenu.Trigger adds no view of its own. It gives its one child the menu, and the child keeps its look, its label and its disabled state: a disabled Button opens nothing. The trigger's accessibilityState gains expanded while the menu is open, and the menu is named after the trigger's label unless the content has an accessibilityLabel.
Any element that takes these props, and passes them on to the node that is pressed, can be a trigger: a Button, a Pressable, or a component of yours.
| Prop | What the menu does with it |
|---|---|
ref | Places the menu beside that node. Your own ref on the child still gets it. |
onPress | Opens and closes the menu, after your own onPress on the child has run. |
accessibilityState | Adds expanded to your own state. |
accessibilityLabel | Names the menu; a child whose children is a string is named by it otherwise. |
The menu opens on the trigger's primary press: a click, Enter, Space or a screen reader's activate. Give the trigger a control whose press only opens the menu: a Toggle or a Knob as a trigger would do its own thing and open the menu as well.
Placement
DropdownMenu.Content takes side (below by default), align (the trigger's start, centre or end) and offset, for where the menu goes when there is room. It flips to the other side when there isn't, follows the trigger if it moves while open, and stays inside the editor. anchor places it beside something else: a node ref, a point { x, y }, or a rectangle { x, y, width, height } in logical view coordinates. Pass a new point or rectangle when it moves, or a stable { getRegion: () => region } to follow an anchor whose position changes outside React. The reader runs while the menu is open and may return null if the anchor is temporarily unavailable.
Opening it yourself
Pass open and onOpenChange to DropdownMenu to open and close it in code, as from a shortcut. It is placed beside the trigger even before the trigger was ever pressed. Opened this way, focus goes to the first item, as from the keyboard.
const [open, setOpen] = useState(false);
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenu.Trigger>
<Button>Preset</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<PresetActions preset={preset} />
</DropdownMenu.Content>
</DropdownMenu>;Keyboard and accessibility
| Input | Does |
|---|---|
Enter or Space on the trigger | Open, with focus on the first item |
| Click on the trigger | Open, with focus on the menu and nothing highlighted |
↑ / ↓, Home / End | Move between items, skipping disabled ones and dividers |
| A letter | The next item whose label starts with it |
Enter, Space or a click | Run the item |
Esc, Tab or a click outside | Close, and give focus back to the trigger |
→, Enter or Space on a Menu.SubTrigger | Open its submenu, with focus on its first row |
← or Esc in a submenu | Close it, and give focus back to its trigger |
The menu is a menu, its items are menuitems, and a Menu.Label is a header. In a menu opened from a Modal, Esc closes the menu and leaves the modal open. Soundor has no "has popup" state to give the trigger: assistive technology learns of the menu from expanded.
Props
DropdownMenu
DropdownMenu.Content
Menu.Item
Menu.Sub
Menu.SubTrigger
Menu.SubContent
Popup anchors
The anchor prop on DropdownMenu.Content, Popover.Content and Tooltip accepts a PopupAnchor:
type PopupRegion = Readonly<
| { x: number; y: number }
| { x: number; y: number; width: number; height: number }
>;
type PopupAnchor =
| RefObject<UiNode | null>
| PopupRegion
| { readonly getRegion: () => PopupRegion | null };Points and rectangles use logical view coordinates. Use a node ref or a stable getRegion reader to follow an anchor that moves outside React renders. Return null when the region is unavailable.