Components / Selection

Menu15

A list of actions behind a button: edit, duplicate, delete. Rows deal themselves out one by one in Sticker, Comic highlights them in a slanted caption box, and Arcade boots the list up like an old CRT.

Menu · PlaygroundLive
placement
JSX
<MenuTrigger>
  <Button>Actions</Button>
  <Menu>
    <MenuItem id="edit">Edit</MenuItem>
    <MenuItem id="duplicate">Duplicate</MenuItem>
    <MenuItem id="delete" variant="danger">
      Delete
    </MenuItem>
  </Menu>
</MenuTrigger>

Usage

TSX
import { Button, Menu, MenuItem, MenuTrigger } from "plonk-ui";

export function ProjectActions() {
  return (
    <MenuTrigger>
      <Button>Actions</Button>
      <Menu onAction={(id) => run(id)}>
        <MenuItem id="edit">Edit</MenuItem>
        <MenuItem id="delete" variant="danger">
          Delete
        </MenuItem>
      </Menu>
    </MenuTrigger>
  );
}

MenuTrigger connects the button to the menu. Choosing an item calls onAction with its id and closes the menu; an item can also have its own onAction.

Icons, shortcuts and sections

Items can have an icon, a description and a shortcut. Group them with MenuSection and MenuSeparator, and mark destructive ones with variant="danger".

Menu · ActionsLive
Last action: nothing yet
import { useState } from "react";
import {
  Button,
  Menu,
  MenuItem,
  MenuSection,
  MenuSeparator,
  MenuTrigger,
} from "plonk-ui";

const Icon = ({ d }: { d: string }) => (
  <svg
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth={2.5}
    strokeLinecap="round"
    strokeLinejoin="round"
  >
    <path d={d} />
  </svg>
);

export default function Actions() {
  const [last, setLast] = useState("nothing yet");

  return (
    <div className="flex flex-col items-center gap-3">
      <MenuTrigger>
        <Button variant="neutral">Project</Button>
        <Menu onAction={(key) => setLast(String(key))}>
          <MenuSection title="Edit">
            <MenuItem
              id="rename"
              icon={<Icon d="M4 20h4L18.5 9.5a2.1 2.1 0 0 0-4-4L4 16z" />}
              shortcut="⌘E"
            >
              Rename
            </MenuItem>
            <MenuItem
              id="duplicate"
              icon={<Icon d="M8 8h12v12H8zM4 16V4h12" />}
              shortcut="⌘D"
              description="Copies settings too"
            >
              Duplicate
            </MenuItem>
            <MenuItem
              id="archive"
              icon={<Icon d="M4 7h16v13H4zM2 3h20v4H2zM10 12h4" />}
              isDisabled
            >
              Archive
            </MenuItem>
          </MenuSection>
          <MenuSeparator />
          <MenuItem
            id="delete"
            variant="danger"
            icon={
              <Icon d="M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3" />
            }
            shortcut="⌫"
          >
            Delete
          </MenuItem>
        </Menu>
      </MenuTrigger>
      <span className="font-bold">Last action: {last}</span>
    </div>
  );
}

A shortcut is only a hint. It doesn't register the key: wire it up yourself, for example with the command palette.

Selection

With selectionMode, items become checkable: single for a choice like the view, multiple for toggles like visible columns. The selection is a Set of ids.

Menu · SelectionLive
grid · name, owner
import { useState } from "react";
import { Button, Menu, MenuItem, MenuTrigger } from "plonk-ui";

// A menu reports its selection as a Set of item ids ("all" only with select all).
const ids = (keys: Iterable<unknown> | "all") =>
  keys === "all" ? [] : [...keys].map(String);

