Skip to Content
CodeUI

DropdownMenu

@codeui/solid v0.3.0

A menu of actions that opens from a trigger button.

When to use

Use a dropdown menu to offer a short list of actions behind one button, such as the export formats for a snippet. Each item runs a command. The menu closes after the user picks one.

When to choose another component

  • Select picks a value that stays visible in the trigger. A menu runs an action and keeps no value.
  • Popover holds content that is not a list of actions, such as a form.
  • Command menu finds any action by name across the app.
  • Inkstone does not use a menu to manage things the user is building, such as steps. Use inline controls for those.

Live demo

Last export: none
dropdown-menu/basic.tsx
import { createSignal } from "solid-js";
import {
	Button,
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuTrigger,
} from "@codeui/solid";

export default function DropdownMenuBasic() {
	const [last, setLast] = createSignal("none");
	return (
		<>
			<DropdownMenu>
				<DropdownMenuTrigger as={Button} variant="secondary">
					Export
				</DropdownMenuTrigger>
				<DropdownMenuPortal>
					<DropdownMenuContent>
						<DropdownMenuItem rightSlot="⌘S" onSelect={() => setLast("PNG")}>
							Save as PNG
						</DropdownMenuItem>
						<DropdownMenuItem rightSlot="⇧⌘S" onSelect={() => setLast("SVG")}>
							Save as SVG
						</DropdownMenuItem>
						<DropdownMenuItem onSelect={() => setLast("JPEG")}>Save as JPEG</DropdownMenuItem>
					</DropdownMenuContent>
				</DropdownMenuPortal>
			</DropdownMenu>
			<span>Last export: {last()}</span>
		</>
	);
}

DropdownMenu holds the open state. DropdownMenuTrigger opens it. Put DropdownMenuContent inside DropdownMenuPortal, and one DropdownMenuItem per action. Handle a choice with onSelect on the item.

Anatomy

Tap a part to outline it in the demo.

  1. 01DropdownMenuContentdropdownmenu-contentclasses.class
  2. 02DropdownMenuItemdropdownmenu-itemclasses.class
ExportRendersclasses keydata-cuiRole
DropdownMenuNothing. Holds the open statenonenonenone
DropdownMenuTriggerbuttonnonenoneOpens the menu
DropdownMenuPortalNothing. Moves the menu to document.bodynonenonenone
DropdownMenuContentdivnone, use classdropdownmenu-contentrole="menu"
DropdownMenuItemdivnone, use classdropdownmenu-itemrole="menuitem"
DropdownMenuSeparatorhrnonenonerole="separator"
DropdownSubMenuNothing. Holds the submenu statenonenonenone
DropdownSubMenuTriggerdivnonenonerole="menuitem" that opens the submenu
DropdownSubMenuContentdivnonenonerole="menu"

The rightSlot of an item and of a submenu trigger renders in a div without a data-cui attribute.

Variants

Groups

dropdown-menu/groups.tsx
import {
	Button,
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuSeparator,
	DropdownMenuTrigger,
} from "@codeui/solid";

export default function DropdownMenuGroups() {
	return (
		<DropdownMenu>
			<DropdownMenuTrigger as={Button} variant="secondary">
				Snippet
			</DropdownMenuTrigger>
			<DropdownMenuPortal>
				<DropdownMenuContent>
					<DropdownMenuItem rightSlot="F2">Rename</DropdownMenuItem>
					<DropdownMenuItem rightSlot="⌘D">Duplicate</DropdownMenuItem>
					<DropdownMenuSeparator />
					<DropdownMenuItem>Move to folder</DropdownMenuItem>
					<DropdownMenuItem>Copy link</DropdownMenuItem>
					<DropdownMenuSeparator />
					<DropdownMenuItem rightSlot="⌫">Delete</DropdownMenuItem>
				</DropdownMenuContent>
			</DropdownMenuPortal>
		</DropdownMenu>
	);
}

Split related actions with DropdownMenuSeparator. Keep a destructive action, such as Delete, in its own group at the end. Use Group and GroupLabel from @codeui/solid/dropdown-menu/parts to label a set of actions. Add your own styles to the group label or render it as a kit Text.

dropdown-menu/submenu.tsx
import {
	Button,
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuSeparator,
	DropdownMenuTrigger,
	DropdownSubMenu,
	DropdownSubMenuContent,
	DropdownSubMenuTrigger,
} from "@codeui/solid";

