{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pinned-word",
  "title": "Pinned Word",
  "description": "A serif italic word is pinned in a sentence while the rest changes, 'Own every video frame.'.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/pinned-word/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadSerif } from \"@remotion/google-fonts/InstrumentSerif\";\nimport { loadFont as loadSans } from \"@remotion/google-fonts/SchibstedGrotesk\";\nimport type React from \"react\";\nimport { useEffect, useId, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst sans = loadSans(\"normal\", { weights: [\"500\"], subsets: [\"latin\"] });\nconst serif = loadSerif(\"italic\", { weights: [\"400\"], subsets: [\"latin\"] });\n\nexport interface PinnedWordProps {\n  lines?: string[];\n  word?: string;\n  hold?: number;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst MESH = [\"#ecdcf2\", \"#8796ee\", \"#2e45b7\", \"#0e1645\"] as const;\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0B\",\n    ink: \"#F4F3F0\",\n    word: (a: string) =>\n      `linear-gradient(100deg, ${MESH[0]} 0%, ${MESH[1]} 22%, color-mix(in oklab, ${a} 80%, ${MESH[1]}) 40%, ${MESH[0]} 50%, ${MESH[1]} 62%, color-mix(in oklab, ${a} 80%, ${MESH[1]}) 80%, ${MESH[0]} 100%)`,\n    ground: [\n      `radial-gradient(60% 50% at 50% 118%, color-mix(in oklab, ${MESH[2]} 38%, transparent) 0%, color-mix(in oklab, ${MESH[3]} 55%, transparent) 50%, transparent 80%)`,\n    ].join(\", \"),\n    grain: 0.05,\n  },\n  light: {\n    bg: \"#F5F4F0\",\n    ink: \"#111113\",\n    word: (a: string) =>\n      `linear-gradient(100deg, ${MESH[3]} 0%, ${MESH[2]} 22%, ${a} 40%, #7f6bd6 50%, ${MESH[2]} 62%, ${a} 80%, ${MESH[3]} 100%)`,\n    ground: `radial-gradient(60% 46% at 50% 120%, color-mix(in oklab, ${MESH[1]} 26%, transparent) 0%, transparent 78%)`,\n    grain: 0.035,\n  },\n} as const;\n\nconst SANS_TRACK = -0.025;\nconst PIN_SCALE = 1.8;\n\nconst INTRO_WORD = 0;\nconst INTRO_LINE = 8;\nconst FIRST_FLICK = 40;\nconst FLICK = 18;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_FLICK = Easing.bezier(0.3, 0, 0.08, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Split {\n  left: string;\n  token: string;\n  right: string;\n  found: boolean;\n}\n\nfunction split(line: string, word: string): Split {\n  const at = word ? line.toLowerCase().indexOf(word.toLowerCase()) : -1;\n  if (at < 0) return { left: line, token: \"\", right: \"\", found: false };\n  return {\n    left: line.slice(0, at),\n    token: line.slice(at, at + word.length),\n    right: line.slice(at + word.length),\n    found: true,\n  };\n}\n\ninterface Widths {\n  left: number;\n  token: number;\n  right: number;\n}\n\nfunction measure(parts: Split[]): Widths[] {\n  const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n  if (!ctx) {\n    return parts.map((p) => ({\n      left: p.left.length * 0.5,\n      token: p.token.length * 0.45 * PIN_SCALE,\n      right: p.right.length * 0.5,\n    }));\n  }\n  const sansW = (s: string) => {\n    if (!s) return 0;\n    ctx.font = `500 100px \"${sans.fontFamily}\"`;\n    return ctx.measureText(s).width / 100 + SANS_TRACK * Array.from(s).length;\n  };\n  const serifW = (s: string) => {\n    if (!s) return 0;\n    ctx.font = `italic 400 100px \"${serif.fontFamily}\"`;\n    return (ctx.measureText(s).width / 100) * PIN_SCALE;\n  };\n  return parts.map((p) => ({\n    left: sansW(p.left),\n    token: serifW(p.token),\n    right: sansW(p.right),\n  }));\n}\n\nfunction useWidths(parts: Split[]) {\n  const key = JSON.stringify(parts);\n  const [state, setState] = useState<{ key: string; w: Widths[] } | null>(null);\n  const [handle] = useState(() => delayRender(\"pinned-word: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      sans.waitUntilDone(),\n      serif.waitUntilDone(),\n      document.fonts.load(`500 100px \"${sans.fontFamily}\"`),\n      document.fonts.load(`italic 400 100px \"${serif.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setState({ key, w: measure(JSON.parse(key) as Split[]) });\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, handle]);\n  return state && state.key === key ? state.w : null;\n}\n\nfunction Grain({ opacity }: { opacity: number }) {\n  const id = `pw${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  return (\n    <svg\n      width=\"100%\"\n      height=\"100%\"\n      style={{ position: \"absolute\", inset: 0, opacity, pointerEvents: \"none\" }}\n      aria-hidden\n    >\n      <filter id={id}>\n        <feTurbulence\n          type=\"fractalNoise\"\n          baseFrequency=\"0.9\"\n          numOctaves={2}\n          seed={9}\n          stitchTiles=\"stitch\"\n        />\n        <feColorMatrix values=\"0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0.9 0\" />\n      </filter>\n      <rect width=\"100%\" height=\"100%\" filter={`url(#${id})`} />\n    </svg>\n  );\n}\n\nexport function PinnedWord({\n  lines = [\n    \"Ship a video tonight.\",\n    \"Write the video in code.\",\n    \"Own every video frame.\",\n  ],\n  word = \"video\",\n  hold = 36,\n  accent = \"#3B5BFD\",\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: PinnedWordProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H } = useVideoConfig();\n  const c = THEMES[theme];\n  const uid = useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const blurId = `pwb${uid}`;\n  const list = lines.length > 0 ? lines : [word];\n  const parts = useMemo(() => list.map((l) => split(l, word)), [list, word]);\n  const widths = useWidths(parts);\n\n  const transparent = background === \"transparent\";\n  const stage = transparent ? \"transparent\" : (background ?? c.bg);\n\n  if (!widths) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const portrait = H > W * 1.1;\n  const s = portrait ? W / 1080 : Math.min(W / 1280, H / 720);\n  const pinned = word ? (widths.find((_, i) => parts[i].found)?.token ?? 0) : 0;\n\n  const maxL = Math.max(\n    0,\n    ...widths.map((w, i) => (parts[i].found ? w.left : 0)),\n  );\n  const maxR = Math.max(\n    0,\n    ...widths.map((w, i) => (parts[i].found ? w.right : 0)),\n  );\n  const loose = Math.max(\n    0,\n    ...widths.map((w, i) => (parts[i].found ? 0 : w.left)),\n  );\n  const fStack = Math.min(\n    (portrait ? 120 : 84) * s,\n    (W * 0.86) / Math.max(0.1, maxL, maxR, pinned, loose),\n    (H * 0.8) / (PIN_SCALE + 2.6 * 1.3),\n  );\n  const fInline = Math.min(\n    H * 0.13,\n    (W * 0.45) / Math.max(0.1, Math.max(maxL, maxR) + pinned / 2),\n    (W * 0.9) / Math.max(0.1, loose),\n  );\n  const stacked = portrait || fInline < H * 0.075;\n  const F = stacked ? fStack : fInline;\n  const pinW = pinned * F;\n  const pinX = W / 2 - pinW / 2;\n  const clipGap = F * 0.14;\n  const baseY = H / 2 + F * PIN_SCALE * 0.3;\n  const rowGap = F * 1.3;\n\n  const n = list.length;\n  const STEP = Math.max(FLICK + 6, hold);\n  const flickAt = (k: number) => FIRST_FLICK + (k - 1) * STEP;\n  const posAt = (f: number) => {\n    let p = 0;\n    for (let k = 1; k < n; k++) {\n      p += interpolate(f, [flickAt(k), flickAt(k) + FLICK], [0, 1], {\n        ...CLAMP,\n        easing: EASE_FLICK,\n      });\n    }\n    return p;\n  };\n  const pos = posAt(t);\n  const vel = Math.abs(pos - posAt(t - 1));\n  const pitch = stacked ? H * 0.5 : H * 0.62;\n  const blurY = Math.min(F * 0.5, vel * pitch * 0.3);\n\n  const wordIn = interpolate(t, [INTRO_WORD, INTRO_WORD + 12], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const openL = interpolate(t, [INTRO_LINE, INTRO_LINE + 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const openR = interpolate(t, [INTRO_LINE + 3, INTRO_LINE + 23], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n\n  const nearest = Math.min(n - 1, Math.max(0, Math.round(pos)));\n  const pinVisible = parts[nearest].found ? 1 : 0;\n\n  let sheen = -1;\n  for (let k = 0; k < n; k++) {\n    const at = k === 0 ? INTRO_LINE + 4 : flickAt(k) + FLICK * 0.4;\n    const p = interpolate(t, [at, at + 24], [0, 1], {\n      ...CLAMP,\n      easing: Easing.bezier(0.4, 0, 0.2, 1),\n    });\n    if (p > 0 && p < 1) sheen = p;\n  }\n  const bgShift = sheen < 0 ? 0 : sheen * pinW;\n\n  const sansStyle: React.CSSProperties = {\n    position: \"absolute\",\n    whiteSpace: \"pre\",\n    fontFamily: sans.fontFamily,\n    fontWeight: 500,\n    fontSize: F,\n    lineHeight: 1,\n    letterSpacing: `${SANS_TRACK}em`,\n    color: c.ink,\n  };\n  const serifStyle = (w: number): React.CSSProperties => ({\n    position: \"absolute\",\n    whiteSpace: \"pre\",\n    fontFamily: serif.fontFamily,\n    fontStyle: \"italic\",\n    fontWeight: 400,\n    fontSize: F * PIN_SCALE,\n    lineHeight: 1,\n    backgroundImage: c.word(accent),\n    backgroundSize: `${(w * 2).toFixed(1)}px 100%`,\n    WebkitBackgroundClip: \"text\",\n    backgroundClip: \"text\",\n    color: \"transparent\",\n  });\n  const sansTop = (base: number) => base - F * 0.8;\n  const serifTop = (base: number) => base - F * PIN_SCALE * 0.78;\n\n  const rows = list.map((_, i) => {\n    const d = i - pos;\n    if (Math.abs(d) > 1.05) return null;\n    const base = baseY + d * pitch;\n    const p = parts[i];\n    const w = widths[i];\n    const o = Math.max(0, (1 - Math.min(1, Math.abs(d))) * 1.15 - 0.15);\n    const copyO = Math.min(1, Math.abs(d) * 4) * o;\n    const kl = i === 0 ? openL : 1;\n    const kr = i === 0 ? openR : 1;\n\n    if (!p.found) {\n      return (\n        <div\n          key={`r${i}`}\n          style={{\n            ...sansStyle,\n            left: W / 2 - (w.left * F) / 2,\n            top: sansTop(base),\n            opacity: o,\n          }}\n        >\n          {p.left}\n        </div>\n      );\n    }\n\n    if (stacked) {\n      const rp = p.right.trimStart();\n      const punct = /^[.,;:!?\\u2026]+/.exec(rp)?.[0] ?? \"\";\n      const rest = rp.slice(punct.length).trim();\n      const left = p.left.trimEnd();\n      const centred: React.CSSProperties = {\n        ...sansStyle,\n        left: 0,\n        width: W,\n        textAlign: \"center\",\n      };\n      return (\n        <div key={`r${i}`}>\n          {left ? (\n            <div\n              style={{\n                ...centred,\n                top: sansTop(base - rowGap * PIN_SCALE) + (1 - kl) * F * 0.4,\n                opacity: o * kl,\n              }}\n            >\n              {left}\n            </div>\n          ) : null}\n          <div\n            style={{\n              ...serifStyle(pinW),\n              left: pinX + (pinW - w.token * F) / 2,\n              top: serifTop(base),\n              opacity: copyO * 0.55,\n            }}\n          >\n            {p.token}\n          </div>\n          {punct ? (\n            <div\n              style={{\n                ...sansStyle,\n                left: pinX + pinW + F * 0.04,\n                top: sansTop(base),\n                opacity: o * kr,\n              }}\n            >\n              {punct}\n            </div>\n          ) : null}\n          {rest ? (\n            <div\n              style={{\n                ...centred,\n                top: sansTop(base + rowGap) - (1 - kr) * F * 0.4,\n                opacity: o * kr,\n              }}\n            >\n              {rest}\n            </div>\n          ) : null}\n        </div>\n      );\n    }\n\n    return (\n      <div key={`r${i}`}>\n        {p.left ? (\n          <div\n            style={{\n              position: \"absolute\",\n              left: 0,\n              top: 0,\n              width: pinX - clipGap,\n              height: H,\n              overflow: \"hidden\",\n            }}\n          >\n            <div\n              style={{\n                ...sansStyle,\n                left: pinX - w.left * F + (1 - kl) * w.left * F,\n                top: sansTop(base),\n                opacity: o * Math.min(1, kl * 1.6),\n              }}\n            >\n              {p.left}\n            </div>\n          </div>\n        ) : null}\n        <div\n          style={{\n            ...serifStyle(pinW),\n            left: pinX + (pinW - w.token * F) / 2,\n            top: serifTop(base),\n            opacity: copyO * 0.55,\n          }}\n        >\n          {p.token}\n        </div>\n        {p.right ? (\n          <div\n            style={{\n              position: \"absolute\",\n              left: pinX + pinW + clipGap,\n              top: 0,\n              width: W - pinX - pinW - clipGap,\n              height: H,\n              overflow: \"hidden\",\n            }}\n          >\n            <div\n              style={{\n                ...sansStyle,\n                left: -clipGap - (1 - kr) * w.right * F,\n                top: sansTop(base),\n                opacity: o * Math.min(1, kr * 1.6),\n              }}\n            >\n              {p.right}\n            </div>\n          </div>\n        ) : null}\n      </div>\n    );\n  });\n\n  const pinToken = parts.find((p) => p.found)?.token ?? word;\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      {transparent ? null : (\n        <div style={{ position: \"absolute\", inset: 0, background: c.ground }} />\n      )}\n      <svg width={0} height={0} style={{ position: \"absolute\" }} aria-hidden>\n        <filter id={blurId} x=\"-10%\" y=\"-50%\" width=\"120%\" height=\"200%\">\n          <feGaussianBlur stdDeviation={`0 ${blurY.toFixed(2)}`} />\n        </filter>\n      </svg>\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          filter: blurY > 0.3 ? `url(#${blurId})` : undefined,\n        }}\n      >\n        {rows}\n      </div>\n      {word ? (\n        <div\n          style={{\n            ...serifStyle(pinW),\n            left: pinX,\n            top: serifTop(baseY),\n            opacity: wordIn * pinVisible,\n            filter:\n              wordIn < 1\n                ? `blur(${((1 - wordIn) * 12 * s).toFixed(2)}px)`\n                : undefined,\n            transform: `scale(${(0.94 + 0.06 * wordIn).toFixed(4)})`,\n            backgroundPosition: `${bgShift.toFixed(2)}px 0`,\n          }}\n        >\n          {pinToken}\n        </div>\n      ) : null}\n      {transparent ? null : <Grain opacity={c.grain} />}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/pinned-word.tsx"
    }
  ],
  "type": "registry:component"
}
