Skip to content

Type to search the docs, API, and blog.

↑↓ navigate↵ openesc close
  • Zazz 0.14 alpha

    API

    Menu

    A popover menu of actions, opened by click or hover.

    Installation

    Source
    pnpm dlx zazz-ui add menu
    npx zazz-ui add menu
    yarn dlx zazz-ui add menu
    bunx zazz-ui add menu

    Also adds fields, kbd, button, popover, which it depends on. Run zazz-ui init first if the project has no zazz/ folder yet.

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Paint the right theme (background, form controls, scrollbars) before CSS loads -->
    <meta name="color-scheme" content="light dark">
    <!-- Zazz styles: base layers, then fields, kbd, button, popover, menu in cascade order -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_layers.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_variables.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_shadows.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_breakpoints.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_reset.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_typography.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_view-transitions.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/fields/fields.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/kbd/kbd.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/button/button.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/popover/popover.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/menu/menu.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-sm.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-md.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-lg.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-2xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-disabled.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-active.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-visible.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-within.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-hover.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-checked.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-current.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-open.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-stuck.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-starting.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-sm.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-md.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-lg.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-2xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-disabled.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-active.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-visible.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-within.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-hover.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-checked.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-current.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-open.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-stuck.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-starting.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-flow.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-grid.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-spacing.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-margin.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sizing.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-typography.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-color.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-effects.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-box.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-pseudo.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_defaults.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sr.css">
    <!-- Import map: bare specifiers in Zazz scripts resolve to pinned, SRI-checked files -->
    <script type="importmap">
    {
      "imports": {
        "signal-polyfill": "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js"
      },
      "integrity": {
        "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js": "sha384-0OqM7OXGT6Oz1z98u/geiFn/FUyz4iWexkXHCvbIGXRRDXl6bp6IKc0j78AQ6DEK"
      }
    }
    </script>
    <!-- Polyfill: Interest Invokers (interestfor) — native in Chromium only -->
    <script type="module" src="https://cdn.jsdelivr.net/npm/invokers@2.2.2/dist/esm/production/interest.js" integrity="sha384-hR2BVNtsS7fIIMwOm+f7MY0JZfuByGhQQfevCBB6evFATKzBsTw0JzH/RxD94z2T" crossorigin="anonymous"></script>
    <!-- Zazz behavior: side-effect modules by tag; the rest via module imports -->
    <script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/dialog-lifecycle.js"></script>
    <script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/menu/menu.js"></script>
    <!-- Theme: apply the persisted (or preferred) scheme before first paint -->
    <script>
      (() => {
        try {
          // A stored choice pins the scheme with data-theme; with no choice the
          // page follows the system scheme.
          const storedTheme = localStorage.getItem("theme");
          if (storedTheme === "dark" || storedTheme === "light") {
            document.documentElement.dataset.theme = storedTheme;
          }
        } catch {
          /* no storage access: follow the system scheme */
        }
      })();
    </script>

    A <head> that loads the base layers and menu with its dependencies from jsDelivr, pinned to 0.14.2. To load the whole kit in two requests, see the CDN install.

    Examples

    Default

    <div
      style="
        --display: flex;
        --gap: 4;
      "
    >
      <ui-menu>
        <button data-ui="button" type="button" popovertarget="menu-example-1">Bottom / start</button>
        <div
          id="menu-example-1"
          data-menu-slot="popover"
          data-popover-side="bottom"
          data-popover-align="start"
          popover="auto"
        >
          <menu>
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 1
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 1</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 2</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 3</a
              >
            </li>
            <hr data-ui="separator" style="--my: 2" />
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 2
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 4</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 5</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 6</a
              >
            </li>
          </menu>
        </div>
      </ui-menu>
    
      <ui-menu>
        <button data-ui="button" type="button" popovertarget="menu-example-2">Bottom / center</button>
        <div
          id="menu-example-2"
          data-menu-slot="popover"
          data-popover-side="bottom"
          data-popover-align="center"
          popover="auto"
        >
          <menu>
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 1
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 1</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 2</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 3</a
              >
            </li>
            <hr data-ui="separator" style="--my: 2" />
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 2
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 4</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 5</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 6</a
              >
            </li>
          </menu>
        </div>
      </ui-menu>
    
      <ui-menu>
        <button data-ui="button" type="button" popovertarget="menu-example-3">Bottom / end</button>
        <div
          id="menu-example-3"
          data-menu-slot="popover"
          data-popover-side="bottom"
          data-popover-align="end"
          popover="auto"
        >
          <menu>
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 1
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 1</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 2</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 3</a
              >
            </li>
            <hr data-ui="separator" style="--my: 2" />
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Group 2
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 4</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 5</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Link 6</a
              >
            </li>
          </menu>
        </div>
      </ui-menu>
    </div>
    /**
     * menu.css: Menu (ui-menu | [data-ui~="menu"]; slot data-menu-slot="popover")
     *
     * @layer      variables, components
     * @requires   _layers.css, _variables.css, _properties.css (--_*-resolved), popover.css, button.css, fields.css
     * @uses       popovertarget + [popover]: inherits Popover API from popover.css
     * @uses       interestfor: optional hover/focus open on the same trigger
     *             (Interest Invokers, Chromium 142+; polyfill via invokers)
     * @uses       anchor-name, anchor-scope, position-anchor: tether panel to trigger
     * @uses       data-popover-side / data-popover-align: maps to --ui-popover-position-* in popover.css
     * @tokens     --ui-menu-* (@layer variables)
     * @consumedby select.css (--ui-menu-button-radius), autocomplete.css, combobox.css, command.css (--ui-menu-shadow)
     * @see        menu.ts: optional arrow-key navigation; both forms stay no-JS
     */
    
    @layer variables {
      :root {
        --ui-menu-button-radius: var(--radius-sm);
        --ui-menu-button-corner-shape: var(--radius-corner-shape);
        /* a scale number or a length */
        --ui-menu-min-w: calc(var(--spacing) * 52);
        --ui-menu-gap: var(--ui-field-option-gap);
        --ui-menu-shadow: var(--shadow-sm);
      }
    }
    
    @layer ui.components {
      /* ===========================================================================
      MENU: trigger + anchored popover of actions/links
      - Inherits --ui-popover-* tokens and [popover] surface/positioning from popover.css.
      - Only menu-specific wiring lives here: anchor scope/name and nested buttons.
      - The native <menu> element stays inside the panel as the list; the root names
        the trigger + panel assembly.
      - Explicit anchor-name covers both popovertarget (click) and interestfor
        (hover/focus) triggers; implicit interest anchors are not polyfilled.
      =========================================================================== */
    
      :where(ui-menu, [data-ui~="menu"]) {
        anchor-scope: --ui-menu-trigger;
        display: grid;
        --_gap-len: var(--_gap-resolved, var(--ui-menu-gap));
        --_gap-num: var(--_gap-resolved, var(--ui-menu-gap));
        gap: calc(var(--_gap-len) + var(--_gap-num) * var(--spacing));
      }
    
      :where(ui-menu, [data-ui~="menu"]) > :where([popovertarget], [interestfor]) {
        anchor-name: --ui-menu-trigger;
      }
    
      :where(ui-menu, [data-ui~="menu"]) [data-menu-slot~="popover"] {
        position-anchor: --ui-menu-trigger;
        --_min-w-len: var(--_min-w-resolved, var(--ui-menu-min-w));
        --_min-w-num: var(--_min-w-resolved, var(--ui-menu-min-w));
        min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
        box-shadow: var(--_shadow-resolved, var(--ui-menu-shadow));
      }
    
      :where(ui-menu, [data-ui~="menu"]) [data-menu-slot~="popover"] [data-ui~="button"] {
        --ui-button-radius: var(--ui-menu-button-radius);
        --ui-button-corner-shape: var(--ui-menu-button-corner-shape);
      }
    }
    // primitives/menu/menu.js
    "use strict";
    /**
     * @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
     * @description Light-DOM custom element that augments the CSS-only menu
     * pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
     * arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
     * without JavaScript: the Popover API provides open/close, light dismiss,
     * and focus return on its own.
     *
     * Keyboard behavior:
     * - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
     *   first / last item.
     * - ArrowDown / ArrowUp inside the open panel move focus between items,
     *   wrapping around and skipping disabled items.
     * - Home / End jump to the first / last item.
     * - Escape and light dismiss are native Popover API behavior (no code here).
     *
     * The menu keeps the honest disclosure posture: items are plain links and
     * buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
     * yourself only if every item is an action and you implement the rest of the
     * pattern (typeahead, close-on-activate).
     *
     * @example
     * <ui-menu>
     *   <button data-ui="button" type="button" popovertarget="m1">Open</button>
     *   <div id="m1" data-menu-slot="popover" popover="auto">
     *     <menu>
     *       <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
     *     </menu>
     *   </div>
     * </ui-menu>
     */
    import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
    class UiMenu extends ZazzElement {
        setup(signal) {
            this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
        }
        /**
         * @description The menu's own panel: a direct child so nested menus keep
         * their panels to themselves.
         *
         * @returns The panel element, or null when the markup is incomplete.
         */
        #panel() {
            const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
            return panel instanceof HTMLElement ? panel : null;
        }
        /**
         * @description Focusable items inside the panel, in DOM order.
         *
         * @param panel - The menu panel.
         * @returns Enabled links and buttons the arrow keys move between.
         */
        #items(panel) {
            return Array.from(panel.querySelectorAll("a[href], button"))
                .filter((node) => node instanceof HTMLElement)
                .filter((item) => !item.hasAttribute("disabled") &&
                item.getAttribute("aria-disabled") !== "true" &&
                item.closest('ui-menu, [data-ui~="menu"]') === this);
        }
        /**
         * @description Routes arrow-key, Home, and End presses: opens the panel from
         * the trigger, or moves focus between items inside the open panel.
         *
         * @param event - The keydown event.
         */
        #onKeydown(event) {
            if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
                return;
            const target = event.target;
            if (!(target instanceof HTMLElement))
                return;
            // Ignore keys that belong to a nested ui-menu
            if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
                return;
            const panel = this.#panel();
            if (!panel)
                return;
            const isTrigger = target.parentElement === this &&
                (target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
            const open = panel.matches(":popover-open");
            if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
                event.preventDefault();
                panel.showPopover();
                const items = this.#items(panel);
                items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
                return;
            }
            if (!open || !panel.contains(target))
                return;
            const items = this.#items(panel);
            if (items.length === 0)
                return;
            const index = items.indexOf(target);
            let nextIndex;
            switch (event.key) {
                case "ArrowDown":
                    nextIndex = index === -1 ? 0 : (index + 1) % items.length;
                    break;
                case "ArrowUp":
                    nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
                    break;
                case "Home":
                    nextIndex = 0;
                    break;
                case "End":
                    nextIndex = items.length - 1;
                    break;
                default:
                    return;
            }
            event.preventDefault();
            items[nextIndex].focus();
        }
    }
    defineZazzElement("ui-menu", UiMenu);
    export { UiMenu };

    Interest

    <div
      style="
        --display: flex;
        --gap: 4;
      "
    >
      <ui-menu>
        <button
          data-ui="button"
          type="button"
          popovertarget="menu-interest-example-1"
          interestfor="menu-interest-example-1"
        >
          Hover or click
        </button>
        <div
          id="menu-interest-example-1"
          data-menu-slot="popover"
          data-popover-side="bottom"
          data-popover-align="start"
          popover="auto"
        >
          <menu>
            <li
              style="
                --display: inline-flex;
                --p: 2;
                --font-size: var(--font-size-eyebrow);
                --font-weight: strong;
                --leading: var(--leading-eyebrow);
                --tracking: var(--tracking-eyebrow);
                --text-transform: uppercase;
                --text: var(--color-muted-foreground);
              "
            >
              Account
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Profile</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Settings</a
              >
            </li>
            <hr data-ui="separator" style="--my: 2" />
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Sign out</a
              >
            </li>
          </menu>
        </div>
      </ui-menu>
    
      <ui-menu>
        <button
          data-ui="button"
          type="button"
          data-button-variant="outline"
          popovertarget="menu-interest-example-2"
          interestfor="menu-interest-example-2"
        >
          Resources
        </button>
        <div
          id="menu-interest-example-2"
          data-menu-slot="popover"
          data-popover-side="bottom"
          data-popover-align="start"
          popover="auto"
        >
          <menu>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Documentation</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Changelog</a
              >
            </li>
            <li>
              <a href="#" data-ui="button" data-button-variant="ghost" style="--justify: start"
                >Support</a
              >
            </li>
          </menu>
        </div>
      </ui-menu>
    </div>
    /**
     * menu.css: Menu (ui-menu | [data-ui~="menu"]; slot data-menu-slot="popover")
     *
     * @layer      variables, components
     * @requires   _layers.css, _variables.css, _properties.css (--_*-resolved), popover.css, button.css, fields.css
     * @uses       popovertarget + [popover]: inherits Popover API from popover.css
     * @uses       interestfor: optional hover/focus open on the same trigger
     *             (Interest Invokers, Chromium 142+; polyfill via invokers)
     * @uses       anchor-name, anchor-scope, position-anchor: tether panel to trigger
     * @uses       data-popover-side / data-popover-align: maps to --ui-popover-position-* in popover.css
     * @tokens     --ui-menu-* (@layer variables)
     * @consumedby select.css (--ui-menu-button-radius), autocomplete.css, combobox.css, command.css (--ui-menu-shadow)
     * @see        menu.ts: optional arrow-key navigation; both forms stay no-JS
     */
    
    @layer variables {
      :root {
        --ui-menu-button-radius: var(--radius-sm);
        --ui-menu-button-corner-shape: var(--radius-corner-shape);
        /* a scale number or a length */
        --ui-menu-min-w: calc(var(--spacing) * 52);
        --ui-menu-gap: var(--ui-field-option-gap);
        --ui-menu-shadow: var(--shadow-sm);
      }
    }
    
    @layer ui.components {
      /* ===========================================================================
      MENU: trigger + anchored popover of actions/links
      - Inherits --ui-popover-* tokens and [popover] surface/positioning from popover.css.
      - Only menu-specific wiring lives here: anchor scope/name and nested buttons.
      - The native <menu> element stays inside the panel as the list; the root names
        the trigger + panel assembly.
      - Explicit anchor-name covers both popovertarget (click) and interestfor
        (hover/focus) triggers; implicit interest anchors are not polyfilled.
      =========================================================================== */
    
      :where(ui-menu, [data-ui~="menu"]) {
        anchor-scope: --ui-menu-trigger;
        display: grid;
        --_gap-len: var(--_gap-resolved, var(--ui-menu-gap));
        --_gap-num: var(--_gap-resolved, var(--ui-menu-gap));
        gap: calc(var(--_gap-len) + var(--_gap-num) * var(--spacing));
      }
    
      :where(ui-menu, [data-ui~="menu"]) > :where([popovertarget], [interestfor]) {
        anchor-name: --ui-menu-trigger;
      }
    
      :where(ui-menu, [data-ui~="menu"]) [data-menu-slot~="popover"] {
        position-anchor: --ui-menu-trigger;
        --_min-w-len: var(--_min-w-resolved, var(--ui-menu-min-w));
        --_min-w-num: var(--_min-w-resolved, var(--ui-menu-min-w));
        min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
        box-shadow: var(--_shadow-resolved, var(--ui-menu-shadow));
      }
    
      :where(ui-menu, [data-ui~="menu"]) [data-menu-slot~="popover"] [data-ui~="button"] {
        --ui-button-radius: var(--ui-menu-button-radius);
        --ui-button-corner-shape: var(--ui-menu-button-corner-shape);
      }
    }
    // primitives/menu/menu.js
    "use strict";
    /**
     * @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
     * @description Light-DOM custom element that augments the CSS-only menu
     * pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
     * arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
     * without JavaScript: the Popover API provides open/close, light dismiss,
     * and focus return on its own.
     *
     * Keyboard behavior:
     * - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
     *   first / last item.
     * - ArrowDown / ArrowUp inside the open panel move focus between items,
     *   wrapping around and skipping disabled items.
     * - Home / End jump to the first / last item.
     * - Escape and light dismiss are native Popover API behavior (no code here).
     *
     * The menu keeps the honest disclosure posture: items are plain links and
     * buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
     * yourself only if every item is an action and you implement the rest of the
     * pattern (typeahead, close-on-activate).
     *
     * @example
     * <ui-menu>
     *   <button data-ui="button" type="button" popovertarget="m1">Open</button>
     *   <div id="m1" data-menu-slot="popover" popover="auto">
     *     <menu>
     *       <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
     *     </menu>
     *   </div>
     * </ui-menu>
     */
    import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
    class UiMenu extends ZazzElement {
        setup(signal) {
            this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
        }
        /**
         * @description The menu's own panel: a direct child so nested menus keep
         * their panels to themselves.
         *
         * @returns The panel element, or null when the markup is incomplete.
         */
        #panel() {
            const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
            return panel instanceof HTMLElement ? panel : null;
        }
        /**
         * @description Focusable items inside the panel, in DOM order.
         *
         * @param panel - The menu panel.
         * @returns Enabled links and buttons the arrow keys move between.
         */
        #items(panel) {
            return Array.from(panel.querySelectorAll("a[href], button"))
                .filter((node) => node instanceof HTMLElement)
                .filter((item) => !item.hasAttribute("disabled") &&
                item.getAttribute("aria-disabled") !== "true" &&
                item.closest('ui-menu, [data-ui~="menu"]') === this);
        }
        /**
         * @description Routes arrow-key, Home, and End presses: opens the panel from
         * the trigger, or moves focus between items inside the open panel.
         *
         * @param event - The keydown event.
         */
        #onKeydown(event) {
            if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
                return;
            const target = event.target;
            if (!(target instanceof HTMLElement))
                return;
            // Ignore keys that belong to a nested ui-menu
            if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
                return;
            const panel = this.#panel();
            if (!panel)
                return;
            const isTrigger = target.parentElement === this &&
                (target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
            const open = panel.matches(":popover-open");
            if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
                event.preventDefault();
                panel.showPopover();
                const items = this.#items(panel);
                items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
                return;
            }
            if (!open || !panel.contains(target))
                return;
            const items = this.#items(panel);
            if (items.length === 0)
                return;
            const index = items.indexOf(target);
            let nextIndex;
            switch (event.key) {
                case "ArrowDown":
                    nextIndex = index === -1 ? 0 : (index + 1) % items.length;
                    break;
                case "ArrowUp":
                    nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
                    break;
                case "Home":
                    nextIndex = 0;
                    break;
                case "End":
                    nextIndex = items.length - 1;
                    break;
                default:
                    return;
            }
            event.preventDefault();
            items[nextIndex].focus();
        }
    }
    defineZazzElement("ui-menu", UiMenu);
    export { UiMenu };

    Behavior

    primitives/menu/menu.js

    <ui-menu>: HTML web component for keyboard-enhanced menus. Light-DOM custom element that augments the CSS-only menu pattern (trigger + anchored [data-menu-slot~="popover"] panel) with arrow-key navigation. The attribute form [data-ui~="menu"] stays fully functional without JavaScript: the Popover API provides open/close, light dismiss, and focus return on its own.

    Keyboard behavior:

    • ArrowDown / ArrowUp on the closed trigger open the panel and focus the first / last item.
    • ArrowDown / ArrowUp inside the open panel move focus between items, wrapping around and skipping disabled items.
    • Home / End jump to the first / last item.
    • Escape and light dismiss are native Popover API behavior (no code here).

    The menu keeps the honest disclosure posture: items are plain links and buttons, and no role="menu" is claimed. Add the full ARIA menu contract yourself only if every item is an action and you implement the rest of the pattern (typeahead, close-on-activate).

    Attributes

    menu attributes
    AttributeKindValuesNotes
    data-ui="menu"Identityfree valueIdentity token in data-ui.
    <ui-menu>Tag formfree valueTag form; styled and, when scripted, registered under this name.
    data-menu-slotSlotpopoverSlot: a part inside the primitive.

    Requires: popover, button.

    Hooks

    Set a hook on :root, on a subtree, or inline on one element. A state hook (--…--hover) themes that state only; the primitive keeps its other states.

    menu hooks
    HookDefaultState
    --ui-menu-button-corner-shapevar(--radius-corner-shape)—
    --ui-menu-button-radiusvar(--radius-sm)—
    --ui-menu-gapvar(--ui-field-option-gap)—
    --ui-menu-min-wcalc(var(--spacing) * 52)—
    --ui-menu-shadowvar(--shadow-sm)—

    API