Actions

Context Menu

A menu of actions for one area of the page, opened by right-click, Shift+F10 or a More actions button.

Class nameTypeDescription
context-menuplf:context-menuComponentWraps the surface and the menu
menuplf:menuComponentThe list of items, with context-menu-content
context-menu-triggerplf:context-menu-triggerPartThe surface that opens the menu
context-menu-contentplf:context-menu-contentPartThe floating panel
menu-shortcutplf:menu-shortcutPartA key hint; binds no key
menu-itemplf:menu-itemPartA button or link item
menu-titleplf:menu-titlePartA label above items
menu-separatorplf:menu-separatorPartA divider between items
menu-iconplf:menu-iconPartAn SVG icon before the text; an icon class there doesn't need it
menu-textplf:menu-textPartAn item's text
menu-item-errorplf:menu-item-errorColorA destructive item

Default

Keep the More actions button: touch screens have no right-click.

Workspace preview

A scoped area with a compact action menu. Selected text keeps the browser's menu.

In a list

Each row’s icon-only button needs an aria-label that names the row.

  • Design notes

    Document · Updated today

  • Budget report

    Spreadsheet · Updated yesterday

  • Project archive

    Folder · Rename unavailable

JavaScript

Context Menu needs JavaScript. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-context-menu Marks the wrapper
data-plf-context-menu-trigger Optional: marks the surface, otherwise the wrapper’s direct child without hidden: a div, section, article or li. It gets role="group", and its first heading names it unless it has aria-label or aria-labelledby
data-plf-context-menu-button The More actions <button>, with text or aria-label; authored hidden, shown by the script
data-plf-context-menu-content Optional: marks the panel, otherwise the wrapper’s direct child with hidden. The surface’s first heading names it unless it has aria-label or aria-labelledby. Its direct <button> and <a href> children are the items, and it holds no other controls
data-plf-context-menu-separator Optional: gives another element the separator role; an <hr class="menu-separator"> needs none
data-plf-label On an item, the text typeahead matches
data-plf-placement On the wrapper: top, bottom (default), left or right, for keyboard and button opening
data-plf-align On the wrapper: start, center (default) or end
disabled, aria-disabled="true" An item or the More actions button that cannot be used
Method Behavior
PLF.ContextMenu.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.ContextMenu.getInstance(element) Returns the instance or null
instance.open(point) Opens, at an optional viewport point {x, y}. Returns false when already open, unavailable or canceled
instance.close(reason) Closes; reason is api (the default), trigger, keyboard or pointer. Returns false when already closed or canceled
instance.getState() Returns {open, position}; position is {x, y} or null
instance.destroy() Closes without events and removes listeners and added attributes
Event When
plf:context-menu:before-open Before opening; cancelable
plf:context-menu:open After opening
plf:context-menu:before-close Before closing; cancelable
plf:context-menu:close After closing

Each event’s detail:

Property Value
instance The Context Menu instance
reason api, trigger, keyboard or pointer
trigger The surface, the button or the chosen item, or null
target The surface
position The viewport point it opened at, {x, y}, or null
Key Action
Shift+F10, ContextMenu On the surface or the button, opens the menu
Enter, Space On the button, toggles the menu; on an item, activates it
Down Arrow, Up Arrow On the button, open at the first or last item; in the menu, move to the next or previous item
Home, End Move to the first or last item
Letters Move to the next item that starts with the typed text
Escape Closes the menu and returns focus
Tab, Shift+Tab Close the menu and move to the next or previous control

React

ContextMenu from @pelatform/react renders this menu: it keeps its own state, or the application sets it with open, and each part is a component of its own.

Prop Values Default
placement top, right, bottom or left, for keyboard and button opening bottom
align start, center or end center
open true while the menu is open, beside the More actions button, which the application changes in onOpenChange Its own state
defaultOpen true starts open when the application does not set open false
onOpenChange (open, reason), called when the menu opens or closes: pointer (a right-click, a press outside or scrolling), trigger (the More actions button or a chosen item), keyboard, or api (another Context Menu opening); keep open to stay as it is None

The parts:

Part Element
ContextMenuTrigger A div with context-menu-trigger, the surface: a group its first heading names, which a right-click, Shift+F10 or the Menu key opens the menu from
ContextMenuButton The More actions button; buttonClass() gives it a look
ContextMenuContent A div with context-menu-content menu, the menu
ContextMenuItem A button with menu-item, or a link with href; color="error" marks a destructive item, and onSelect(event) runs when it is chosen, then the menu closes unless it calls event.preventDefault()
ContextMenuTitle A div with menu-title
ContextMenuSeparator An hr with menu-separator
ContextMenuText A span with menu-text
ContextMenuShortcut A span with menu-shortcut, hidden from assistive technologies

The other props are the element’s own, className and ref included.

Variable Default
--plf-context-menu-width 14rem, capped to the viewport
--plf-context-menu-bg, --plf-context-menu-color Elevated surface and text
--plf-context-menu-border --plf-color-border-subtle; also the separator color
--plf-context-menu-radius Large radius (8px)
--plf-context-menu-item-hover-bg --plf-color-surface-muted; --plf-color-danger-subtle on destructive items

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons