{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"announcement-banner","type":"registry:component","title":"Announcement banner","description":"Dismissing folds the bar away and the page slides up to fill the gap.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"components/announcement-banner.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE = \"cubic-bezier(0.23, 1, 0.32, 1)\";\n// The iOS drawer curve for the height: quick to start, long soft settle, so\n// the page below glides rather than lurches.\nconst DRAWER = \"cubic-bezier(0.32, 0.72, 0, 1)\";\n\nexport function AnnouncementBanner({\n  open,\n  onDismiss,\n  icon,\n  link,\n  label = \"Announcement\",\n  className,\n  children,\n}: {\n  open: boolean;\n  onDismiss: () => void;\n  icon?: React.ReactNode;\n  link?: { href: string; label: string };\n  label?: string;\n  className?: string;\n  children: React.ReactNode;\n}) {\n  const reduceMotion = useReducedMotion();\n  // The very first appearance slides down from above; later ones (after a\n  // dismiss) reverse the dismissal instead: open the space, then fade in.\n  const [shown, setShown] = useState(open);\n  const [firstShow, setFirstShow] = useState(true);\n  if (open !== shown) {\n    setShown(open);\n    if (!open) setFirstShow(false);\n  }\n\n  const rows = reduceMotion\n    ? \"none\"\n    : open\n      ? `grid-template-rows 280ms ${DRAWER}`\n      : // Waits for the content to mostly fade, so the text never gets\n        // squeezed while it's still readable.\n        `grid-template-rows 240ms ${DRAWER} 80ms`;\n\n  const content = open\n    ? reduceMotion\n      ? `opacity 200ms ${EASE}`\n      : firstShow\n        ? `translate 280ms ${DRAWER}, opacity 200ms ${EASE}, filter 200ms ${EASE}`\n        : // Starts once the space has mostly opened.\n          `opacity 200ms ${EASE} 120ms, filter 200ms ${EASE} 120ms`\n    : `opacity 120ms ${EASE}, filter 120ms ${EASE}`;\n\n  return (\n    <div\n      className=\"grid\"\n      style={{ gridTemplateRows: open ? \"1fr\" : \"0fr\", transition: rows }}\n    >\n      <div className=\"min-h-0 overflow-hidden\">\n        <section\n          aria-label={label}\n          inert={!open}\n          tabIndex={-1}\n          className={cn(\n            \"flex h-11 items-center gap-2.5 bg-foreground pr-1.5 pl-4 text-sm text-background outline-hidden\",\n            className,\n          )}\n          style={{\n            opacity: open ? 1 : 0,\n            filter: open || reduceMotion ? \"blur(0px)\" : \"blur(2px)\",\n            translate:\n              !open && firstShow && !reduceMotion ? \"0 -100%\" : \"0 0\",\n            transition: content,\n          }}\n        >\n          {icon && (\n            <span aria-hidden className=\"flex shrink-0\">\n              {icon}\n            </span>\n          )}\n          <p className=\"min-w-0 truncate\">{children}</p>\n          {link && (\n            <a\n              href={link.href}\n              className=\"shrink-0 rounded-sm font-medium underline decoration-background/40 underline-offset-[3px] outline-hidden transition-[text-decoration-color] duration-150 ease-out hover:decoration-background focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-background\"\n            >\n              {link.label}\n            </a>\n          )}\n          <button\n            type=\"button\"\n            aria-label=\"Dismiss announcement\"\n            onClick={onDismiss}\n            className={cn(\n              \"relative ml-auto flex size-8 shrink-0 touch-manipulation items-center justify-center rounded-full text-background/70 outline-hidden transition-[scale,color,background-color] duration-150 ease-out select-none hover:bg-background/15 hover:text-background focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-background active:scale-[0.96] motion-reduce:transition-[color,background-color]\",\n              // Grows the hit area to 40px without growing the circle.\n              \"after:absolute after:-inset-1 after:rounded-full\",\n            )}\n          >\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              aria-hidden\n            >\n              <path d=\"m4.5 4.5 7 7M11.5 4.5l-7 7\" />\n            </svg>\n          </button>\n        </section>\n      </div>\n    </div>\n  );\n}\n\nconst STORAGE_KEY = \"ui-lab:announcement-dismissed\";\n\nfunction readDismissed() {\n  try {\n    return sessionStorage.getItem(STORAGE_KEY) === \"1\";\n  } catch {\n    return false;\n  }\n}\n\nfunction writeDismissed(dismissed: boolean) {\n  try {\n    if (dismissed) sessionStorage.setItem(STORAGE_KEY, \"1\");\n    else sessionStorage.removeItem(STORAGE_KEY);\n  } catch {\n    // Private windows can refuse storage; the banner just won't remember.\n  }\n}\n\nexport default function AnnouncementBannerDemo() {\n  const frameRef = useRef<HTMLDivElement>(null);\n  const againRef = useRef<HTMLButtonElement>(null);\n  // Unknown (null) on the server and the first client render, then decided,\n  // so the entrance plays and a remembered dismissal never mismatches\n  // hydration. While unknown, neither the banner nor \"Show again\" shows.\n  const [open, setOpen] = useState<boolean | null>(null);\n  // Focus can only move once the target has lost its inert attribute.\n  const pendingFocus = useRef<HTMLElement | null>(null);\n\n  useEffect(() => {\n    pendingFocus.current?.focus({ preventScroll: true });\n    pendingFocus.current = null;\n  }, [open]);\n\n  useEffect(() => {\n    const frame = requestAnimationFrame(() => {\n      const el = frameRef.current;\n      if (!el) return;\n      // Reading layout commits the closed state first, so the opening is a\n      // real transition even when this mounts without a server render.\n      void el.offsetHeight;\n      // The index preview always shows it; a dismissal there couldn't be undone.\n      setOpen(el.closest(\"[inert]\") !== null || !readDismissed());\n    });\n    return () => cancelAnimationFrame(frame);\n  }, []);\n\n  const dismiss = () => {\n    const hadFocus = frameRef.current\n      ?.querySelector(\"section\")\n      ?.contains(document.activeElement);\n    setOpen(false);\n    writeDismissed(true);\n    // The dismiss button is going away; hand focus to the one way back.\n    if (hadFocus) pendingFocus.current = againRef.current;\n  };\n\n  const restore = () => {\n    setOpen(true);\n    writeDismissed(false);\n    pendingFocus.current = frameRef.current?.querySelector(\"section\") ?? null;\n  };\n\n  return (\n    <div\n      ref={frameRef}\n      className=\"relative h-[300px] w-[min(520px,100%)] overflow-hidden rounded-2xl bg-background shadow-raised\"\n    >\n      <AnnouncementBanner\n        open={open === true}\n        onDismiss={dismiss}\n        link={{ href: \"#changelog\", label: \"See what changed\" }}\n        icon={\n          <svg\n            viewBox=\"0 0 16 16\"\n            className=\"size-4\"\n            fill=\"none\"\n            stroke=\"currentColor\"\n            strokeWidth={1.5}\n            strokeLinejoin=\"round\"\n          >\n            <path d=\"M9 1.75 3.5 9h4l-.75 5.25L12.5 7h-4L9 1.75Z\" />\n          </svg>\n        }\n      >\n        Deploys now build twice as fast.\n      </AnnouncementBanner>\n\n      {/* In normal flow under the banner, so it rides the same height\n          transition up and down with no animation of its own. */}\n      <div className=\"flex h-12 items-center gap-5 border-b border-border px-5 text-sm\">\n        <span className=\"font-semibold text-foreground\">Northwind</span>\n        <span className=\"text-foreground\">Overview</span>\n        <span className=\"hidden text-muted-foreground sm:inline\">Deployments</span>\n        <span className=\"text-muted-foreground\">Settings</span>\n        <span aria-hidden className=\"ml-auto size-7 rounded-full bg-muted\" />\n      </div>\n      <div className=\"px-5 pt-5\">\n        <h3 className=\"text-lg font-semibold tracking-tight text-foreground\">\n          Overview\n        </h3>\n        <p className=\"mt-1 text-sm text-muted-foreground\">\n          Three deploys today, all healthy.\n        </p>\n        <div className=\"mt-4 grid grid-cols-3 gap-2\">\n          {[\n            [\"Requests\", \"1.2M\"],\n            [\"Latency\", \"84 ms\"],\n            [\"Errors\", \"0.02%\"],\n          ].map(([name, value]) => (\n            <div key={name} className=\"rounded-xl bg-muted px-3 py-2.5\">\n              <p className=\"text-xs text-muted-foreground\">{name}</p>\n              <p className=\"mt-0.5 text-[15px] font-medium text-foreground tabular-nums\">\n                {value}\n              </p>\n            </div>\n          ))}\n        </div>\n      </div>\n\n      {/* Pinned to the frame rather than the page flow, so it stays exactly\n          under the cursor while the banner reopens and pushes content down.\n          Arrives after the banner has closed, leaves at once. */}\n      <button\n        ref={againRef}\n        type=\"button\"\n        onClick={restore}\n        inert={open !== false}\n        className={cn(\n          \"absolute right-3 bottom-3 flex h-9 touch-manipulation items-center rounded-full bg-muted px-3.5 text-sm font-medium text-foreground shadow-raised outline-hidden transition-[opacity,filter,translate,scale] ease-[cubic-bezier(0.23,1,0.32,1)] select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[opacity]\",\n          open !== false\n            ? \"translate-y-1 opacity-0 blur-[2px] duration-100 motion-reduce:translate-y-0 motion-reduce:blur-[0px]\"\n            : \"translate-y-0 opacity-100 blur-[0px] delay-200 duration-200\",\n        )}\n      >\n        Show again\n      </button>\n    </div>\n  );\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":{"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":{"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":{"shadow-raised":"var(--shadow-raised)"}},"categories":["feedback"],"docs":"From ui lab: https://lab.xevrion.dev/lab/announcement-banner"}