Actions

Dropdown Menu

A menu of actions and links that opens from a button.

Class nameTypeDescription
dropdownplf:dropdownComponentWraps the trigger and the menu
menuplf:menuComponentThe list of items, with dropdown-content
dropdown-contentplf:dropdown-contentPartThe floating panel
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-shortcutplf:menu-shortcutPartA key hint; binds no key
menu-chevronplf:menu-chevronPartThe trigger's chevron; turns while open
menu-item-errorplf:menu-item-errorColorA destructive item

Default

Items hold text and an icon only; other controls in the menu stop it from working.

Disabled

A link item takes aria-disabled="true" instead of disabled.

Placement

The menu flips to the opposite side when the chosen one has no room; a left or right menu that fits on neither side opens below or above.

JavaScript

Dropdown 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-dropdown-menu Marks the container
data-plf-dropdown-menu-trigger Optional: marks the <button> that opens the menu, needed only when another element sits beside the menu, as in a Button Group
data-plf-dropdown-menu-content Optional: marks the menu, otherwise the container’s one direct child with hidden. The trigger names the menu unless it has aria-label or aria-labelledby
data-plf-dropdown-menu-separator Optional: gives another element the separator role; an <hr class="menu-separator"> needs none
data-plf-placement top, right, bottom (default) or left
data-plf-align start, center (default) or end
disabled, aria-disabled="true" An item or trigger that cannot be used
Method Behavior
PLF.DropdownMenu.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.DropdownMenu.getInstance(element) Returns the instance or null
instance.open() Opens at the first enabled item; returns false when canceled, open or disabled
instance.close(reason) Closes and focuses the trigger; reason is api (default), trigger or keyboard. Returns false when canceled or closed
instance.toggle() Opens or closes
instance.trigger, instance.panel The trigger button and the menu
instance.destroy() Closes without events and removes listeners and added attributes
Event When
plf:dropdown-menu:before-open Before opening; cancelable
plf:dropdown-menu:open After opening
plf:dropdown-menu:before-close Before closing; cancelable
plf:dropdown-menu:close After closing

Each event’s detail:

Property Value
instance The Dropdown Menu instance
reason api, trigger or keyboard
trigger The trigger, the chosen item, or null
Key Action
Enter, Space, Down Arrow On the trigger, open the menu at the first item
Up Arrow On the trigger, opens the menu at the last item
Down Arrow, Up Arrow Move to the next or previous item
Home, End Move to the first or last item
A character Moves to the next item starting with it
Enter, Space Choose the focused item
Escape Closes the menu and focuses the trigger
Tab, Shift+Tab Close the menu and move on

React

DropdownMenu 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 bottom
align start, center or end center
open true while the menu is open, 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: trigger (the trigger, a press outside or a chosen item) or keyboard; keep open to stay as it is None

The parts:

Part Element
DropdownMenuTrigger A button that opens the menu and names it; buttonClass() gives it a look
DropdownMenuContent A div with dropdown-content menu, the menu
DropdownMenuItem 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()
DropdownMenuTitle A div with menu-title
DropdownMenuSeparator An hr with menu-separator
DropdownMenuText A span with menu-text
DropdownMenuShortcut A span with menu-shortcut, hidden from assistive technologies

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

Variable Default
--plf-dropdown-menu-width 14rem, capped at the viewport
--plf-dropdown-menu-bg, --plf-dropdown-menu-color Elevated surface and text
--plf-dropdown-menu-border Subtle border; also colors separators
--plf-dropdown-menu-radius Large radius (8px)
--plf-dropdown-menu-item-hover-bg Muted surface; 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