{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "view-transition",
  "type": "registry:component",
  "title": "View Transition",
  "description": "View transitions two ways: wrappers over React's own <ViewTransition> (a shared element that morphs between two places, a boundary that cross-fades what changes inside it), and helpers for the browser's API (run an update as a transition, name an element, a link that navigates inside a transition).",
  "categories": [
    "motion"
  ],
  "dependencies": [
    "radix-ui"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/motion/view-transition.tsx",
      "type": "registry:component",
      "target": "components/motion/view-transition.tsx",
      "content": "\"use client\";\n/**\n * @name view-transition\n * @category motion\n * @description View transitions two ways: wrappers over React's own <ViewTransition> (a shared element that morphs between two places, a boundary that cross-fades what changes inside it), and helpers for the browser's API (run an update as a transition, name an element, a link that navigates inside a transition).\n * @when Moving between pages or between two states of a view, where an element in the old view should become one in the new. Use the React wrappers (SharedElement, TransitionBoundary) when the change is a React transition: a state update inside startTransition, a Suspense boundary revealing, a navigation by a router that uses transitions (Next.js). React then starts the browser's transition itself. Use the browser helpers (startViewTransition, ViewTransitionName, TransitionLink) when nothing in the tree uses React's component: a synchronous update, a router that does not use transitions, an element that stays mounted while the view around it changes. Do not mix the two for one update: React's documentation says not to call document.startViewTransition yourself around an update it is already animating. Both degrade to an instant change where the API is missing, and the theme switches them off under reduced motion. For a shared element that must be interrupted mid-way use layout-transition; for a simple enter on route change use page-transition.\n * @example\n * // React's way: the same name in two places; the update runs in a transition.\n * const [open, setOpen] = useState(false);\n * {open ? (\n *   <SharedElement name=\"atlas-cover\"><img src=\"/atlas.jpg\" alt=\"Atlas\" className=\"w-full\" /></SharedElement>\n * ) : (\n *   <button onClick={() => startTransition(() => setOpen(true))}>\n *     <SharedElement name=\"atlas-cover\"><img src=\"/atlas.jpg\" alt=\"Atlas\" className=\"size-24\" /></SharedElement>\n *   </button>\n * )}\n *\n * // The browser's way. A link that navigates inside a view transition; in Next.js pass router.push:\n * <TransitionLink href=\"/work/atlas\" navigate={router.push}>Atlas</TransitionLink>\n * // The same name on the list page and on the detail page: the image morphs from one to the other.\n * <ViewTransitionName name=\"work-atlas-cover\" asChild>\n *   <img src=\"/atlas.jpg\" alt=\"Atlas\" />\n * </ViewTransitionName>\n * // A state change in place. flushSync (from react-dom) makes React apply it before the browser's snapshot.\n * <Button onClick={() => startViewTransition(() => flushSync(() => setLayout(\"grid\")))}>Grid</Button>\n */\nimport { Slot } from \"radix-ui\";\nimport { useLayoutEffect, useRef, useTransition, ViewTransition, type ComponentProps, type CSSProperties, type MouseEvent, type ViewTransitionProps } from \"react\";\nimport { cn } from \"@/lib/cn\";\n\n/** How long a navigation may hold the page still before the transition goes ahead without it, in milliseconds. */\nconst NAVIGATION_TIMEOUT = 1500;\n\n/**\n * Runs `update` (which changes the page) inside a view transition and resolves when the transition has\n * finished. Where the API is missing, or the visitor has asked for reduced motion, it runs `update` directly.\n * The durations and easing of the transition are set by the theme (`::view-transition-*` in rivier-ui.css).\n * If `update` returns a promise, the browser holds the old view until it resolves, so resolve it only once\n * the new view is in the document.\n */\nexport async function startViewTransition(update: () => void | Promise<void>): Promise<void> {\n  const supported = typeof document !== \"undefined\" && typeof document.startViewTransition === \"function\";\n  if (!supported || window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n    await update();\n    return;\n  }\n  await document.startViewTransition(update).finished;\n}\n\n/**\n * The style that names an element for view transitions. An element with the same name in the old and the new\n * view morphs from one to the other. A name must be unique among the elements on screen at one time; anything\n * that is not valid in a CSS identifier is replaced.\n */\nexport function viewTransitionName(name: string): CSSProperties {\n  return { viewTransitionName: identifierOf(name) };\n}\n\n/** A name as a CSS identifier: what is not valid in one is replaced. */\nfunction identifierOf(name: string): string {\n  const identifier = name.trim().replace(/[^a-zA-Z0-9_-]+/g, \"-\");\n  return /^[a-zA-Z_]/.test(identifier) ? identifier : `vt-${identifier}`;\n}\n\nexport type SharedElementProps = Omit<ViewTransitionProps, \"name\"> & {\n  /** The shared name. Use the same one in the other place; it must be unique among what is on screen at one time. */\n  name: string;\n};\n\n/**\n * React's <ViewTransition> with a name. When one SharedElement leaves the tree and another with the same\n * name enters it in the same React transition (startTransition, a Suspense reveal, a navigation), the\n * browser morphs the first into the second. It adds no element: the name is put on the child's DOM node.\n * Updates outside a transition (a plain setState) are applied at once, with no animation.\n */\nexport function SharedElement({ name, children, ...props }: SharedElementProps) {\n  return (\n    <ViewTransition name={identifierOf(name)} {...props}>\n      {children}\n    </ViewTransition>\n  );\n}\n\n/**\n * React's <ViewTransition> without a name: what enters, leaves or changes inside it during a React\n * transition cross-fades instead of swapping. `enter`, `exit`, `update` and `share` take a\n * view-transition-class (or \"none\") to style or switch off each case.\n */\nexport function TransitionBoundary(props: ViewTransitionProps) {\n  return <ViewTransition {...props} />;\n}\n\nexport type ViewTransitionNameProps = ComponentProps<\"div\"> & {\n  /** The shared name. Use the same one on the matching element of the other view (\"post-42-cover\"). */\n  name: string;\n  /** Put the name on the child element instead of a wrapping div. */\n  asChild?: boolean;\n};\n\n/** Names an element for view transitions: a div, or with `asChild` the element passed in. */\nexport function ViewTransitionName({ className, name, asChild = false, style, ...props }: ViewTransitionNameProps) {\n  const Comp = asChild ? Slot.Root : \"div\";\n  return <Comp data-slot=\"view-transition-name\" className={cn(className)} style={{ ...style, ...viewTransitionName(name) }} {...props} />;\n}\n\nexport type TransitionLinkProps = Omit<ComponentProps<\"a\">, \"href\"> & {\n  href: string;\n  /** Performs the client navigation: `router.push` in Next.js, `navigate` in React Router. May return a promise. */\n  navigate: (href: string) => void | Promise<void>;\n};\n\n/**\n * An anchor that navigates on the client inside a view transition. It is a real link: modified clicks, new\n * tabs, downloads and other origins are left to the browser. It has no dependency on a router: the old view\n * is held until React has finished the transition that `navigate` starts (or the promise it returns), and\n * never for longer than a moment.\n */\nexport function TransitionLink({ className, href, navigate, onClick, ...props }: TransitionLinkProps) {\n  const [pending, startTransition] = useTransition();\n  const settle = useRef<(() => void) | null>(null);\n\n  // The navigation has been committed (pending is false again) or this link has left the page with the old\n  // view: either way the new view is in the document, so let the browser take its snapshot.\n  useLayoutEffect(() => {\n    if (!pending) settle.current?.();\n  }, [pending]);\n  useLayoutEffect(() => () => settle.current?.(), []);\n\n  const handleClick = (event: MouseEvent<HTMLAnchorElement>): void => {\n    onClick?.(event);\n    const anchor = event.currentTarget;\n    if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;\n    if ((anchor.target && anchor.target !== \"_self\") || anchor.hasAttribute(\"download\") || anchor.origin !== window.location.origin) return;\n    event.preventDefault();\n    void startViewTransition(\n      () =>\n        new Promise<void>((resolve) => {\n          const timer = window.setTimeout(() => settle.current?.(), NAVIGATION_TIMEOUT);\n          settle.current = () => {\n            window.clearTimeout(timer);\n            settle.current = null;\n            resolve();\n          };\n          startTransition(async () => {\n            await navigate(href);\n          });\n        }),\n    ).catch(() => {});\n  };\n\n  return <a data-slot=\"transition-link\" className={cn(className)} href={href} onClick={handleClick} {...props} />;\n}\n"
    }
  ],
  "meta": {
    "category": "motion",
    "when": "Moving between pages or between two states of a view, where an element in the old view should become one in the new. Use the React wrappers (SharedElement, TransitionBoundary) when the change is a React transition: a state update inside startTransition, a Suspense boundary revealing, a navigation by a router that uses transitions (Next.js). React then starts the browser's transition itself. Use the browser helpers (startViewTransition, ViewTransitionName, TransitionLink) when nothing in the tree uses React's component: a synchronous update, a router that does not use transitions, an element that stays mounted while the view around it changes. Do not mix the two for one update: React's documentation says not to call document.startViewTransition yourself around an update it is already animating. Both degrade to an instant change where the API is missing, and the theme switches them off under reduced motion. For a shared element that must be interrupted mid-way use layout-transition; for a simple enter on route change use page-transition.",
    "example": "// React's way: the same name in two places; the update runs in a transition.\nconst [open, setOpen] = useState(false);\n{open ? (\n  <SharedElement name=\"atlas-cover\"><img src=\"/atlas.jpg\" alt=\"Atlas\" className=\"w-full\" /></SharedElement>\n) : (\n  <button onClick={() => startTransition(() => setOpen(true))}>\n    <SharedElement name=\"atlas-cover\"><img src=\"/atlas.jpg\" alt=\"Atlas\" className=\"size-24\" /></SharedElement>\n  </button>\n)}\n\n// The browser's way. A link that navigates inside a view transition; in Next.js pass router.push:\n<TransitionLink href=\"/work/atlas\" navigate={router.push}>Atlas</TransitionLink>\n// The same name on the list page and on the detail page: the image morphs from one to the other.\n<ViewTransitionName name=\"work-atlas-cover\" asChild>\n  <img src=\"/atlas.jpg\" alt=\"Atlas\" />\n</ViewTransitionName>\n// A state change in place. flushSync (from react-dom) makes React apply it before the browser's snapshot.\n<Button onClick={() => startViewTransition(() => flushSync(() => setLayout(\"grid\")))}>Grid</Button>",
    "client": true
  }
}