export default function SelectionMenu() {
  const [view, setView] = useState(["grid"]);
  const [columns, setColumns] = useState(["name", "owner"]);

  return (
    <div className="flex flex-col items-center gap-3">
      <div className="flex flex-wrap justify-center gap-3">
        <MenuTrigger>
          <Button variant="neutral">View</Button>
          <Menu
            selectionMode="single"
            selectedKeys={view}
            onSelectionChange={(keys) => setView(ids(keys))}
          >
            <MenuItem id="list">List</MenuItem>
            <MenuItem id="grid">Grid</MenuItem>
            <MenuItem id="board">Board</MenuItem>
          </Menu>
        </MenuTrigger>
        <MenuTrigger>
          <Button variant="neutral">Columns</Button>
          <Menu
            selectionMode="multiple"
            selectedKeys={columns}
            onSelectionChange={(keys) => setColumns(ids(keys))}
          >
            <MenuItem id="name">Name</MenuItem>
            <MenuItem id="owner">Owner</MenuItem>
            <MenuItem id="updated">Last updated</MenuItem>
          </Menu>
        </MenuTrigger>
      </div>
      <span className="font-bold">
        {view.join("")} · {columns.join(", ") || "no columns"}
      </span>
    </div>
  );
}

For picking a value in a form, use a Select instead: it has a label and submits with the form.

Personalities

Every list opens from the edge facing its trigger and never tilts, so it can't cover the button that opened it.

  • Sticker: rows slide in one after another; the highlight is a solid fill.
  • Retro: appears instantly, like a system menu, with inverted highlights.
  • Comic: a halftone list whose rows drop in "pat pat"; the highlight is a yellow caption box leaning forward, with a red arrow.
  • Arcade: opens like a CRT, from a dot to a line to the full list, and types the rows in; a blinking pixel arrow marks the highlight and a star marks selected items.

Highlight hooks

The highlighted row reads its look from four variables. Set them on .plonk-item to restyle every list (menus, selects, combo boxes):

CSS
[data-plonk-personality="sticker"] .plonk-item {
  --plonk-item-on-bg: var(--plonk-color-accent);
  --plonk-item-on-ink: var(--plonk-color-on-accent);
  --plonk-item-on-shadow: inset 0 0 0 2px var(--plonk-color-ink);
  --plonk-item-on-transform: none;
}

Variables instead of a :hover rule because the highlight has several states (hover, keyboard focus, closing), and the variables cover all of them at once.

Accessibility

  • Enter, Space or ↓ on the trigger opens the menu with the first item focused; ↑ opens it at the last.
  • Arrow keys move, Home and End jump, and typing a letter jumps to the matching item.
  • Esc closes the menu and returns focus to the trigger.
  • Icons are hidden from screen readers; descriptions and shortcuts are read with the label.
  • The highlight follows the keyboard and the pointer, never only focus, so it doesn't flash when the menu closes.

API

PropTypeDefaultDescription
placement"left" | "right" | "bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right top" | "right bottom" | "end" | "end top" | "end bottom""bottom start"Where the menu opens relative to its trigger.
classNamestring—Extra classes for the list.
onAction(key: Key, value: T) => void—Called with the item's id when an item is chosen. The menu then closes.
selectionMode"none" | "multiple" | "single"—single or multiple makes the items checkable, like a view switcher or a column picker.
selectedKeysIterable<Key> | "all"—Controlled selection: the checked items' ids.
defaultSelectedKeysIterable<Key> | "all"—Checked items on first render, when uncontrolled.
onSelectionChange(keys: Selection) => void—Called with the new selection, a Set of ids ("all" with select all).
disabledKeysIterable<Key>—Ids of items that can't be chosen.
onClose() => void—Called when the menu closes.
PropTypeDefaultDescription
variant"danger" | "default""default"danger for destructive actions like delete.
classNamestring—Extra classes for the row.
iconReactNode—Icon before the label. Hidden from screen readers.
descriptionReactNode—Smaller text under the label, read with it.
shortcutstring—Keyboard shortcut shown on the right, like ⌘E. It's a hint only: wire the shortcut yourself.
idKey—Unique key, used for selection and in callbacks.
onAction() => void—Called when this item is chosen.
isDisabledboolean—Can't be focused or chosen.
textValuestring—Plain text for type-to-search. Set it when the label isn't a string.
PropTypeDefaultDescription
titleReactNode—Group heading, read by screen readers.

MenuSeparator draws a line between groups and takes no props.

PropTypeDefaultDescription
isOpenboolean—Controlled open state.
defaultOpenboolean—Open on first render, when uncontrolled.
onOpenChange(isOpen: boolean) => void—Called when the menu opens or closes.
trigger"contextMenu" | "press" | "longPress""press"longPress opens it on a long press, for secondary actions.