{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hamburger-icon-that-morphs-into-an-x","type":"registry:component","title":"Hamburger Icon That Morphs Into an X","description":"Free hamburger menu in HTML, CSS and React: three lines turn into an X on a real button with aria-expanded, the menu drops below and Escape closes it.","dependencies":[],"files":[{"path":"components/we-inc/hamburger-icon-that-morphs-into-an-x.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Edit the menu here. The icon itself is three spans moved with CSS transforms.\nconst brand = \"Fenwick Bakehouse\";\nconst links = [\n  { label: \"Breads\", note: \"Sourdough, rye and the Saturday loaf\", href: \"#\" },\n  { label: \"Pastries\", note: \"Laminated every morning from 4am\", href: \"#\" },\n  { label: \"Order ahead\", note: \"Collect from 7:30 on weekdays\", href: \"#\" },\n  { label: \"Visit\", note: \"14 Quay Street, open six days\", href: \"#\" },\n];\n\nexport default function HamburgerIconThatMorphsIntoAnX() {\n  const [open, setOpen] = useState(true);\n  const button = useRef<HTMLButtonElement>(null);\n\n  useEffect(() => {\n    if (!open) return;\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        setOpen(false);\n        button.current?.focus();\n      }\n    };\n    document.addEventListener(\"keydown\", onKey);\n    return () => document.removeEventListener(\"keydown\", onKey);\n  }, [open]);\n\n  const bar = \"absolute left-0 h-0.5 w-6 rounded-full bg-current transition duration-300 ease-out motion-reduce:transition-none\";\n\n  return (\n    <div className=\"bg-amber-50 px-4 py-10 sm:px-8\">\n      <div className=\"relative mx-auto h-[420px] max-w-3xl overflow-hidden rounded-2xl border border-amber-200 bg-white\">\n        <header className=\"flex h-16 items-center justify-between border-b border-amber-100 px-5\">\n          <a href=\"#\" className=\"rounded font-serif text-lg font-semibold tracking-tight text-stone-900 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600\">\n            {brand}\n          </a>\n          <button\n            ref={button}\n            type=\"button\"\n            aria-expanded={open}\n            aria-controls=\"hmx-menu\"\n            aria-label={open ? \"Close menu\" : \"Open menu\"}\n            onClick={() => setOpen((v) => !v)}\n            className=\"relative grid h-11 w-11 place-items-center rounded-full text-stone-900 transition-colors hover:bg-amber-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600\"\n          >\n            <span className=\"relative block h-6 w-6\" aria-hidden=\"true\">\n              <span className={`${bar} top-[5px] ${open ? \"translate-y-[6px] rotate-45\" : \"\"}`} />\n              <span className={`${bar} top-[11px] ${open ? \"scale-x-0 opacity-0\" : \"\"}`} />\n              <span className={`${bar} top-[17px] ${open ? \"-translate-y-[6px] -rotate-45\" : \"\"}`} />\n            </span>\n          </button>\n        </header>\n\n        <nav\n          id=\"hmx-menu\"\n          aria-label=\"Main\"\n          className={`absolute inset-x-0 top-16 origin-top px-5 pb-6 pt-4 transition duration-300 ease-out motion-reduce:transition-none ${open ? \"visible translate-y-0 opacity-100\" : \"invisible -translate-y-2 opacity-0\"}`}\n        >\n          <ul className=\"divide-y divide-amber-100\">\n            {links.map((l) => (\n              <li key={l.label}>\n                <a href=\"#\" className=\"group flex items-baseline justify-between gap-4 rounded-lg px-2 py-4 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-600\">\n                  <span className=\"font-serif text-2xl text-stone-900 group-hover:text-amber-700\">{l.label}</span>\n                  <span className=\"hidden text-sm text-stone-600 sm:block\">{l.note}</span>\n                </a>\n              </li>\n            ))}\n          </ul>\n        </nav>\n\n        {!open && (\n          <div className=\"grid h-[calc(100%-4rem)] place-items-center px-6 text-center\">\n            <p className=\"max-w-xs text-stone-600\">Menu closed. Press the button to see the three lines turn back into an X.</p>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n"}]}