export default function DropdownMenuSubmenu() {
	return (
		<DropdownMenu>
			<DropdownMenuTrigger as={Button} variant="secondary">
				Share
			</DropdownMenuTrigger>
			<DropdownMenuPortal>
				<DropdownMenuContent>
					<DropdownMenuItem>Open in new tab</DropdownMenuItem>
					<DropdownMenuSeparator />
					<DropdownSubMenu>
						<DropdownSubMenuTrigger rightSlot="›">Copy</DropdownSubMenuTrigger>
						<DropdownMenuPortal>
							<DropdownSubMenuContent>
								<DropdownMenuItem>Copy image</DropdownMenuItem>
								<DropdownMenuItem>Copy link</DropdownMenuItem>
								<DropdownMenuItem>Copy embed code</DropdownMenuItem>
							</DropdownSubMenuContent>
						</DropdownMenuPortal>
					</DropdownSubMenu>
					<DropdownSubMenu gutter={4}>
						<DropdownSubMenuTrigger rightSlot="›">Post to</DropdownSubMenuTrigger>
						<DropdownMenuPortal>
							<DropdownSubMenuContent>
								<DropdownMenuItem>Mastodon</DropdownMenuItem>
								<DropdownMenuItem>Bluesky</DropdownMenuItem>
							</DropdownSubMenuContent>
						</DropdownMenuPortal>
					</DropdownSubMenu>
				</DropdownMenuContent>
			</DropdownMenuPortal>
		</DropdownMenu>
	);
}

Wrap a DropdownSubMenuTrigger and a portaled DropdownSubMenuContent in DropdownSubMenu. The submenu opens on the right of its trigger. Its gutter defaults to -4, so the submenu overlaps the parent menu's padding and its first item lines up with the trigger. Pass a larger gutter, as in Post to, to leave a gap.

Custom trigger

dropdown-menu/icon-trigger.tsx
import {
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuTrigger,
	IconButton,
	icons,
} from "@codeui/solid";

export default function DropdownMenuIconTrigger() {
	return (
		<DropdownMenu placement="bottom-end">
			<DropdownMenuTrigger as={IconButton} variant="secondary" aria-label="More export options">
				<icons.ChevronDownIcon />
			</DropdownMenuTrigger>
			<DropdownMenuPortal>
				<DropdownMenuContent>
					<DropdownMenuItem>Copy to clipboard</DropdownMenuItem>
					<DropdownMenuItem>Export at 2x</DropdownMenuItem>
					<DropdownMenuItem>Export at 3x</DropdownMenuItem>
				</DropdownMenuContent>
			</DropdownMenuPortal>
		</DropdownMenu>
	);
}

DropdownMenuTrigger is polymorphic. Pass as={Button} or as={IconButton} to use a kit button. An icon button needs an aria-label. Set placement on DropdownMenu to align the menu with the trigger's end edge.

Checkbox and radio items

The kit does not export checkbox or radio items. To show the current choice, put it in the trigger label, as in the controlled example below, or use a Select or a SegmentedControl.

States

Disabled

dropdown-menu/disabled.tsx
import {
	Button,
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuTrigger,
	DropdownSubMenu,
	DropdownSubMenuContent,
	DropdownSubMenuTrigger,
} from "@codeui/solid";

export default function DropdownMenuDisabled() {
	return (
		<DropdownMenu>
			<DropdownMenuTrigger as={Button} variant="secondary">
				Export
			</DropdownMenuTrigger>
			<DropdownMenuPortal>
				<DropdownMenuContent>
					<DropdownMenuItem>Save as PNG</DropdownMenuItem>
					<DropdownMenuItem disabled>Export as video</DropdownMenuItem>
					<DropdownSubMenu>
						<DropdownSubMenuTrigger disabled rightSlot="›">
							Export as GIF
						</DropdownSubMenuTrigger>
						<DropdownMenuPortal>
							<DropdownSubMenuContent>
								<DropdownMenuItem>15 fps</DropdownMenuItem>
							</DropdownSubMenuContent>
						</DropdownMenuPortal>
					</DropdownSubMenu>
				</DropdownMenuContent>
			</DropdownMenuPortal>
		</DropdownMenu>
	);
}

A disabled item or submenu trigger is dimmed, skipped by the arrow keys, and ignores clicks.

Controlled

The menu is closed.
dropdown-menu/controlled.tsx
import { createSignal } from "solid-js";
import {
	Button,
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuPortal,
	DropdownMenuTrigger,
} from "@codeui/solid";

export default function DropdownMenuControlled() {
	const [open, setOpen] = createSignal(false);
	const [format, setFormat] = createSignal("PNG");
	return (
		<>
			<DropdownMenu open={open()} onOpenChange={setOpen}>
				<DropdownMenuTrigger as={Button} variant="secondary">
					Format: {format()}
				</DropdownMenuTrigger>
				<DropdownMenuPortal>
					<DropdownMenuContent>
						<DropdownMenuItem onSelect={() => setFormat("PNG")}>PNG</DropdownMenuItem>
						<DropdownMenuItem onSelect={() => setFormat("SVG")}>SVG</DropdownMenuItem>
						<DropdownMenuItem onSelect={() => setFormat("JPEG")}>JPEG</DropdownMenuItem>
					</DropdownMenuContent>
				</DropdownMenuPortal>
			</DropdownMenu>
			<span>The menu is {open() ? "open" : "closed"}.</span>
		</>
	);
}

Pass open and onOpenChange to DropdownMenu to control the open state.

Ready API

DropdownMenu is a compound component. Each export wraps the matching Kobalte dropdown menu part and takes its props. The root also exports dropdownMenuStyles, the raw style module with the content, subMenuContent, item, separator, and rightSlot classes.

Kobalte's DropdownMenu root with a gutter of 6. It renders no element.

