Skip to content

Components

Dropdown menu

A button that opens a list of actions and links, defined as data and fully usable by keyboard.

Live demo

This is the Tailwind wrapper from the recipe below, running. Pick an item and the result appears underneath.

Nothing selected yet

Usage

Items are plain objects: an action has onselect, a link has href. Selecting an item closes the menu and returns focus to the trigger, then calls onselect, so a handler that opens a dialog can take focus.

<script>
	import { DropdownMenu } from '@frostycle/rime/dropdown-menu';

	const items = [
		{ label: 'Edit', onselect: edit },
		{ label: 'View docs', href: '/docs' },
		{ type: 'separator' },
		{
			type: 'group',
			label: 'Danger zone',
			items: [{ label: 'Delete', onselect: remove, disabled: !canDelete }]
		}
	];
</script>

<DropdownMenu {items} label="Actions" />

A MenuItem is { label, onselect?, href?, target?, rel?, disabled? }. An item with href is a link, and a disabled link renders without href.

Props

PropTypeNotes
items(T | MenuSeparator | MenuGroup<T>)[]Required. T extends MenuItem, so extra fields such as an icon flow into the item snippet.
labelstringText of the default trigger. Default Menu.
placementPlacementFrom @floating-ui/dom. Default bottom-start.
disabledbooleanDisables the trigger, and closes the menu if it is open.
transitionbooleanKeeps the popup mounted through its CSS transition, as on the date pickers. Default false.
classes{ trigger, popup, item, separator, group, groupLabel }Classes for default-rendered elements.

Snippets

All optional. Spread each props onto your element.

  • trigger

    props, open

  • popup

    props, content

    Render content for the default body.

  • item

    item, props, disabled

    Spread props onto a <button>, or an <a> when item.href is set.

  • separator

    props

  • groupLabel

    label, props

Keyboard

Disabled items are skipped. Hovering an enabled item moves focus to it, so pointer and keyboard share one current item.

Enter, Space, Down on the trigger
Open and focus the first enabled item
Up on the trigger
Open and focus the last enabled item
Down, Up
Move between enabled items, wrapping at the ends
Home, End
Go to the first or last enabled item
Printable characters
Jump to the next enabled item starting with what was typed
Enter, Space
Activate the current item
Escape
Close and return focus to the trigger
Tab
Close and let focus move on

State attributes

Style state with data-* attributes rather than props. They are present or absent, with no value.

ElementAttributes
Triggerdata-open, data-disabled, data-hover, data-focus, data-active
Popupdata-open, plus data-closed, data-enter and data-leave with transition
Itemdata-disabled, data-hover, data-focus, data-active

Tailwind recipe

The library ships no Tailwind classes, so there is nothing to configure or override. Copy this wrapper into your project and own it. It needs Tailwind CSS v4, whose bare data-hover: variants it uses; in v3 write data-[hover]:.

import type { MenuClasses } from '@frostycle/rime/dropdown-menu';

export const menuClasses: MenuClasses = {
	trigger:
		'rounded-md border border-gray-300 bg-white px-3 py-2 text-sm text-gray-900 data-hover:bg-gray-50 data-focus:outline-2 data-focus:outline-offset-2 data-focus:outline-blue-600 data-open:border-blue-600 data-disabled:opacity-50 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-100 dark:data-hover:bg-gray-700',
	popup:
		'min-w-48 rounded-lg border border-gray-200 bg-white p-1.5 text-sm text-gray-900 shadow-lg transition duration-150 ease-out outline-none data-closed:-translate-y-1 data-closed:opacity-0 motion-reduce:transition-none dark:border-gray-700 dark:bg-gray-800 dark:text-gray-100',
	item: 'block w-full rounded-md px-2.5 py-1.5 text-start outline-none data-hover:bg-gray-100 data-focus:bg-blue-600 data-focus:text-white data-disabled:opacity-50 dark:data-hover:bg-gray-700 dark:data-focus:bg-blue-500',
	separator: 'mx-1 my-1.5 h-px bg-gray-200 dark:bg-gray-700',
	groupLabel: 'px-2.5 pt-1.5 pb-1 text-xs font-semibold text-gray-500 dark:text-gray-400'
};
<script lang="ts" generics="T extends MenuItem">
	import { DropdownMenu, type DropdownMenuProps, type MenuItem } from '@frostycle/rime/dropdown-menu';
	import { menuClasses } from './menu-classes.js';

	let props: DropdownMenuProps<T> = $props();
</script>

<DropdownMenu transition classes={menuClasses} {...props} />
<AppMenu {items} label="Actions" />

Behaviour to plan for

  • Clicks inside the menu bubble like any native click. If a parent such as a table row handles clicks, stop them on a wrapper around the menu.
  • Disabling an open menu closes it and disables the trigger, so focus falls to <body>. Move focus yourself if you disable a menu while it is in use.
  • Items have no ids, so a recreated list matches the focused item by label. If labels repeat and the list also reorders, focus can land on the other item with that label.