A menu toggle made of lines of different length. On hover the lines even out, and when open they turn into a cross or an arrow. The animation is CSS and the component is a Server Component. By default the state lives in a visually hidden checkbox, popoverTarget turns it into a button that opens a popover and follows :popover-open, and open with onOpenChange gives you a controlled button for state kept in a client component.
Browser Support
Styles elements based on their contents
Opens and closes the popup
Preview
Hover and click.
Installation
Usage
import { BurgerButton } from "@/components/ui/burger-button"
// A Server Component: the burger opens a popover menu on its own
export default function Header() {
return (
<header>
<BurgerButton popoverTarget="main-menu" />
<nav id="main-menu" popover="auto">…</nav>
</header>
)
}Examples
Animation variants
cross rotates the outer lines, squeeze first brings them together and then rotates, spin turns the whole icon, arrow forms an arrow.
<BurgerButton variant="cross" />
<BurgerButton variant="squeeze" />
<BurgerButton variant="spin" />
<BurgerButton variant="arrow" />Sizes and appearance
Three sizes and three looks: ghost, outline and solid.
<BurgerButton size="sm" appearance="ghost" />
<BurgerButton size="md" appearance="outline" />
<BurgerButton size="lg" appearance="solid" />Popover menu without JavaScript
popoverTarget points the button at an element with the popover attribute. The browser opens and closes it, closes it on Escape or an outside click, and the lines follow :popover-open.
<BurgerButton popoverTarget="main-menu" variant="arrow" />
<nav id="main-menu" popover="auto">…</nav>CSS-only menu
In the default mode the burger is a checkbox, so a parent can show the menu with :has(:checked). No state and no script.
<header className="group/header">
<BurgerButton aria-controls="menu" />
<nav id="menu" className="hidden group-has-[.gb_:checked]/header:block">…</nav>
</header>Controlled menu
Keep the state yourself with open and onOpenChange, and link the button to the menu with aria-controls.
Press the burger
const [open, setOpen] = useState(false)
<BurgerButton
open={open}
onOpenChange={setOpen}
aria-controls="main-menu"
labels={{ open: "Open navigation", close: "Close navigation" }}
/>
<nav id="main-menu" hidden={!open}>…</nav>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | - | Controlled state. Renders a button with aria-expanded |
| defaultOpen | boolean | false | Initial state of the built-in checkbox |
| onOpenChange | (open: boolean) => void | - | Called with the next state. Only inside client components |
| popoverTarget | string | - | Id of a popover element. The button opens it and follows its state |
| name | string | - | Name of the built-in checkbox for forms |
| variant | "cross" | "squeeze" | "spin" | "arrow" | "cross" | How the lines animate into the open state |
| size | "sm" | "md" | "lg" | "md" | Button and icon size |
| appearance | "outline" | "ghost" | "solid" | "outline" | Button look |
| labels | { open?: string; close?: string; menu?: string } | "Open menu" / "Close menu" / "Menu" | Screen reader labels: open and close for the controlled button, menu for the checkbox and popover modes |
| className | string | - | Additional CSS classes |