ContextMenu
A menu for a control, at the pointer or from the keyboard.
ContextMenu gives a control a menu. The menu opens on the platform's secondary press (a right-click, or a Control-click on a Mac) or a long touch, at the pointer. From the keyboard, Shift+F10 or the context menu key opens it beside the focused control, and a screen reader's "show menu" action does the same. Its items are the same Menu.Items as a DropdownMenu's, and so are its submenus.
import {
ContextMenu,
ControlGroup,
Fader,
Knob,
ParameterMenuItems,
Slider,
TextField,
type HostProps,
type RangeParameter,
} from '@soundor/components';
import { Modal, View } from '@soundor/react';
import { useState, type ReactElement } from 'react';
import { parameters } from 'soundor:parameters';
// Right-click a control (or focus it and press Shift+F10) for its menu.
export default function Demo() {
const [editing, setEditing] = useState<RangeParameter | null>(null);
const [error, setError] = useState<string | undefined>();
// One component for each control's menu: the trigger and its items.
const withMenu = (
parameter: RangeParameter,
control: ReactElement<HostProps>,
) => (
<ContextMenu>
<ContextMenu.Trigger>{control}</ContextMenu.Trigger>
<ContextMenu.Content>
<ParameterMenuItems
parameter={parameter}
onEnterValue={() => setEditing(parameter)}
/>
</ContextMenu.Content>
</ContextMenu>
);
return (
<>
<ControlGroup title="Delay" direction="row" dividers>
{withMenu(
parameters.feedback,
<Knob parameter={parameters.feedback} />,
)}
{withMenu(parameters.mix, <Slider parameter={parameters.mix} />)}
{withMenu(
parameters.gain,
<Fader parameter={parameters.gain} length={110} />,
)}
</ControlGroup>
<Modal
visible={editing !== null}
onRequestClose={() => setEditing(null)}
accessibilityLabel="Enter a value"
backdropStyle={{ backgroundColor: 'rgba(0, 0, 0, 0.25)' }}
>
<View
style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}
>
{editing && (
<View
style={{
padding: 16,
borderRadius: 8,
backgroundColor: '#f3f4ee',
}}
>
<TextField
label={editing.info.label}
defaultValue={editing.format(editing.get())}
error={error}
description="Enter to apply, Esc to cancel"
onChange={() => setError(undefined)}
onSubmit={(text) => {
const value = editing.parse(text);
if (value === null) return setError('Not a value');
editing.beginGesture({ history: true });
editing.set(value);
editing.endGesture();
setEditing(null);
}}
/>
</View>
)}
</View>
</Modal>
</>
);
}context-menu.tsx, running in the web runtime.Sourceimport { ContextMenu, Knob, Menu } from '@soundor/components';
import { parameters } from 'soundor:parameters';
<ContextMenu>
<ContextMenu.Trigger>
<Knob parameter={parameters.gain} />
</ContextMenu.Trigger>
<ContextMenu.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>
</ContextMenu.Content>
</ContextMenu>;The content renders as the menu opens, so the items show the state at that moment: disabled={!canPaste} is read then. A menu whose content renders no item at all closes as it opens.
With parameter controls
ParameterMenuItems renders a parameter's usual items:
- Reset to default: one host gesture and one undo step, as a double-click is. It is disabled when the parameter is already at its default.
- Enter value…: only with
onEnterValue. It calls your function to open a field of your own, as the demo'sModaldoes. - Copy value and Paste value: the parameter's own
format()goes to the system clipboard, and pasted text is read back with itsparse(). Text that isn't a value changes nothing.clipboard={false}leaves them out.
import {
ContextMenu,
Knob,
Menu,
ParameterMenuItems,
} from '@soundor/components';
<ContextMenu>
<ContextMenu.Trigger>
<Knob parameter={parameters.gain} />
</ContextMenu.Trigger>
<ContextMenu.Content>
<ParameterMenuItems parameter={parameters.gain} />
<Menu.Divider />
<Menu.Item onPress={learn}>MIDI learn</Menu.Item>
</ContextMenu.Content>
</ContextMenu>;Put items of your own before or after them. A read-only parameter's items are disabled, except Copy. labels gives them in your language.
Parameter context menus aren't added to the controls automatically: give the ones that should have one a ContextMenu. A component of yours keeps that short:
function WithMenu({
parameter,
children,
}: {
parameter: RangeParameter;
children: ReactElement<HostProps>;
}) {
return (
<ContextMenu>
<ContextMenu.Trigger>{children}</ContextMenu.Trigger>
<ContextMenu.Content>
<ParameterMenuItems parameter={parameter} />
</ContextMenu.Content>
</ContextMenu>
);
}The trigger keeps what it is
A context menu is a second way into a control, so ContextMenu.Trigger leaves the first one alone. It adds no view: its child lays out exactly as it would without it. The child keeps its role, its name, its value and state, its own actions, its focus, and its primary pointer and keys. A Knob stays a slider (adjustable), with the show menu action offered after its own increment, decrement, set value and reset.
Knob, Slider, Fader, XYPad and Plot markers act on the primary button only, so a secondary press starts no drag and opens no gesture. A secondary press during a primary drag opens nothing. Double-click reset and parameter bindings work as before. The menu's actions start host gestures only when they change a value.
The child is one element that takes a ref and accessibilityActions: any control of this library, or a View. Its own ref, actions and onAccessibilityAction keep working. A ContextMenu inside another's trigger takes the press, the keys and the action for its own control: the outer one opens only from the rest of its child.
For a group of controls, give the trigger a View around them. A secondary press, a long touch and the keys reach it from any control inside. A screen reader's action, though, is offered by the control it is on, and a plain View is not one: where each control should offer "show menu", give each its own ContextMenu.
<ContextMenu>
<ContextMenu.Trigger>
<View style={{ flexDirection: 'row', gap: 12 }}>
<Knob parameter={mix} />
<Knob parameter={tone} />
</View>
</ContextMenu.Trigger>
<ContextMenu.Content>
<ParameterMenuItems parameter={mix} />
</ContextMenu.Content>
</ContextMenu>disabled on the ContextMenu opens no menu and offers no "show menu" action.
Submenus
A context menu takes the same submenus as a DropdownMenu: a Menu.Sub with a Menu.SubTrigger, the row that opens it, and a Menu.SubContent with its rows. Use one for a choice that would make the menu long, such as an EQ band's filter type:
import { ContextMenu, Menu } from '@soundor/components';
<ContextMenu>
<ContextMenu.Trigger>
<BandHandle band={band} />
</ContextMenu.Trigger>
<ContextMenu.Content accessibilityLabel={`Band ${band.index}`}>
<Menu.Item onPress={() => band.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={() => band.setType(type)}
>
{type}
</Menu.Item>
))}
</Menu.SubContent>
</Menu.Sub>
<Menu.Divider />
<Menu.Item onPress={() => band.remove()}>Delete</Menu.Item>
</ContextMenu.Content>
</ContextMenu>;The submenu behaves as it does in a DropdownMenu:
- Opening: the pointer resting on the trigger opens it (after 150 ms), a press opens it at once, and
→,EnterorSpaceopen it with focus on its first row. - On the way there: the pointer may cross the rows below the trigger while it heads for the submenu; the submenu stays open while the pointer is aiming at it, and the row under the pointer takes over when it isn't.
- Placement: beside the menu, its first row level with the trigger. A menu opened at the pointer near the editor's right edge has its submenus flip to the left; they stay inside the editor, and a long one scrolls.
- Closing: choosing a row closes the whole menu, then runs it.
←orEsccloses only the innermost submenu and gives focus back to its trigger.Tabor a click outside closes them all, and focus goes back to the control.
Menu.Sub takes openDelay, restDelay and aimTolerance to tune the timing, and side="left" to prefer the left in an editor that reads right to left.
Keyboard and accessibility
| Input | Does |
|---|---|
| Secondary press | Open at the pointer, with focus on the menu |
| A long touch | Open at the touch, with focus on the menu |
Shift+F10, or the context menu key | Open beside the focused control, with focus on the first item |
| A screen reader's "show menu" | Open beside the control, with focus on the first item |
↑ / ↓, Home / End, a letter | Move between items |
Enter, Space or a click | Run the item |
Esc, Tab or a click outside | Close, and give focus back to the control |
→, 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 |
A Menu.SubTrigger is a menuitem whose expanded state says whether its submenu is open, and the submenu is a menu named after its trigger. A submenu's Esc closes only that submenu, so the menu and the control's focus are kept.
Engine support
ContextMenu opens on Soundor's semantic contextmenu event, which the engine recognises on every view and dispatches as one cancellable event. What each platform sends is the engine's:
- Secondary press: the secondary button going down, or a Control-click on macOS, with its position in the view. The web runtime's own browser menu is kept away.
- Clipboard:
soundor:ui'sclipboard. Copy and Paste are only offered where the runtime has one. A clipboard the platform refuses, such as a browser that denies permission to read it, makes them do nothing rather than fail. - Long touch: the platform's long press, which is also the engine's
longpressevent. A touch that moves first, or is cancelled, is the control's drag. - "Show menu": the engine's predefined
showContextMenuaccessibility action, added after the control's own. It is AXShowMenu on macOS,ShowContextMenuin UI Automation,popupin AT-SPI, and an action in VoiceOver's Actions rotor. - Context menu key:
Shift+F10and the context menu key, where the platform reports it. - A menu is drawn inside the editor, as every Soundor overlay is, and is held within the editor's bounds. It can't extend past the plugin window as a system menu can.