dropdown-menu
A menu of actions behind a button, on the platform's popover: the browser puts it in the top layer and closes it on Escape or a click outside. Down or Up on the button opens it; arrow keys, Home, End and typing a letter move among the items; Enter or Space picks one, and focus goes back to the button. It's placed below the button by CSS anchor positioning, or by script where that isn't supported. items is [{ value, label, disabled? }].
Try it: events appear here (itemselect).
Source: dropdown-menu.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/dropdown-menu.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/dropdown-menu.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/dropdown-menu.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/dropdown-menu.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/dropdown-menu.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/dropdown-menu.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/dropdown-menu.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/dropdown-menu.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/dropdown-menu.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/dropdown-menu.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/dropdown-menu.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/dropdown-menu.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/dropdown-menu.js"></script>The compiled install writes src/components/zubra/dropdown-menu.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<dropdown-menu label="Actions"></dropdown-menu>
<script type="module">
import './components/zubra/dropdown-menu.js';
const element = document.querySelector('dropdown-menu');
element.items = [
{ value: 'edit', label: 'Edit' },
{ value: 'duplicate', label: 'Duplicate' },
{ value: 'archive', label: 'Archive', disabled: true },
{ value: 'delete', label: 'Delete' },
];
element.addEventListener('itemselect', (event) => console.log('itemselect', event.detail));
</script>import './components/zubra/dropdown-menu.js';
const items = [
{ value: 'edit', label: 'Edit' },
{ value: 'duplicate', label: 'Duplicate' },
{ value: 'archive', label: 'Archive', disabled: true },
{ value: 'delete', label: 'Delete' },
];
export function DropdownMenuExample() {
return (
<dropdown-menu
label="Actions"
items={items}
onitemselect={(event) => console.log('itemselect', event.detail)}
></dropdown-menu>
);
}<script setup>
import './components/zubra/dropdown-menu.js';
const items = [
{ value: 'edit', label: 'Edit' },
{ value: 'duplicate', label: 'Duplicate' },
{ value: 'archive', label: 'Archive', disabled: true },
{ value: 'delete', label: 'Delete' },
];
const onItemselect = (event) => console.log('itemselect', event.detail);
</script>
<template>
<dropdown-menu label="Actions" :items.prop="items" @itemselect="onItemselect"></dropdown-menu>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/dropdown-menu.js';
const items = [
{ value: 'edit', label: 'Edit' },
{ value: 'duplicate', label: 'Duplicate' },
{ value: 'archive', label: 'Archive', disabled: true },
{ value: 'delete', label: 'Delete' },
];
</script>
<dropdown-menu
label="Actions"
{items}
onitemselect={(event) => console.log('itemselect', event.detail)}
></dropdown-menu>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/dropdown-menu.js';
@Component({
selector: 'app-dropdown-menu-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<dropdown-menu
label="Actions"
[items]="items"
(itemselect)="onItemselect($event)"
></dropdown-menu>
`,
})
export class DropdownMenuExample {
items = [
{ value: 'edit', label: 'Edit' },
{ value: 'duplicate', label: 'Duplicate' },
{ value: 'archive', label: 'Archive', disabled: true },
{ value: 'delete', label: 'Delete' },
];
onItemselect(event: Event) {
console.log('itemselect', (event as CustomEvent).detail);
}
}API
| Property | Type | Default |
|---|---|---|
label | string | "Options" |
itemsproperty only | { value, label }[] | [] |
| Event | When |
|---|---|
itemselect | An item was picked; detail is its value. |
| Slot | For |
|---|---|
label | The button's content (defaults to label). |
Size
2.0 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 19.4 KB.