Menubar
File, Edit, View, like a desktop app.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add menubarOr install the package
Don’t want the file? Import from
asta-ui/menubarand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { Menubar } from "asta-ui/menubar";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const KEYFRAMES = `@keyframes asta-menubar-in { from { opacity: 0; transform: translateY(-4px) scale(.97) } } @media (prefers-reduced-motion: reduce) { .asta-menubar-panel { animation: none !important } }`; interface Common { label: string; shortcut?: string; disabled?: boolean; } export type MenubarItem = | (Common & { type?: "item"; icon?: ReactNode; onSelect?: () => void }) | (Common & { type: "checkbox"; checked: boolean; onCheckedChange: (checked: boolean) => void }) | (Common & { type: "radio"; checked: boolean; onSelect: () => void }) | { type: "separator" } | { type: "label"; label: string }; export interface MenubarMenu { label: string; items: MenubarItem[]; } export interface MenubarProps { menus: MenubarMenu[]; /** Accessible name for the bar. */ label?: string; className?: string; } type Actionable = Exclude<MenubarItem, { type: "separator" } | { type: "label" }>; const actionable = (it: MenubarItem): it is Actionable => it.type !== "separator" && it.type !== "label"; const enabled = (it: MenubarItem) => actionable(it) && !it.disabled; const Check = () => ( <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.85" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="m5 12.5 4.5 4.5L19 7.5" /> </svg> ); /** * The menu row of a desktop-style app: File, Edit, View. Click or press ↓ to open a menu. While one is open, * pointing at another switches to it at once, and ← → walk between them. Items take shortcuts, check marks and * radio dots. Escape closes and hands focus back, and type-ahead jumps to an item by its first letter. */ export function Menubar({ menus, label = "Menu bar", className }: MenubarProps) { const [open, setOpen] = useState<number | null>(null); const [focus, setFocus] = useState(0); const [hi, setHi] = useState(-1); // Opening from closed animates; switching between open menus does not, like a native menu bar. const [instant, setInstant] = useState(false); const [shift, setShift] = useState(0); const root = useRef<HTMLDivElement>(null); const tops = useRef<(HTMLButtonElement | null)[]>([]); const items = useRef<(HTMLButtonElement | null)[]>([]); const panel = useRef<HTMLDivElement>(null); const first = (m: number, from = -1, dir = 1) => { const list = menus[m].items; for (let k = 1; k <= list.length; k++) { const i = (from + dir * k + list.length * 2) % list.length; if (enabled(list[i])) return i; } return -1; }; const show = (m: number | null, highlight = -1) => { setInstant(open !== null && m !== null); setOpen(m); setHi(highlight); if (m !== null) setFocus(m); }; const close = (refocus = true) => { const m = open; show(null); if (refocus && m !== null) tops.current[m]?.focus(); }; // Keep the open panel inside the viewport on narrow screens. Measured once per open. useLayoutEffect(() => { const el = panel.current; if (!el) return; // Measure without the shift from the last open, or the clamp flips between fitting and overflowing. const r = el.getBoundingClientRect(); const right = r.right - shift; const left = r.left - shift; const over = right - (window.innerWidth - 8); setShift(over > 0 ? -Math.min(over, left - 8) : 0); // eslint-disable-next-line react-hooks/exhaustive-deps -- measure once per open, not when the shift itself changes }, [open]); // Move DOM focus to the highlighted item. useEffect(() => { if (open !== null && hi >= 0) items.current[hi]?.focus(); }, [open, hi]); // Close on a pointer down outside. useEffect(() => { if (open === null) return; const down = (e: PointerEvent) => { if (!root.current?.contains(e.target as Node)) { setOpen(null); setHi(-1); } }; document.addEventListener("pointerdown", down); return () => document.removeEventListener("pointerdown", down); }, [open]); const activate = (it: MenubarItem) => { if (!enabled(it)) return; if (it.type === "checkbox") it.onCheckedChange(!it.checked); else if (it.type === "radio" || it.type === undefined || it.type === "item") it.onSelect?.(); close(); }; const onTopKey = (e: KeyboardEvent, i: number) => { const n = menus.length; const go = (j: number) => { e.preventDefault(); setFocus(j); tops.current[j]?.focus(); if (open !== null) show(j, first(j)); }; if (e.key === "ArrowRight") go((i + 1) % n); else if (e.key === "ArrowLeft") go((i - 1 + n) % n); else if (e.key === "Home") go(0); else if (e.key === "End") go(n - 1); else if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") { e.preventDefault(); show(i, first(i)); } else if (e.key === "ArrowUp") { e.preventDefault(); show(i, first(i, 0, -1)); } else if (e.key === "Escape" && open !== null) { e.preventDefault(); close(); } }; const onMenuKey = (e: KeyboardEvent) => { if (open === null) return; const list = menus[open].items; const n = menus.length; const k = e.key; if (k === "ArrowDown" || k === "ArrowUp") { e.preventDefault(); setHi(first(open, hi < 0 && k === "ArrowUp" ? 0 : hi, k === "ArrowDown" ? 1 : -1)); } else if (k === "Home" || k === "End") { e.preventDefault(); setHi(k === "Home" ? first(open) : first(open, 0, -1)); } else if (k === "ArrowRight" || k === "ArrowLeft") { e.preventDefault(); const j = (open + (k === "ArrowRight" ? 1 : -1) + n) % n; show(j, first(j)); } else if (k === "Escape") { e.preventDefault(); close(); } else if (k === "Tab") { close(false); } else if (k.length === 1 && /\S/.test(k)) { const ch = k.toLowerCase(); for (let s = 1; s <= list.length; s++) { const i = (hi + s) % list.length; const it = list[i]; if (enabled(it) && actionable(it) && it.label.toLowerCase().startsWith(ch)) { setHi(i); break; } } } }; return ( <div ref={root} role="menubar" aria-label={label} className={cx("relative flex items-center gap-0.5", className)}> <style href="asta-menubar" precedence="asta">{KEYFRAMES}</style> {menus.map((m, i) => { const isOpen = open === i; return ( <div key={m.label} className="relative"> <button ref={(el) => { tops.current[i] = el; }} type="button" role="menuitem" aria-haspopup="menu" aria-expanded={isOpen} tabIndex={focus === i ? 0 : -1} onClick={() => (isOpen ? close() : show(i))} onPointerEnter={(e) => { if (e.pointerType === "mouse" && open !== null && open !== i) show(i); }} onKeyDown={(e) => onTopKey(e, i)} className={cx( "flex h-8 select-none items-center rounded-md px-2.5 text-[13px] font-medium outline-none transition-colors duration-150", "focus-visible:ring-2 focus-visible:ring-brand/50", isOpen ? "bg-paper-2 text-ink" : "text-ink-2 hover:bg-paper-2 hover:text-ink", )} > {m.label} </button> {isOpen && ( <div ref={panel} role="menu" aria-label={m.label} onKeyDown={onMenuKey} style={{ transform: shift ? `translateX(${shift}px)` : undefined }} className={cx( "asta-menubar-panel absolute left-0 top-full z-50 mt-1.5 flex min-w-[232px] origin-top-left flex-col rounded-[11px] bg-paper p-1", "shadow-[0_0_0_1px_color-mix(in_srgb,var(--color-ink)_9%,transparent),0_12px_32px_-12px_rgb(0_0_0/0.28),0_2px_6px_-2px_rgb(0_0_0/0.08)]", !instant && "animate-[asta-menubar-in_.18s_cubic-bezier(.16,1,.3,1)_both]", )} > {m.items.map((it, j) => { if (it.type === "separator") return <div key={`s${j}`} role="separator" className="mx-2 my-1 h-px bg-[color-mix(in_srgb,var(--color-ink)_8%,transparent)]" />; if (it.type === "label") return ( <p key={`l${j}`} className="px-2.5 pb-1 pt-2 font-mono text-[10.5px] uppercase tracking-wider text-mute"> {it.label} </p> ); const role = it.type === "checkbox" ? "menuitemcheckbox" : it.type === "radio" ? "menuitemradio" : "menuitem"; const checked = it.type === "checkbox" || it.type === "radio" ? it.checked : undefined; return ( <button key={it.label} ref={(el) => { items.current[j] = el; }} type="button" role={role} aria-checked={checked} aria-disabled={it.disabled || undefined} tabIndex={-1} onClick={() => activate(it)} onPointerMove={() => !it.disabled && hi !== j && setHi(j)} className={cx( "flex h-8 w-full select-none items-center gap-2 rounded-[7px] px-2 text-left text-[13px] outline-none", hi === j ? "bg-paper-2 text-ink" : "text-ink-2", it.disabled && "opacity-45", )} > <span className="flex w-4 shrink-0 justify-center text-ink"> {it.type === "checkbox" && it.checked && <Check />} {it.type === "radio" && it.checked && <span className="h-1.5 w-1.5 rounded-full bg-current" />} {(it.type === undefined || it.type === "item") && it.icon && <span className="flex text-mute [&_svg]:h-3.5 [&_svg]:w-3.5">{it.icon}</span>} </span> <span className="flex-1 truncate">{it.label}</span> {it.shortcut && <span className="pl-4 text-[12px] tracking-wide text-mute">{it.shortcut}</span>} </button> ); })} </div> )} </div> ); })} </div> ); } export default Menubar;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Menubar component to my project. File, Edit, View, like a desktop app. It includes: menus, shortcuts, checkbox items, radio items, separators, type-ahead, keys. Install it: ```sh pnpm dlx asta-ui@latest add menubar --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/menubar.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Menubar, type MenubarMenu } from "@/components/astabyte/menubar"; ``` Usage example and props: https://ui.astabyte.com/menubar Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- Each menu is an asta-dropdown, so asta.js opens it, positions it and handles the keys and aria-checked. --> <div role="menubar" class="asta-menubar" aria-label="Astabyte Notes"> <div class="asta-dropdown" data-asta-dropdown> <button type="button" class="asta-menubar-trigger" data-asta-dropdown-trigger aria-haspopup="menu" aria-expanded="false" id="ex-mb-file-trigger" aria-controls="ex-mb-file">File</button> <div role="menu" popover="manual" tabindex="-1" data-asta-mpanel id="ex-mb-file" aria-labelledby="ex-mb-file-trigger" class="asta-float asta-dropdown-panel"><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">New note</span><span class="asta-menu-shortcut">⌘N</span></button><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Open…</span><span class="asta-menu-shortcut">⌘O</span></button><div role="separator" class="asta-menu-separator"></div><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Export as PDF</span></button><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Share link</span><span class="asta-menu-shortcut">⌘⇧S</span></button></div> </div> <div class="asta-dropdown" data-asta-dropdown> <button type="button" class="asta-menubar-trigger" data-asta-dropdown-trigger aria-haspopup="menu" aria-expanded="false" id="ex-mb-edit-trigger" aria-controls="ex-mb-edit">Edit</button> <div role="menu" popover="manual" tabindex="-1" data-asta-mpanel id="ex-mb-edit" aria-labelledby="ex-mb-edit-trigger" class="asta-float asta-dropdown-panel"><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Undo</span><span class="asta-menu-shortcut">⌘Z</span></button><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Redo</span><span class="asta-menu-shortcut">⌘⇧Z</span></button><div role="separator" class="asta-menu-separator"></div><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Find</span><span class="asta-menu-shortcut">⌘F</span></button></div> </div> <div class="asta-dropdown" data-asta-dropdown> <button type="button" class="asta-menubar-trigger" data-asta-dropdown-trigger aria-haspopup="menu" aria-expanded="false" id="ex-mb-view-trigger" aria-controls="ex-mb-view">View</button> <div role="menu" popover="manual" tabindex="-1" data-asta-mpanel id="ex-mb-view" aria-labelledby="ex-mb-view-trigger" class="asta-float asta-dropdown-panel"><button type="button" role="menuitemcheckbox" tabindex="-1" aria-checked="false" data-keep-open data-value="ruler" class="asta-menu-item"><span class="asta-menu-check"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m4 8.5 2.5 2.5L12 5.5"/></svg></span><span class="asta-menu-item-text">Show ruler</span></button><button type="button" role="menuitemcheckbox" tabindex="-1" aria-checked="false" data-keep-open data-value="wrap" class="asta-menu-item"><span class="asta-menu-check"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m4 8.5 2.5 2.5L12 5.5"/></svg></span><span class="asta-menu-item-text">Wrap lines</span></button><div role="separator" class="asta-menu-separator"></div><div role="presentation" class="asta-menu-label">Zoom</div><button type="button" role="menuitemradio" tabindex="-1" aria-checked="false" data-group="zoom" data-value="90" class="asta-menu-item"><span class="asta-menu-radio"><span></span></span><span class="asta-menu-item-text">90%</span></button><button type="button" role="menuitemradio" tabindex="-1" aria-checked="true" data-group="zoom" data-value="100" class="asta-menu-item"><span class="asta-menu-radio"><span></span></span><span class="asta-menu-item-text">100%</span></button><button type="button" role="menuitemradio" tabindex="-1" aria-checked="false" data-group="zoom" data-value="125" class="asta-menu-item"><span class="asta-menu-radio"><span></span></span><span class="asta-menu-item-text">125%</span></button></div> </div> <div class="asta-dropdown" data-asta-dropdown> <button type="button" class="asta-menubar-trigger" data-asta-dropdown-trigger aria-haspopup="menu" aria-expanded="false" id="ex-mb-help-trigger" aria-controls="ex-mb-help">Help</button> <div role="menu" popover="manual" tabindex="-1" data-asta-mpanel id="ex-mb-help" aria-labelledby="ex-mb-help-trigger" class="asta-float asta-dropdown-panel"><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">Keyboard shortcuts</span><span class="asta-menu-shortcut">?</span></button><button type="button" role="menuitem" tabindex="-1" class="asta-menu-item"><span class="asta-menu-item-text">What’s new</span></button></div> </div> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { Menubar, type MenubarMenu } from "@/components/astabyte/menubar";
export default function MenubarDemo() {
const [last, setLast] = useState("Nothing yet");
const [ruler, setRuler] = useState(false);
const [wrap, setWrap] = useState(false);
const [zoom, setZoom] = useState("100%");
const [saved, setSaved] = useState(true);
const act = (name: string) => () => {
setLast(name);
if (name === "Save") setSaved(true);
else if (name !== "Export as PDF") setSaved(false);
};
const menus: MenubarMenu[] = [
{
label: "File",
items: [
{ label: "New note", shortcut: "⌘N", onSelect: act("New note") },
{ label: "Open…", shortcut: "⌘O", onSelect: act("Open") },
{ type: "separator" },
{ label: "Save", shortcut: "⌘S", onSelect: act("Save") },
{ label: "Export as PDF", onSelect: act("Export as PDF") },
{ type: "separator" },
{ label: "Share with team", disabled: true },
],
},
{
label: "Edit",
items: [
{ label: "Undo", shortcut: "⌘Z", onSelect: act("Undo") },
{ label: "Redo", shortcut: "⇧⌘Z", onSelect: act("Redo") },
{ type: "separator" },
{ label: "Cut", shortcut: "⌘X", onSelect: act("Cut") },
{ label: "Copy", shortcut: "⌘C", onSelect: act("Copy") },
{ label: "Paste", shortcut: "⌘V", onSelect: act("Paste") },
{ type: "separator" },
{ label: "Find…", shortcut: "⌘F", onSelect: act("Find") },
],
},
{
label: "View",
items: [
{ type: "checkbox", label: "Show ruler", checked: ruler, onCheckedChange: setRuler },
{ type: "checkbox", label: "Wrap lines", shortcut: "⌥Z", checked: wrap, onCheckedChange: setWrap },
{ type: "separator" },
{ type: "label", label: "Zoom" },
...["75%", "100%", "125%"].map((z) => ({ type: "radio" as const, label: z, checked: zoom === z, onSelect: () => setZoom(z) })),
],
},
{
label: "Help",
items: [
{ label: "Keyboard shortcuts", shortcut: "⌘/", onSelect: act("Keyboard shortcuts") },
{ label: "What's new", onSelect: act("What's new") },
{ label: "Contact support", onSelect: act("Contact support") },
],
},
];
return (
<div className="flex min-h-[460px] w-full items-start justify-center p-4 pt-8 sm:p-10">
<div className="w-full max-w-[640px] rounded-xl bg-paper shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent),0_18px_40px_-24px_rgb(0_0_0/0.3)]">
<div className="flex items-center gap-2 px-2 py-1.5">
<span className="ml-1.5 mr-1 hidden h-5 w-5 shrink-0 place-items-center rounded-[5px] bg-ink text-[10px] font-semibold text-paper sm:grid">A</span>
<Menubar menus={menus} label="Astabyte Notes" />
<span className="ml-auto hidden pr-2 text-[12px] text-mute sm:block">{saved ? "Saved" : "Edited"}</span>
</div>
<div className="mx-2 mb-2 rounded-[9px] bg-paper-2 px-5 py-6 sm:px-8">
{ruler && (
// A page ruler: a major tick per unit with its number, eight minor ticks between.
<div
aria-hidden
className="relative -mx-1 mb-4 h-4"
style={{
backgroundImage:
"linear-gradient(to right, color-mix(in srgb, var(--color-ink) 35%, transparent) 1px, transparent 1px), linear-gradient(to right, color-mix(in srgb, var(--color-ink) 18%, transparent) 1px, transparent 1px), linear-gradient(color-mix(in srgb, var(--color-ink) 18%, transparent), color-mix(in srgb, var(--color-ink) 18%, transparent))",
backgroundSize: "calc(100% / 6) 10px, calc(100% / 48) 4px, 100% 1px",
backgroundPosition: "left bottom, left bottom, left bottom",
backgroundRepeat: "repeat-x, repeat-x, no-repeat",
}}
>
{[0, 1, 2, 3, 4, 5].map((n) => (
<span key={n} className="absolute top-0 pl-1 font-mono text-[9.5px] leading-none text-mute" style={{ left: `${(n / 6) * 100}%` }}>
{n}
</span>
))}
</div>
)}
<div style={{ fontSize: `${(parseInt(zoom) / 100) * 14}px` }} className="text-ink-2">
<p className="text-[1.3em] font-medium tracking-[-0.02em] text-ink">Launch week notes</p>
<p className={`mt-2 leading-relaxed ${wrap ? "" : "truncate"}`}>Ship the new pricing page on Monday, then the changelog post and a short demo video for the newsletter on Wednesday.</p>
</div>
<p className="mt-6 text-[12px] text-mute">
Last action: <span className="text-ink">{last}</span> · Zoom {zoom}
</p>
</div>
</div>
</div>
);
}