Dropdown Menu
A list of actions that opens from a button.
Reka UI Dropdown Menu
<script setup lang="ts">
import * as DropdownMenu from '@/components/ui/dropdown-menu';
import { ref } from 'vue';
const showStatusBar = ref(true);
const lastAction = ref('none');
</script>
<template>
<DropdownMenu.Root>
<DropdownMenu.Trigger>Open menu</DropdownMenu.Trigger>
<DropdownMenu.Content style="--dui-dropdown-menu-min-width: 14rem">
<DropdownMenu.Label>My account</DropdownMenu.Label>
<DropdownMenu.Group>
<DropdownMenu.Item @select="lastAction = 'profile'">
Profile <DropdownMenu.Shortcut>⇧⌘P</DropdownMenu.Shortcut>
</DropdownMenu.Item>
<DropdownMenu.Item @select="lastAction = 'billing'">Billing</DropdownMenu.Item>
<DropdownMenu.Item disabled>Team</DropdownMenu.Item>
<DropdownMenu.Item @select="lastAction = 'settings'">Settings</DropdownMenu.Item>
</DropdownMenu.Group>
<DropdownMenu.Separator />
<DropdownMenu.CheckboxItem v-model="showStatusBar">Status bar</DropdownMenu.CheckboxItem>
<DropdownMenu.Separator />
<DropdownMenu.Item variant="destructive" @select="lastAction = 'logout'">
Log out
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
<p>Last action: {{ lastAction }}. Status bar: {{ showStatusBar ? 'on' : 'off' }}.</p>
</template>
<style scoped>
p {
margin: var(--dui-space-3) 0 0;
color: var(--dui-color-muted-foreground);
font-size: var(--dui-text-sm);
}
</style>
Installation
npx distill-ui add dropdown-menuIt also copies Button, which Dropdown Menu uses.
It installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy Button too, which it uses.
- Copy these files into your project:
<script setup lang="ts">
import { DropdownMenuRoot } from 'reka-ui';
import { ref, toRef } from 'vue';
import { provideDropdownMenu } from './context.js';
import type { Placement } from './placement.js';
// Use `v-model:open` to control it from outside.
const { placement = 'bottom-start' } = defineProps<{ placement?: Placement }>();
const open = defineModel<boolean>('open', { default: false });
provideDropdownMenu({ open, placement: toRef(() => placement), focusLast: ref(false) });
</script>
<template>
<!-- Not modal, like the Svelte version: the page stays usable and readable while it is open. -->
<DropdownMenuRoot v-model:open="open" :modal="false">
<slot />
</DropdownMenuRoot>
</template>
<script setup lang="ts">
import { DropdownMenuCheckboxItem } from 'reka-ui';
// Use `v-model` for checked. Stays open on select, so several options can be toggled in a row.
const { disabled = false } = defineProps<{ disabled?: boolean }>();
const checked = defineModel<boolean>({ default: false });
</script>
<template>
<DropdownMenuCheckboxItem
v-model="checked"
:disabled="disabled"
as-child
@select="(event: Event) => event.preventDefault()"
>
<div class="item">
<svg
class="check"
viewBox="0 0 24 24"
aria-hidden="true"
:data-checked="checked || undefined"
>
<path d="M20 6 9 17l-5-5" />
</svg>
<slot />
</div>
</DropdownMenuCheckboxItem>
</template>
<style scoped>
.item {
position: relative;
display: flex;
align-items: center;
gap: var(--dui-space-2);
padding: var(--dui-space-1-5) var(--dui-space-2) var(--dui-space-1-5) var(--dui-space-8);
border-radius: var(--dui-radius-sm);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
cursor: default;
outline: none;
user-select: none;
&:focus {
background: var(--dui-color-accent);
color: var(--dui-color-accent-foreground);
}
&[data-disabled] {
pointer-events: none;
opacity: 0.5;
}
}
.check {
position: absolute;
left: var(--dui-space-2);
width: 1rem;
height: 1rem;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
visibility: hidden;
&[data-checked] {
visibility: visible;
}
}
</style>
<script setup lang="ts">
import { DropdownMenuContent } from 'reka-ui';
import { computed, useTemplateRef } from 'vue';
import { injectDropdownMenu } from './context.js';
import { toSideAlign } from './placement.js';
defineOptions({ inheritAttrs: false });
const menu = injectDropdownMenu();
const position = computed(() => toSideAlign(menu.placement.value));
const el = useTemplateRef<HTMLElement>('el');
// Opened with ArrowUp on the trigger: start on the last item that can be picked.
function onOpenAutoFocus(event: Event) {
if (!menu.focusLast.value) return;
menu.focusLast.value = false;
event.preventDefault();
const items = el.value?.querySelectorAll<HTMLElement>('[role^="menuitem"]:not([data-disabled])');
items?.[items.length - 1]?.focus();
}
</script>
<template>
<!-- No portal: Reka positions it with position: fixed, so it stays inside your styles. -->
<DropdownMenuContent
:side="position.side"
:align="position.align"
:side-offset="4"
as-child
@open-auto-focus="onOpenAutoFocus"
>
<div ref="el" v-bind="$attrs" class="content"><slot /></div>
</DropdownMenuContent>
</template>
<!--
Options you can set from a parent or on the dropdown menu content itself:
--dui-dropdown-menu-min-width, --dui-dropdown-menu-radius, --dui-dropdown-menu-bg, --dui-dropdown-menu-fg
-->
<style scoped>
.content {
min-width: var(--dui-dropdown-menu-min-width, 8rem);
max-height: var(--reka-dropdown-menu-content-available-height);
padding: var(--dui-space-1);
overflow-x: hidden;
overflow-y: auto;
border-radius: var(--dui-dropdown-menu-radius, var(--dui-radius-md));
background: var(--dui-dropdown-menu-bg, var(--dui-color-popover));
color: var(--dui-dropdown-menu-fg, var(--dui-color-popover-foreground));
box-shadow:
0 0 0 1px color-mix(in oklch, var(--dui-color-foreground) 10%, transparent),
var(--dui-shadow-md);
outline: none;
/* Reka copies this onto the positioning wrapper it puts around the menu. */
z-index: var(--dui-z-dropdown);
/* Reka waits for animationend before removing it, so this uses the shared keyframes. */
&[data-state='open'] {
animation: distill-zoom-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-zoom-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
</style>
<script setup lang="ts">
import { DropdownMenuGroup } from 'reka-ui';
</script>
<template>
<DropdownMenuGroup as-child>
<div class="group"><slot /></div>
</DropdownMenuGroup>
</template>
<style scoped>
.group {
display: block;
}
</style>
<script setup lang="ts">
import { DropdownMenuItem } from 'reka-ui';
// Listen with `@select`. It closes the menu after, unless you call event.preventDefault().
const {
disabled = false,
variant = 'default',
inset = false
} = defineProps<{
disabled?: boolean;
variant?: 'default' | 'destructive';
/** Indent to line up with items that have a check mark or icon. */
inset?: boolean;
}>();
</script>
<template>
<DropdownMenuItem :disabled="disabled" as-child>
<div class="item" :data-variant="variant" :data-inset="inset || undefined"><slot /></div>
</DropdownMenuItem>
</template>
<style scoped>
.item {
position: relative;
display: flex;
align-items: center;
gap: var(--dui-space-2);
padding: var(--dui-space-1-5) var(--dui-space-2);
border-radius: var(--dui-radius-sm);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
cursor: default;
outline: none;
user-select: none;
&:focus {
background: var(--dui-color-accent);
color: var(--dui-color-accent-foreground);
}
&[data-disabled] {
pointer-events: none;
opacity: 0.5;
}
&[data-inset] {
padding-inline-start: var(--dui-space-8);
}
& :deep(svg) {
flex-shrink: 0;
width: 1rem;
height: 1rem;
pointer-events: none;
}
}
.item[data-variant='destructive'] {
color: var(--dui-color-destructive-text);
&:focus {
background: color-mix(in oklch, var(--dui-color-destructive) 10%, transparent);
color: var(--dui-color-destructive-text);
}
}
</style>
<script setup lang="ts">
import { DropdownMenuLabel } from 'reka-ui';
</script>
<template>
<DropdownMenuLabel as-child>
<div class="label"><slot /></div>
</DropdownMenuLabel>
</template>
<style scoped>
.label {
padding: var(--dui-space-1-5) var(--dui-space-2);
color: var(--dui-color-muted-foreground);
font-size: var(--dui-text-xs);
line-height: var(--dui-text-xs-line-height);
font-weight: var(--dui-weight-medium);
}
</style>
<script setup lang="ts">
import { DropdownMenuSeparator } from 'reka-ui';
</script>
<template>
<DropdownMenuSeparator as-child>
<div class="separator"></div>
</DropdownMenuSeparator>
</template>
<style scoped>
.separator {
height: 1px;
margin: var(--dui-space-1) calc(var(--dui-space-1) * -1);
background: var(--dui-color-border);
}
</style>
<template>
<span class="shortcut"><slot /></span>
</template>
<style scoped>
.shortcut {
margin-inline-start: auto;
/* Pulled toward the foreground so it passes AA contrast on the highlighted item. */
color: color-mix(in oklch, var(--dui-color-muted-foreground) 70%, var(--dui-color-foreground));
font-size: var(--dui-text-xs);
letter-spacing: 0.1em;
}
</style>
<script setup lang="ts">
import { DropdownMenuTrigger } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
import { injectDropdownMenu } from './context.js';
// Renders our Button, so it takes the same variant and size props.
const { variant = 'outline', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
const menu = injectDropdownMenu();
// The menu button pattern also opens on ArrowUp, landing on the last item.
function onKeydown(event: KeyboardEvent) {
if (event.key !== 'ArrowUp' || menu.open.value) return;
event.preventDefault();
menu.focusLast.value = true;
menu.open.value = true;
}
</script>
<template>
<DropdownMenuTrigger as-child>
<Button :variant="variant" :size="size" @keydown="onKeydown"><slot /></Button>
</DropdownMenuTrigger>
</template>
import { inject, provide, type Ref } from 'vue';
import type { PlacementRef } from './placement.js';
// The root shares its open state and placement with the parts. `focusLast` lets the trigger
// open the menu on the last item (ArrowUp), which Reka doesn't do on its own.
type DropdownMenuContext = { open: Ref<boolean>; placement: PlacementRef; focusLast: Ref<boolean> };
const key = Symbol('distill-dropdown-menu');
export const provideDropdownMenu = (ctx: DropdownMenuContext) => provide(key, ctx);
export const injectDropdownMenu = () => inject<DropdownMenuContext>(key)!;
export { default as Root } from './DropdownMenu.vue';
export { default as Trigger } from './DropdownMenuTrigger.vue';
export { default as Content } from './DropdownMenuContent.vue';
export { default as Item } from './DropdownMenuItem.vue';
export { default as CheckboxItem } from './DropdownMenuCheckboxItem.vue';
export { default as Label } from './DropdownMenuLabel.vue';
export { default as Separator } from './DropdownMenuSeparator.vue';
export { default as Group } from './DropdownMenuGroup.vue';
export { default as Shortcut } from './DropdownMenuShortcut.vue';
export type { Placement } from './placement.js';
import type { Ref } from 'vue';
// Placement is written like Floating UI's ('bottom', 'top-start'…), the same as the
// Svelte version. Reka takes it as a side and an alignment, so this splits it.
export type Placement =
'top' | 'right' | 'bottom' | 'left' | `${'top' | 'right' | 'bottom' | 'left'}-${'start' | 'end'}`;
export function toSideAlign(placement: Placement) {
const [side, align = 'center'] = placement.split('-') as [
'top' | 'right' | 'bottom' | 'left',
('start' | 'end' | 'center')?
];
return { side, align };
}
export type PlacementRef = Readonly<Ref<Placement>>;
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-dropdown-menu-min-width | 8rem |
--dui-dropdown-menu-radius | var(--dui-radius-md) |
--dui-dropdown-menu-bg | var(--dui-color-popover) |
--dui-dropdown-menu-fg | var(--dui-color-popover-foreground) |