Playground

Context menu

A menu of actions opened by right click or long press on an area.

  • Built on native ARIA (shared positioning)
  • 6 parts, named exports
  • Opens with right click, long press or the context-menu key (Shift+F10).
  • Arrow keys move, Enter or Space selects, Escape closes and returns focus.
  • Every action must also exist elsewhere in the UI for people who cannot open it.
  • Styles in one file: popover.css

Import

import '@monoline/css';
import {ContextMenu} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add context-menu

Usage

import {ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuLabel, ContextMenuSeparator, ContextMenuTrigger} from '@monoline/react';

<ContextMenu>
  <ContextMenuTrigger><div style={{display: 'grid', placeItems: 'center', height: '120px', border: '1px dashed var(--rule)'}}>Right click here</div></ContextMenuTrigger>
  <ContextMenuContent aria-label="File actions"><ContextMenuLabel>File</ContextMenuLabel><ContextMenuItem>Rename</ContextMenuItem><ContextMenuItem>Duplicate</ContextMenuItem><ContextMenuSeparator /><ContextMenuItem tone="danger">Delete</ContextMenuItem></ContextMenuContent>
</ContextMenu>

Anatomy

Import the parts you need and nest them like this.

import {ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem, ContextMenuLabel, ContextMenuSeparator} from '@monoline/react';

<ContextMenu>
  <ContextMenuTrigger />
  <ContextMenuContent />
  <ContextMenuItem />
  <ContextMenuLabel />
  <ContextMenuSeparator />
</ContextMenu>

Examples

Right click the box

Props

ContextMenuItemProps

PropTypeDefaultDescription
tone"danger" | "default"'default'`danger` highlights in the danger colour.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-disabled]popover.css
[data-highlighted]popover.css
[data-variant="danger"]popover.css

CSS classes

ClassStylesheet
.menu-contentpopover.css
.menu-indicatorpopover.css
.menu-itempopover.css
.menu-item-checkpopover.css
.menu-labelpopover.css
.popoverpopover.css
.danger17-dropdown-menu.css
.dropdown17-dropdown-menu.css
.menu17-dropdown-menu.css
.menu-end17-dropdown-menu.css
.menu-item17-dropdown-menu.css
.menu-sep17-dropdown-menu.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.menu-content {
  border-color: var(--accent);
}
.menu-content[data-disabled] {
  background: var(--wash);
}

Accessibility

  • Opens with right click, long press or the context-menu key (Shift+F10).
  • Arrow keys move, Enter or Space selects, Escape closes and returns focus.
  • Every action must also exist elsewhere in the UI for people who cannot open it.

Keyboard

KeysAction
Arrow keysMove
Enter / SpaceSelects
EscapeCloses and returns focus