Prop

Type

Kobalte's DropdownMenu.Content. The menu panel. It takes Kobalte's content props, such as onEscapeKeyDown, and merges class after the kit styles.

Kobalte's DropdownMenu.Portal. Portals the menu, or a submenu, to document.body. Takes mount.

Kobalte's DropdownMenu.Item with a rightSlot for a shortcut or a hint.

Prop

Type

Kobalte's DropdownMenu.Trigger. A button with no kit styles. Use as to render a kit button.

Kobalte's DropdownMenu.Sub with a gutter of -4.

Prop

Type

Kobalte's DropdownMenu.SubTrigger, styled like an item, with a rightSlot for an arrow.

Prop

Type

DropdownSubMenuTrigger replaces any class you pass with the kit item class. Style it through a data-* selector or its role instead.

Kobalte's DropdownMenu.SubContent, styled like the menu panel. It also replaces any class you pass.

Kobalte's DropdownMenu.Separator. A line between groups. It also replaces any class you pass.

Styled parts API

Import from @codeui/solid/dropdown-menu/parts to build a menu with a reusable structure. Keep Trigger and Portal under the same Root. Use GroupLabel inside Group to name a set of related items.

import { Button } from "@codeui/solid/button";
import * as M from "@codeui/solid/dropdown-menu/parts";
import { Text } from "@codeui/solid/text";

<M.Root>
	<M.Trigger as={Button} variant="secondary">Export</M.Trigger>
	<M.Portal>
		<M.Content>
			<M.Group>
				<M.GroupLabel as={Text} tone="label">Image formats</M.GroupLabel>
				<M.Item>PNG</M.Item>
				<M.Item>JPEG</M.Item>
			</M.Group>
		</M.Content>
	</M.Portal>
</M.Root>

Use ItemLabel and ItemDescription inside an Item to link a name and supporting text. For a submenu, put SubTrigger and SubContent under the same Sub. Separator divides groups of actions.

Accessibility

Keyboard

KeyAction
Enter, Space, ArrowDownOn the trigger, opens the menu and highlights the first item.
ArrowUpOn the trigger, opens the menu and highlights the last item.
ArrowDown, ArrowUpMoves the highlight to the next or the previous item. Skips disabled items.
Home, EndMoves the highlight to the first or the last item.
A letter keyMoves the highlight to the next item that starts with that letter.
Enter, SpaceRuns the highlighted item and closes the menu. On a submenu trigger, opens the submenu.
ArrowRightOn a submenu trigger, opens the submenu and highlights its first item.
ArrowLeftIn a submenu, closes it and highlights its trigger.
EscapeCloses the innermost open menu. Focus returns to its trigger.
TabCloses the menu and moves focus on.

ARIA

  • The trigger has aria-haspopup="true", aria-expanded, and aria-controls.
  • The content has role="menu" and is labelled by the trigger.
  • Each item has role="menuitem". A disabled item has aria-disabled="true".
  • A submenu trigger has role="menuitem", aria-haspopup="true", and aria-expanded.
  • The separator has role="separator".

Focus

On open, focus moves into the menu and the first item is highlighted. On close, focus returns to the trigger. The menu is modal by default. While it is open, the rest of the page is hidden from assistive technology and does not take pointer events.

SSR notes

The trigger renders on the server. The portal renders nothing on the server, so the menu and its submenus render on the client, and only while they are open. IDs come from Solid's createUniqueId, so aria-controls on the trigger matches after hydration.

Theming hooks

DropdownMenu reads these --cui-* variables. See Tokens for the defaults.

VariableUsed for
--cui-surface-dialogMenu fill
--cui-shadow-dialogMenu shadow, with a 1px inset ring in dark
--cui-border-menu, --cui-border-widthSeparator line
--cui-radius-cardMenu radius (16px)
--cui-radius-navItem radius (10px), concentric with the 6px menu padding
--cui-space-1-5Menu padding
--cui-space-48Menu minimum width (192px), or the trigger width if wider
--cui-space-0-5Gap between items
--cui-space-1-5, --cui-space-2-5Item padding
--cui-font-size-sm, --cui-font-line-height-snugItem type (14px / 1.3)
--cui-space-1-5, --cui-space-pxSeparator margin and height
--cui-space-8Gap before the right slot
--cui-control-height-smItem minimum height (36px)
--cui-text-menuItem text
--cui-surface-menu-hoverHighlighted item fill
--cui-text-mutedRight slot text
--cui-font-weight-normalItem weight
--cui-motion-duration-normal, --cui-motion-duration-fast, --cui-motion-ease-outPop: in from scale(0.96) over 160ms, out to scale(0.98) over 120ms

Menus and submenus pop from the trigger side when a pointer opens them. A submenu opened by hover pops too. Opened from the keyboard (Enter, Space, the arrow keys), a menu gets data-motion="none" and appears in place. With reduced motion, only the fade is left.

Composition examples

  • Nest overlays opens a menu inside a dialog and shows where Escape goes.
  • Select keeps the chosen value in the trigger.
  • Popover holds free content instead of a list of actions.
  • Command menu searches every action by name.

On this page