Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (6.1 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
labelstring"Options"
items
property only
{ value, label }[][]
Events
EventWhen
itemselectAn item was picked; detail is its value.
Slots
SlotFor
labelThe 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour