GooseUI

Components

Burger Button

New

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

:has()

Styles elements based on their contents

Your browser:
Popover

Opens and closes the popup

Your browser:

Preview

Hover and click.

Installation

npx shadcn@latest add @gooseui/burger-button
pnpm dlx shadcn@latest add @gooseui/burger-button
yarn dlx shadcn@latest add @gooseui/burger-button
bunx --bun shadcn@latest add @gooseui/burger-button

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.

cross
squeeze
spin
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

PropTypeDefaultDescription
openboolean-Controlled state. Renders a button with aria-expanded
defaultOpenbooleanfalseInitial state of the built-in checkbox
onOpenChange(open: boolean) => void-Called with the next state. Only inside client components
popoverTargetstring-Id of a popover element. The button opens it and follows its state
namestring-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
classNamestring-Additional CSS classes