{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "view-transition",
  "type": "registry:ui",
  "title": "View Transition",
  "description": "The browser's View Transitions API, made safe to reach for: a card grows into its detail page, a list reorders in place, and a page change cross-fades instead of blinking. Interlace adds only what the API leaves to every app: a fallback where it is…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "navigation",
    "effect"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/effects/view-transition.tsx",
      "target": "components/ui/effects/view-transition.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/view-transition v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/view-transition\n// What changed since: https://ds.interlace.tools/c/view-transition#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — View transitions: startViewTransition, useViewTransition, Morph, PageTransitions\n *\n * The browser's View Transitions API, made safe to reach for: a card grows\n * into its detail page, a list reorders in place, and a page change\n * cross-fades instead of blinking. Interlace adds only what the API leaves\n * to every app: a fallback where it is missing, nothing at all under reduced\n * motion, React state flushed inside the transition, and names that are\n * always valid.\n *\n * ## Anatomy\n *\n *   startViewTransition(update, options?)   (plain function: any framework)\n *   useViewTransition()                     → (update, options?) => void, with flushSync\n *   Morph name=\"card-42\"                    (gives its one child a view-transition-name)\n *   PageTransitions                         (a <style>: page changes between documents)\n *\n * ## A card that grows into its page\n *\n * Wrap the card's image (or the whole card) in `<Morph name={`item-${id}`}>`\n * and the detail page's hero in the same `<Morph>`. Change the view inside\n * `useViewTransition()` (same document) or navigate between documents with\n * `PageTransitions` on both pages: the browser moves and scales the one into\n * the other. A name must be unique on the page at any moment; `Morph`\n * cleans it into a valid identifier (`viewTransitionName`).\n *\n * ## Motion\n *\n * The page itself: the old view fades out (180ms) and the new one rises in\n * by half a rem (300ms, `--ease-enter`); a named element moves and scales\n * between its two boxes (`--duration-slow`). Opacity and transform only.\n * Under `prefers-reduced-motion: reduce` there is no transition at all: the\n * update runs straight away, and `PageTransitions` turns cross-document\n * transitions off. Where the browser has no View Transitions, the update\n * simply runs.\n *\n * | Rule | Concept                   | Where in this file                              |\n * | ---- | ------------------------- | ----------------------------------------------- |\n * | R6   | data-slot per part        | `data-slot=\"morph\"` on the child; page-transitions |\n * | R25  | Client component          | transitions run in the browser                  |\n * | R26  | A11y                      | off under reduced motion; content never hidden  |\n */\n\nimport { flushSync } from 'react-dom';\n\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\n\nexport interface ViewTransitionOptions {\n  /** View transition types (`:active-view-transition-type(forward)`), where supported. */\n  types?: string[];\n  /** Run the update without a transition. */\n  skip?: boolean;\n}\n\ntype DocumentWithTransitions = Document & {\n  startViewTransition?: (\n    arg: (() => void | Promise<void>) | { update: () => void | Promise<void>; types?: string[] },\n  ) => ViewTransition;\n};\n\n/** Whether this browser can run a same-document view transition. */\nexport function supportsViewTransitions(): boolean {\n  return (\n    typeof document !== 'undefined' && typeof (document as DocumentWithTransitions).startViewTransition === 'function'\n  );\n}\n\nfunction prefersReducedMotion(): boolean {\n  return typeof window !== 'undefined' && typeof window.matchMedia === 'function'\n    ? window.matchMedia('(prefers-reduced-motion: reduce)').matches\n    : false;\n}\n\n/**\n * Runs `update` inside a view transition where the browser has one and the\n * reader has not asked for less motion; otherwise runs it straight away.\n * Returns the transition, or null when there was none.\n */\nexport function startViewTransition(\n  update: () => void | Promise<void>,\n  options: ViewTransitionOptions = {},\n): ViewTransition | null {\n  const doc = (globalThis as { document?: DocumentWithTransitions }).document;\n  if (options.skip || !doc?.startViewTransition || prefersReducedMotion()) {\n    void update();\n    return null;\n  }\n  if (options.types?.length) {\n    try {\n      return doc.startViewTransition({ update, types: options.types });\n    } catch {\n      // Older engines take only a callback.\n    }\n  }\n  return doc.startViewTransition(update);\n}\n\n/**\n * `startViewTransition` for React state: the update is flushed inside the\n * transition, so the browser captures the new view, not the old one.\n */\nexport function useViewTransition(): (update: () => void, options?: ViewTransitionOptions) => ViewTransition | null {\n  const reduced = useReducedMotion();\n  return React.useCallback(\n    (update: () => void, options: ViewTransitionOptions = {}) =>\n      startViewTransition(() => flushSync(update), { ...options, skip: options.skip || reduced }),\n    [reduced],\n  );\n}\n\n/** A valid `view-transition-name` from any string: letters, digits, `-` and `_`, not starting with a digit. */\nexport function viewTransitionName(raw: string): string {\n  const cleaned = raw\n    .normalize('NFKD')\n    .replace(/[^\\w-]+/g, '-')\n    .replace(/-+/g, '-')\n    .replace(/^-|-$/g, '');\n  const name = /^(?:\\d|-\\d|--)/.test(cleaned) || !cleaned ? `vt-${cleaned}` : cleaned;\n  // `none` and `auto` are keywords, not names.\n  return name === 'none' || name === 'auto' ? `vt-${name}` : name;\n}\n\nexport interface MorphProps {\n  /** Shared by the element on both views. Unique on the page at any moment. */\n  name: string;\n  /** Leave the name off, e.g. on every card but the one being opened. @default false */\n  disabled?: boolean;\n  /** One element that takes a `style` prop. */\n  children: React.ReactElement<{ style?: React.CSSProperties; 'data-slot'?: string }>;\n}\n\n/**\n * Names its one child for view transitions, so the same name on the next\n * view morphs from this box into that one.\n */\nfunction Morph({ name, disabled = false, children }: MorphProps) {\n  const child = React.Children.only(children);\n  if (disabled) return child;\n  return React.cloneElement(child, {\n    'data-slot': child.props['data-slot'] ?? 'morph',\n    style: { ...child.props.style, viewTransitionName: viewTransitionName(name) },\n  });\n}\nMorph.displayName = 'Morph';\n\nexport interface PageTransitionsProps {\n  /** Turn on transitions between documents (multi-page sites). @default true */\n  crossDocument?: boolean;\n  /** The page's own fade and rise. 'none' leaves only named elements moving. @default 'rise' */\n  page?: 'rise' | 'fade' | 'none';\n}\n\nconst PAGE: Record<NonNullable<PageTransitionsProps['page']>, string> = {\n  rise:\n    '::view-transition-old(root){animation:ilx-vt-out 180ms var(--ease-exit,ease-in) both}' +\n    '::view-transition-new(root){animation:ilx-vt-in 300ms var(--ease-enter,ease-out) both}',\n  fade:\n    '::view-transition-old(root){animation:ilx-vt-fade-out 180ms var(--ease-exit,ease-in) both}' +\n    '::view-transition-new(root){animation:ilx-vt-fade-in 300ms var(--ease-enter,ease-out) both}',\n  none: '::view-transition-old(root),::view-transition-new(root){animation:none}',\n};\n\n/** The CSS `PageTransitions` renders, for a stylesheet of your own. */\nexport function pageTransitionsCss({ crossDocument = true, page = 'rise' }: PageTransitionsProps = {}): string {\n  return (\n    '@media (prefers-reduced-motion:no-preference){' +\n    (crossDocument ? '@view-transition{navigation:auto}' : '') +\n    '::view-transition-group(*){animation-duration:var(--duration-slow,400ms);animation-timing-function:var(--ease-enter,ease-out)}' +\n    PAGE[page] +\n    '}' +\n    '@media (prefers-reduced-motion:reduce){::view-transition-group(*),::view-transition-old(*),::view-transition-new(*){animation:none!important}}' +\n    '@keyframes ilx-vt-out{to{opacity:0;transform:translateY(-0.25rem)}}' +\n    '@keyframes ilx-vt-in{from{opacity:0;transform:translateY(0.5rem)}}' +\n    '@keyframes ilx-vt-fade-out{to{opacity:0}}' +\n    '@keyframes ilx-vt-fade-in{from{opacity:0}}'\n  );\n}\n\n/**\n * The page-level look of view transitions, once per page (in the root\n * layout). Server-safe: it renders a `<style>` and nothing else.\n */\nfunction PageTransitions(props: PageTransitionsProps) {\n  return <style data-slot=\"page-transitions\">{pageTransitionsCss(props)}</style>;\n}\nPageTransitions.displayName = 'PageTransitions';\n\nexport { Morph, PageTransitions };\n"
    }
  ],
  "meta": {
    "tier": "effect",
    "client": true,
    "minViewport": null,
    "loading": false,
    "version": "1.0.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/view-transition\n\nInstalled to `components/ui/effects/view-transition.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/effects/view-transition';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/view-transition\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
