{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"floating-action-menu","type":"registry:component","title":"Floating action menu","description":"The plus folds away and its actions unfold upward out of the spot it sat in, over a frosted screen.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/floating-action-menu.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { motion } from \"motion/react\";\nimport { Caveat } from \"next/font/google\";\nimport { usePreviewPlay } from \"@/lib/preview-play\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst hand = Caveat({ subsets: [\"latin\"], weight: [\"500\"], preload: false });\n\n// After Nick Pyl's floating action button (https://x.com/nickpylll): the\n// plus folds away and the actions it was holding unfold upward out of the\n// spot it sat in, over a screen that frosts so they're the only thing left\n// to read.\n\nexport type FabAction = {\n  label: string;\n  // The action's own hue, kept as data like an app's brand colours.\n  tone: string;\n  icon: React.ReactNode;\n};\n\n// A row's height plus the gap between rows.\nconst STEP = 58;\n// Springs that settle without a wobble you'd notice, and close quicker\n// than they open.\nconst UNFOLD = { type: \"spring\", visualDuration: 0.42, bounce: 0.18 } as const;\nconst FOLD = { type: \"spring\", visualDuration: 0.26, bounce: 0 } as const;\n// Each row leaves a beat after the one below it, so they fan out of the\n// button rather than appearing as a block.\nconst STAGGER = 0.035;\n\nexport function FloatingActionMenu({\n  actions,\n  onAction,\n  hint = \"tap me, there's more\",\n  className,\n  children,\n}: {\n  actions: FabAction[];\n  onAction?: (label: string) => void;\n  // A handwritten nudge at the button, gone once it has been opened.\n  hint?: string;\n  className?: string;\n  // The screen behind the button.\n  children?: React.ReactNode;\n}) {\n  const reduceMotion = useReducedMotion();\n  const play = usePreviewPlay();\n  const [own, setOwn] = useState(false);\n  const [opened, setOpened] = useState(false);\n  const [show, setShow] = useState(false);\n  const open = play ? show : own;\n  const fab = useRef<HTMLButtonElement>(null);\n  const first = useRef<HTMLButtonElement>(null);\n  const restoreFocus = useRef(false);\n  const menuId = useId();\n\n  // Into the menu when it opens; back to the button when it closes, but\n  // only if focus was inside it, so a click elsewhere keeps its focus.\n  useEffect(() => {\n    if (play) return;\n    if (own) {\n      first.current?.focus({ preventScroll: true });\n    } else if (restoreFocus.current) {\n      restoreFocus.current = false;\n      fab.current?.focus({ preventScroll: true });\n    }\n  }, [own, play]);\n\n  // The card's show: open, hold, fold away, rest.\n  useEffect(() => {\n    if (!play) return;\n    const timers: ReturnType<typeof setTimeout>[] = [];\n    const loop = () => {\n      timers.push(setTimeout(() => setShow(true), 500));\n      timers.push(setTimeout(() => setShow(false), 2300));\n      timers.push(setTimeout(loop, 3400));\n    };\n    loop();\n    return () => {\n      timers.forEach(clearTimeout);\n      setShow(false);\n    };\n  }, [play]);\n\n  const close = (returnFocus: boolean) => {\n    restoreFocus.current = returnFocus;\n    setOwn(false);\n  };\n\n  // The rows sit bottom-up: the last action takes the button's own spot.\n  const last = actions.length - 1;\n\n  return (\n    <div\n      className={cn(\n        \"relative isolate h-[560px] w-[340px] max-w-full overflow-hidden rounded-[44px] bg-background shadow-raised\",\n        className,\n      )}\n      onKeyDown={(e) => {\n        if (e.key === \"Escape\" && own) {\n          e.stopPropagation();\n          close(true);\n        }\n      }}\n    >\n      {children}\n\n      {/* Frosts the screen, and is the way out: a tap anywhere else\n          closes the menu. */}\n      <motion.div\n        aria-hidden\n        className=\"absolute inset-0 z-10 bg-background/60 backdrop-blur-md\"\n        initial={false}\n        animate={{ opacity: open ? 1 : 0 }}\n        transition={{ duration: open ? 0.3 : 0.2, ease: [0.23, 1, 0.32, 1] }}\n        style={{ pointerEvents: open ? \"auto\" : \"none\" }}\n        onClick={() => close(false)}\n      />\n\n      <div\n        role=\"menu\"\n        id={menuId}\n        aria-label=\"Actions\"\n        inert={!open}\n        className=\"absolute right-5 bottom-5 z-20\"\n      >\n        {actions.map((action, i) => {\n          // 0 for the bottom row, which opens first and closes last.\n          const fromBottom = last - i;\n          return (\n            <motion.button\n              key={action.label}\n              ref={i === 0 ? first : undefined}\n              type=\"button\"\n              role=\"menuitem\"\n              onClick={() => {\n                onAction?.(action.label);\n                close(true);\n              }}\n              className=\"absolute right-0 bottom-0 flex h-14 items-center gap-3 rounded-full pr-[15px] pl-3 text-[17px] font-medium whitespace-nowrap outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground\"\n              // Grows out of the icon end, where the button was.\n              style={{ transformOrigin: \"calc(100% - 28px) 50%\" }}\n              initial={false}\n              animate={\n                open\n                  ? {\n                      y: -fromBottom * STEP,\n                      scale: 1,\n                      opacity: 1,\n                      filter: \"blur(0px)\",\n                      transition: reduceMotion\n                        ? { duration: 0.15 }\n                        : { ...UNFOLD, delay: fromBottom * STAGGER },\n                    }\n                  : {\n                      y: reduceMotion ? -fromBottom * STEP : 0,\n                      scale: reduceMotion ? 1 : 0.35,\n                      opacity: 0,\n                      filter: reduceMotion ? \"blur(0px)\" : \"blur(6px)\",\n                      transition: reduceMotion\n                        ? { duration: 0.12 }\n                        : { ...FOLD, delay: (last - fromBottom) * STAGGER },\n                    }\n              }\n            >\n              {action.label}\n              <span\n                className=\"grid size-[26px] place-items-center\"\n                style={{ color: action.tone }}\n              >\n                {action.icon}\n              </span>\n            </motion.button>\n          );\n        })}\n      </div>\n\n      <div className=\"absolute right-5 bottom-5 z-20 size-14\">\n        {hint && <Hint text={hint} gone={opened || open} />}\n        <motion.button\n          ref={fab}\n          type=\"button\"\n          aria-label=\"Actions\"\n          aria-haspopup=\"menu\"\n          aria-expanded={open}\n          aria-controls={menuId}\n          onClick={() => {\n            setOwn(true);\n            setOpened(true);\n          }}\n          className=\"relative grid size-14 touch-manipulation place-items-center rounded-full bg-foreground text-background shadow-[0_8px_24px_-6px_rgb(0_0_0/0.35)] outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground\"\n          initial={false}\n          animate={\n            open\n              ? {\n                  scale: 0.5,\n                  opacity: 0,\n                  filter: reduceMotion ? \"blur(0px)\" : \"blur(4px)\",\n                }\n              : { scale: 1, opacity: 1, filter: \"blur(0px)\" }\n          }\n          whileTap={reduceMotion ? undefined : { scale: 0.92 }}\n          transition={\n            open\n              ? { duration: 0.18, ease: [0.23, 1, 0.32, 1] }\n              : { ...UNFOLD, delay: 0.08 }\n          }\n          style={{ pointerEvents: open ? \"none\" : \"auto\" }}\n        >\n          <svg\n            viewBox=\"0 0 16 16\"\n            className=\"size-[22px]\"\n            fill=\"none\"\n            stroke=\"currentColor\"\n            strokeWidth={2}\n            strokeLinecap=\"round\"\n            aria-hidden\n          >\n            <path d=\"M8 2.5v11M2.5 8h11\" />\n          </svg>\n        </motion.button>\n      </div>\n    </div>\n  );\n}\n\n// One pen stroke, never lifted: out from the note, a quick doodled loop,\n// down to the button, then both barbs of the head flicked out by doubling\n// back over the first, so the head arrives when the pen gets there.\nconst ARROW =\n  \"M3 9C14 3.5 29 3 40 10.5C48.5 16.5 49 28 41.5 28.8C34.5 29.6 35.2 18.6 44 19.8C53.5 21 59 33.5 61.2 47.8\" +\n  \"C58.8 44.6 55.8 42.6 52.4 41.8C55.8 42.6 58.8 44.6 61.2 47.8C62.6 44.2 64.4 40.6 67 38.2\";\n\n// Written above and to the left in red pen, with an arrow that curls down\n// to the button. It draws in once when it comes into view, and leaves for\n// good the first time the menu opens.\nfunction Hint({ text, gone }: { text: string; gone: boolean }) {\n  const ref = useRef<HTMLSpanElement>(null);\n  const [drawn, setDrawn] = useState(false);\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (!entry.isIntersecting) return;\n        setDrawn(true);\n        io.disconnect();\n      },\n      { threshold: 0.6 },\n    );\n    io.observe(el);\n    return () => io.disconnect();\n  }, []);\n\n  const stroke =\n    \"[stroke-dasharray:1] [stroke-dashoffset:1] transition-[stroke-dashoffset] delay-300 duration-[900ms] ease-[cubic-bezier(0.45,0,0.25,1)] group-data-[drawn=true]/hint:[stroke-dashoffset:0] motion-reduce:transition-none\";\n\n  return (\n    <span\n      ref={ref}\n      aria-hidden\n      data-drawn={drawn}\n      className={cn(\n        // Placed so the arrowhead lands just above the button, a little\n        // left of its middle: the head is at (61, 48) in the 80x60 arrow,\n        // which hangs 46px past the note's right edge and 4px below it.\n        \"group/hint pointer-events-none absolute right-[61px] bottom-full flex w-max flex-col items-end text-marker transition-[opacity,filter,translate] duration-300 ease-out\",\n        gone && \"-translate-y-1 opacity-0 blur-[2px]\",\n      )}\n    >\n      <span\n        className={cn(\n          hand.className,\n          \"translate-y-1 -rotate-6 text-[22px] leading-none opacity-0 transition-[opacity,translate] delay-100 duration-300 ease-out group-data-[drawn=true]/hint:translate-y-0 group-data-[drawn=true]/hint:opacity-100 motion-reduce:transition-none\",\n        )}\n      >\n        {text}\n      </span>\n      <svg\n        viewBox=\"0 0 80 60\"\n        className=\"mt-0.5 mr-[-46px] -mb-1 h-[60px] w-20 overflow-visible\"\n        fill=\"none\"\n        stroke=\"currentColor\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      >\n        <path d={ARROW} pathLength={1} strokeWidth={2.1} className={stroke} />\n        {/* A thinner pass a hair off the first: ink pooling where the nib\n            pressed, so the line isn't a uniform vector stroke. */}\n        <path\n          d={ARROW}\n          pathLength={1}\n          strokeWidth={1.1}\n          opacity={0.55}\n          transform=\"translate(0.6 0.4)\"\n          className={stroke}\n        />\n      </svg>\n    </span>\n  );\n}\n\nconst ICON = {\n  viewBox: \"0 0 24 24\",\n  className: \"size-[26px]\",\n  fill: \"none\",\n  stroke: \"currentColor\",\n  strokeWidth: 2.4,\n  strokeLinecap: \"round\",\n  strokeLinejoin: \"round\",\n  \"aria-hidden\": true,\n} as const;\n\n// Each action keeps its own colour, a step lighter in dark mode.\nconst ACTIONS: FabAction[] = [\n  {\n    label: \"Receive\",\n    tone: \"light-dark(oklch(0.66 0.19 42), oklch(0.74 0.16 45))\",\n    icon: (\n      <svg {...ICON}>\n        <path d=\"M12 4v15M6 13l6 6 6-6\" />\n      </svg>\n    ),\n  },\n  {\n    label: \"Send\",\n    tone: \"light-dark(oklch(0.55 0.22 290), oklch(0.7 0.17 290))\",\n    icon: (\n      <svg {...ICON} strokeWidth={2}>\n        <path\n          d=\"M21 3 10.5 13.5M21 3l-6.5 18-4-7.5L3 9.5 21 3Z\"\n          fill=\"currentColor\"\n          fillOpacity={0.9}\n        />\n      </svg>\n    ),\n  },\n  {\n    label: \"Swap\",\n    tone: \"light-dark(oklch(0.57 0.2 258), oklch(0.72 0.15 255))\",\n    icon: (\n      <svg {...ICON}>\n        <path d=\"M4 9.5h13.5L14 6M20 14.5H6.5L10 18\" />\n      </svg>\n    ),\n  },\n];\n\nconst Muted = ({ children }: { children: React.ReactNode }) => (\n  <span className=\"text-muted-foreground\">{children}</span>\n);\n\n// A wallet home screen, so the button has something real to cover.\nfunction WalletScreen() {\n  return (\n    <div className=\"flex h-full flex-col p-5 pt-8\">\n      <p className=\"px-1 text-[13px] text-muted-foreground\">Good evening, Yash</p>\n      <div className=\"mt-4 grid grid-cols-2 gap-2.5\">\n        <div className=\"rounded-[26px] bg-muted p-4\">\n          <p className=\"text-[12px] text-muted-foreground\">Spending</p>\n          <p className=\"mt-6 text-[22px] font-semibold tracking-tight tabular-nums\">\n            $1,265<Muted>.75</Muted>\n          </p>\n        </div>\n        <div className=\"rounded-[26px] bg-muted p-4\">\n          <p className=\"text-[12px] text-muted-foreground\">Savings</p>\n          <p className=\"mt-6 text-[22px] font-semibold tracking-tight tabular-nums\">\n            $3,498<Muted>.41</Muted>\n          </p>\n        </div>\n      </div>\n      <div className=\"relative mt-2.5 rounded-[26px] bg-muted p-4\">\n        <span\n          aria-hidden\n          className=\"block size-10 rounded-xl\"\n          // A brand tile: the gradient is the material, not a theme colour.\n          style={{\n            background:\n              \"linear-gradient(140deg, oklch(0.72 0.14 245), oklch(0.5 0.2 262))\",\n          }}\n        />\n        <span aria-hidden className=\"absolute top-4 right-4 text-muted-foreground\">\n          <svg\n            viewBox=\"0 0 16 16\"\n            className=\"size-4\"\n            fill=\"none\"\n            stroke=\"currentColor\"\n            strokeWidth={1.5}\n            strokeLinecap=\"round\"\n          >\n            <path d=\"m4 4 8 8M12 4l-8 8\" />\n          </svg>\n        </span>\n        <p className=\"mt-10 text-[19px] leading-snug font-semibold tracking-tight\">\n          Earn up to\n          <br />\n          7.80% APY\n        </p>\n      </div>\n      <div className=\"mt-auto flex items-center gap-5 px-2 pb-3 text-muted-foreground\">\n        <svg\n          viewBox=\"0 0 16 16\"\n          className=\"size-[22px]\"\n          fill=\"none\"\n          stroke=\"currentColor\"\n          strokeWidth={1.5}\n          strokeLinecap=\"round\"\n          aria-hidden\n        >\n          <circle cx=\"8\" cy=\"8\" r=\"6\" />\n          <path d=\"M8 4.75V8l2.25 1.5\" />\n        </svg>\n        <svg\n          viewBox=\"0 0 16 16\"\n          className=\"size-[22px]\"\n          fill=\"none\"\n          stroke=\"currentColor\"\n          strokeWidth={1.5}\n          strokeLinecap=\"round\"\n          strokeLinejoin=\"round\"\n          aria-hidden\n        >\n          <circle cx=\"8\" cy=\"8\" r=\"2\" />\n          <path d=\"M8 1.75v1.5M8 12.75v1.5M1.75 8h1.5M12.75 8h1.5M3.6 3.6l1.05 1.05M11.35 11.35l1.05 1.05M3.6 12.4l1.05-1.05M11.35 4.65l1.05-1.05\" />\n        </svg>\n      </div>\n    </div>\n  );\n}\n\nexport default function FloatingActionMenuDemo() {\n  return (\n    <FloatingActionMenu actions={ACTIONS}>\n      <WalletScreen />\n    </FloatingActionMenu>\n  );\n}\n"},{"path":"lib/preview-play.ts","type":"registry:lib","target":"lib/preview-play.ts","content":"\"use client\";\n\nimport { createContext, useContext } from \"react\";\n\n// Lets a demo know whether it's showing in an index card, and whether that\n// card is hovered or focused. Demos use it to act themselves out on hover:\n// a star button stars itself, a checkbox scribbles, and so on.\n//\n// null: not in a preview (the component's own page, or real use).\n// false: in a preview, at rest.\n// true: in a preview, being hovered or focused: play the show.\nexport const PreviewPlayContext = createContext<boolean | null>(null);\n\nexport function usePreviewPlay() {\n  return useContext(PreviewPlayContext);\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"marker":"#d93d31","shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"marker":"#ff6b5f","shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"color-marker":"var(--marker)","shadow-raised":"var(--shadow-raised)"}},"categories":["buttons"],"docs":"From ui lab: https://lab.xevrion.dev/lab/floating-action-menu"}