Dense menubars
Five menubars that keep one visual language and vary only the scale: compact, narrow, tight, slim and micro. The size prop still scales each variant within its own density. Each is self-contained (no radix), token-driven, switches menus on hover once one is open, and closes on outside click or Escape.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/menubar-denseDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/menubar-dense.tsx"use client"
import * as React from "react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Dense menubar family: 5 decorative, SELF-SUFFICIENT menubars. NO radix or
portal. The visual language is the same in every variant; the whole difference is
the SCALE: Compact tightens slightly, Narrow trims the horizontal padding, Tight
lowers the line height, Slim thins the bar, and Micro drops to toolbar density.
The size prop scales each variant within its own density. Classic menubar
behavior: while one menu is open, hovering a sibling trigger switches to that
menu. It closes on an outside click, on Escape (focus returns to the trigger) and
on an item selection. Left/Right arrows move between the top-level menus and
Up/Down moves inside the open one. No transform under reduced motion. Color comes
ONLY from tokens, via alpha color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type MenuItem = { label: React.ReactNode; onSelect?: () => void }
type Menu = { label: React.ReactNode; items: MenuItem[] }
interface MenubarProps {
className?: string
size?: StyledSize
menus?: Menu[]
}
const defaultMenus: Menu[] = [
{
label: "File",
items: [
{ label: "New File" },
{ label: "Open..." },
{ label: "Save" },
{ label: "Save As..." },
],
},
{
label: "Edit",
items: [
{ label: "Undo" },
{ label: "Redo" },
{ label: "Cut" },
{ label: "Copy" },
{ label: "Paste" },
],
},
{
label: "View",
items: [{ label: "Zoom In" }, { label: "Zoom Out" }, { label: "Full Screen" }],
},
]
type Scale = {
bar: Record<StyledSize, string>
trigger: Record<StyledSize, string>
panel: Record<StyledSize, string>
item: Record<StyledSize, string>
panelPad: string
}
const softHover =
"hover:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)]"
const triggerBase =
"inline-flex select-none items-center gap-1.5 whitespace-nowrap rounded-md font-medium outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 [&>svg]:size-4 [&>svg]:shrink-0 [&>i]:text-base [&>i]:leading-none"
const itemBase =
"flex w-full cursor-default select-none items-center gap-2 rounded-md text-left text-popover-foreground outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 [&>svg]:size-4 [&>svg]:shrink-0 [&>i]:text-base [&>i]:leading-none"
const panelBase =
"absolute left-0 top-full z-50 origin-top rounded-lg border border-border bg-popover text-popover-foreground shadow-lg outline-none"
const barBase = "inline-flex items-center border border-border bg-card shadow-xs"
/* The shared menubar core. The panel is always absolutely positioned inside the
relative wrapper of its own trigger, so many examples on a single page never
collide. */
function MenubarCore({
props,
scale,
barClassName,
}: {
props: MenubarProps
scale: Scale
barClassName?: string
}) {
const { className, size = "md", menus = defaultMenus } = props
const reduce = useReducedMotion()
const uid = React.useId()
const [openIndex, setOpenIndex] = React.useState<number | null>(null)
const [autoFocusItem, setAutoFocusItem] = React.useState(false)
const rootRef = React.useRef<HTMLDivElement>(null)
const triggerRefs = React.useRef<Array<HTMLButtonElement | null>>([])
const itemRefs = React.useRef<Array<HTMLButtonElement | null>>([])
React.useEffect(() => {
if (openIndex === null) return
const onKey = (e: KeyboardEvent) => {
if (e.key !== "Escape") return
triggerRefs.current[openIndex]?.focus()
setOpenIndex(null)
}
const onPointer = (e: PointerEvent) => {
const node = rootRef.current
if (node && e.target instanceof Node && !node.contains(e.target)) {
setOpenIndex(null)
}
}
window.addEventListener("keydown", onKey)
window.addEventListener("pointerdown", onPointer)
return () => {
window.removeEventListener("keydown", onKey)
window.removeEventListener("pointerdown", onPointer)
}
}, [openIndex])
React.useEffect(() => {
if (openIndex === null || !autoFocusItem) return
const id = window.requestAnimationFrame(() => itemRefs.current[0]?.focus())
setAutoFocusItem(false)
return () => window.cancelAnimationFrame(id)
}, [openIndex, autoFocusItem])
const moveTrigger = (from: number, dir: 1 | -1) => {
const next = (from + dir + menus.length) % menus.length
triggerRefs.current[next]?.focus()
if (openIndex !== null) setOpenIndex(next)
}
const onTriggerKeyDown = (e: React.KeyboardEvent, i: number) => {
if (e.key === "ArrowRight") {
e.preventDefault()
moveTrigger(i, 1)
} else if (e.key === "ArrowLeft") {
e.preventDefault()
moveTrigger(i, -1)
} else if (e.key === "ArrowDown") {
e.preventDefault()
setOpenIndex(i)
setAutoFocusItem(true)
}
}
const onItemKeyDown = (e: React.KeyboardEvent, j: number, count: number, i: number) => {
if (e.key === "ArrowDown") {
e.preventDefault()
itemRefs.current[(j + 1) % count]?.focus()
} else if (e.key === "ArrowUp") {
e.preventDefault()
itemRefs.current[(j - 1 + count) % count]?.focus()
} else if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
e.preventDefault()
moveTrigger(i, e.key === "ArrowRight" ? 1 : -1)
setAutoFocusItem(true)
}
}
const enter = reduce
? { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } }
: {
initial: { opacity: 0, scale: 0.97, y: -4 },
animate: { opacity: 1, scale: 1, y: 0 },
exit: { opacity: 0, scale: 0.97, y: -4 },
}
return (
<div
ref={rootRef}
data-slot="styled-menubar"
role="menubar"
className={cn(barBase, scale.bar[size], barClassName, className)}
>
{menus.map((menu, i) => {
const isOpen = openIndex === i
const triggerId = `${uid}-trigger-${i}`
const menuId = `${uid}-menu-${i}`
return (
<div key={i} className="relative">
<button
type="button"
id={triggerId}
ref={(node) => {
triggerRefs.current[i] = node
}}
role="menuitem"
aria-haspopup="menu"
aria-expanded={isOpen}
aria-controls={isOpen ? menuId : undefined}
onClick={() => setOpenIndex(isOpen ? null : i)}
onPointerEnter={() => {
if (openIndex !== null) setOpenIndex(i)
}}
onKeyDown={(e) => onTriggerKeyDown(e, i)}
className={cn(
triggerBase,
scale.trigger[size],
isOpen
? "bg-accent text-accent-foreground"
: cn("text-muted-foreground hover:text-foreground", softHover)
)}
>
{menu.label}
</button>
<AnimatePresence>
{isOpen ? (
<motion.div
id={menuId}
role="menu"
aria-labelledby={triggerId}
className={cn(panelBase, scale.panel[size], scale.panelPad)}
initial={enter.initial}
animate={enter.animate}
exit={enter.exit}
transition={
reduce
? { duration: 0.12 }
: { type: "spring" as const, stiffness: 360, damping: 26 }
}
>
{menu.items.map((item, j) => (
<button
key={j}
type="button"
role="menuitem"
ref={(node) => {
if (openIndex === i) itemRefs.current[j] = node
}}
onClick={() => {
item.onSelect?.()
triggerRefs.current[i]?.focus()
setOpenIndex(null)
}}
onKeyDown={(e) => onItemKeyDown(e, j, menu.items.length, i)}
className={cn(itemBase, scale.item[size], softHover)}
>
{item.label}
</button>
))}
</motion.div>
) : null}
</AnimatePresence>
</div>
)
})}
</div>
)
}
/* Compact: temel olcegin bir tik sikilastirilmis hali. */
const compactScale: Scale = {
bar: {
sm: "h-7 gap-0.5 rounded-md px-0.5 text-xs",
md: "h-8 gap-0.5 rounded-md px-1 text-xs",
lg: "h-9 gap-1 rounded-lg px-1 text-sm",
xl: "h-10 gap-1 rounded-lg px-1.5 text-sm",
},
trigger: {
sm: "h-5 px-1.5",
md: "h-6 px-2",
lg: "h-7 px-2.5",
xl: "h-8 px-3",
},
panel: {
sm: "mt-1 min-w-36 text-xs",
md: "mt-1 min-w-40 text-xs",
lg: "mt-1 min-w-44 text-sm",
xl: "mt-1.5 min-w-48 text-sm",
},
item: {
sm: "px-1.5 py-1",
md: "px-2 py-1",
lg: "px-2.5 py-1.5",
xl: "px-3 py-1.5",
},
panelPad: "p-1",
}
export function CompactMenubar2({ className, size = "md", menus }: MenubarProps) {
return <MenubarCore props={{ className, size, menus }} scale={compactScale} />
}
/* Narrow: the horizontal padding is trimmed, the row height is kept. */
const narrowScale: Scale = {
bar: {
sm: "h-8 gap-0 rounded-md px-0.5 text-xs",
md: "h-9 gap-0 rounded-md px-0.5 text-sm",
lg: "h-10 gap-0.5 rounded-lg px-1 text-sm",
xl: "h-11 gap-0.5 rounded-lg px-1 text-base",
},
trigger: {
sm: "h-6 px-1",
md: "h-7 px-1.5",
lg: "h-8 px-2",
xl: "h-9 px-2",
},
panel: {
sm: "mt-1 min-w-32 text-xs",
md: "mt-1 min-w-36 text-sm",
lg: "mt-1 min-w-40 text-sm",
xl: "mt-1.5 min-w-44 text-base",
},
item: {
sm: "px-1.5 py-1.5",
md: "px-1.5 py-1.5",
lg: "px-2 py-2",
xl: "px-2 py-2",
},
panelPad: "p-0.5",
}
export function NarrowMenubar({ className, size = "md", menus }: MenubarProps) {
return <MenubarCore props={{ className, size, menus }} scale={narrowScale} />
}
/* Tight: dikey ritim sikisir, menu satirlari birbirine yaklasir. */
const tightScale: Scale = {
bar: {
sm: "h-6 gap-0.5 rounded-md px-0.5 text-[0.6875rem]",
md: "h-7 gap-0.5 rounded-md px-0.5 text-xs",
lg: "h-8 gap-0.5 rounded-md px-1 text-xs",
xl: "h-9 gap-1 rounded-lg px-1 text-sm",
},
trigger: {
sm: "h-5 px-1.5",
md: "h-5 px-2",
lg: "h-6 px-2",
xl: "h-7 px-2.5",
},
panel: {
sm: "mt-0.5 min-w-32 text-[0.6875rem]",
md: "mt-1 min-w-36 text-xs",
lg: "mt-1 min-w-40 text-xs",
xl: "mt-1 min-w-44 text-sm",
},
item: {
sm: "px-1.5 py-0.5",
md: "px-2 py-0.5",
lg: "px-2 py-1",
xl: "px-2.5 py-1",
},
panelPad: "p-0.5",
}
export function TightMenubar({ className, size = "md", menus }: MenubarProps) {
return <MenubarCore props={{ className, size, menus }} scale={tightScale} />
}
/* Slim: the bar is borderless and thin, carrying a hairline underneath. */
const slimScale: Scale = {
bar: {
sm: "h-6 gap-1 px-0 text-[0.6875rem]",
md: "h-7 gap-1 px-0 text-xs",
lg: "h-8 gap-1.5 px-0 text-xs",
xl: "h-9 gap-1.5 px-0 text-sm",
},
trigger: {
sm: "h-5 px-1.5",
md: "h-6 px-2",
lg: "h-6 px-2",
xl: "h-7 px-2.5",
},
panel: {
sm: "mt-1 min-w-32 text-[0.6875rem]",
md: "mt-1 min-w-36 text-xs",
lg: "mt-1 min-w-40 text-xs",
xl: "mt-1.5 min-w-44 text-sm",
},
item: {
sm: "px-1.5 py-1",
md: "px-2 py-1",
lg: "px-2 py-1",
xl: "px-2.5 py-1.5",
},
panelPad: "p-0.5",
}
export function SlimMenubar({ className, size = "md", menus }: MenubarProps) {
return (
<MenubarCore
props={{ className, size, menus }}
scale={slimScale}
barClassName="rounded-none border-0 border-b border-border bg-transparent shadow-none"
/>
)
}
/* Micro: en yogun kademe, arac cubugu olcegi. */
const microScale: Scale = {
bar: {
sm: "h-5 gap-0 rounded px-0 text-[0.625rem]",
md: "h-6 gap-0 rounded px-0.5 text-[0.6875rem]",
lg: "h-7 gap-0 rounded-md px-0.5 text-xs",
xl: "h-8 gap-0.5 rounded-md px-0.5 text-xs",
},
trigger: {
sm: "h-4 gap-1 rounded-sm px-1",
md: "h-5 gap-1 rounded-sm px-1",
lg: "h-6 gap-1 rounded px-1.5",
xl: "h-7 gap-1 rounded px-1.5",
},
panel: {
sm: "mt-0.5 min-w-28 rounded-md text-[0.625rem]",
md: "mt-0.5 min-w-32 rounded-md text-[0.6875rem]",
lg: "mt-1 min-w-36 rounded-md text-xs",
xl: "mt-1 min-w-40 text-xs",
},
item: {
sm: "gap-1 rounded-sm px-1 py-0.5",
md: "gap-1 rounded-sm px-1.5 py-0.5",
lg: "gap-1.5 rounded px-1.5 py-0.5",
xl: "gap-1.5 rounded px-2 py-1",
},
panelPad: "p-0.5",
}
export function MicroMenubar({ className, size = "md", menus }: MenubarProps) {
return (
<MenubarCore
props={{ className, size, menus }}
scale={microScale}
barClassName="[&_button]:font-normal"
/>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Compact
One notch tighter than the base scale.
import { CompactMenubar2 } from "@/components/ui/menubar-dense"
<CompactMenubar2 />Narrow
Horizontal padding is trimmed, row height stays.
import { NarrowMenubar } from "@/components/ui/menubar-dense"
<NarrowMenubar />Tight
The vertical rhythm closes up across bar and menu.
import { TightMenubar } from "@/components/ui/menubar-dense"
<TightMenubar />Slim
A borderless thin bar on a single hairline rule.
import { SlimMenubar } from "@/components/ui/menubar-dense"
<SlimMenubar />Micro
The densest step, at toolbar scale.
import { MicroMenubar } from "@/components/ui/menubar-dense"
<MicroMenubar />ai2 Dense menubars: 5 styled variations on the token system
The ai2 Dense menubars are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around application menu bars on compact scales. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion springs the open dropdown in and out. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the transforms are skipped and the menu fades in place.
What is in the ai2 Dense menubars?
5 exports in one file: Compact, Narrow, Tight, Slim and Micro. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion springs the open dropdown in and out.
- Reduced-motion aware: Under prefers-reduced-motion, the transforms are skipped and the menu fades in place.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Dense menubars sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.