{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "caret-letter",
  "title": "Caret Letter",
  "description": "A blinking caret types a word and turns into its letter l: 'launch'.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/caret-letter/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/InterTight\";\nimport { useLayoutEffect, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst face = loadFont(\"normal\", {\n  weights: [\"300\", \"400\"],\n  subsets: [\"latin\"],\n});\n\nexport interface CaretLetterProps {\n  /** The word. The caret types every letter but one, then becomes that one. */\n  word?: string;\n  /** Index of the letter the caret turns into. Pick a plain stem: l, I, i without its dot, 1. */\n  stemIndex?: number;\n  /** Small grey line under the word. */\n  subline?: string;\n  /** Colour of the caret, and of the letter it becomes. */\n  accent?: string;\n  theme?: \"dark\" | \"light\";\n  speed?: number;\n}\n\nconst THEMES = {\n  light: { bg: \"#FAFAF9\", fg: \"#0E0E0D\", muted: \"#8C8C88\" },\n  dark: { bg: \"#0A0A0A\", fg: \"#F2F2F0\", muted: \"#7C7C79\" },\n} as const;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IN_OUT = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_TYPE = Easing.bezier(0.4, 0, 0.3, 1);\nconst clamp = { extrapolateLeft: \"clamp\", extrapolateRight: \"clamp\" } as const;\n\n// Beats, in frames at 30fps (before `speed`).\nconst CARET_IN = 0;\nconst TYPE = 46;\nconst TYPE_LEN = 46;\nconst LETTER_LEN = 16;\nconst SLIDE = 110;\nconst SLIDE_LEN = 26;\nconst OPEN = 118;\nconst OPEN_LEN = 24;\nconst MORPH = 134;\nconst MORPH_LEN = 16;\nconst SUB = 150;\n\n/** Caret proportions in em. */\nconst CARET_W = 0.02;\nconst CARET_UP = 0.98;\nconst CARET_DOWN = 0.22;\nconst LINE_HEIGHT = 1.2;\n\ninterface Ink {\n  left: number;\n  right: number;\n  top: number;\n  bottom: number;\n}\n\ninterface Layout {\n  /** Letter origins in the finished word. */\n  full: number[];\n  /** Letter origins in the typed word (stem letter missing). */\n  typed: number[];\n  fullW: number;\n  typedW: number;\n  baseline: number;\n  rowH: number;\n  stem: Ink;\n}\n\nfunction measureInk(char: string, font: string, fontSize: number): Ink | null {\n  const k = 4;\n  const size = fontSize * k;\n  const canvas = document.createElement(\"canvas\");\n  canvas.width = Math.ceil(size * 2);\n  canvas.height = Math.ceil(size * 2);\n  const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n  if (!ctx) return null;\n  const ox = Math.round(size * 0.5);\n  const by = Math.round(size * 1.3);\n  ctx.font = font.replace(\"{size}\", `${size}px`);\n  ctx.textBaseline = \"alphabetic\";\n  ctx.fillStyle = \"#000\";\n  ctx.fillText(char, ox, by);\n  const { data, width, height } = ctx.getImageData(\n    0,\n    0,\n    canvas.width,\n    canvas.height,\n  );\n  let left = width;\n  let right = 0;\n  let top = height;\n  let bottom = 0;\n  for (let y = 0; y < height; y++) {\n    for (let x = 0; x < width; x++) {\n      if (data[(y * width + x) * 4 + 3] > 127) {\n        if (x < left) left = x;\n        if (x + 1 > right) right = x + 1;\n        if (y < top) top = y;\n        if (y + 1 > bottom) bottom = y + 1;\n      }\n    }\n  }\n  if (right <= left) return null;\n  return {\n    left: (left - ox) / k,\n    right: (right - ox) / k,\n    top: (top - by) / k,\n    bottom: (bottom - by) / k,\n  };\n}\n\nfunction sameLayout(a: Layout | null, b: Layout) {\n  return (\n    a !== null &&\n    a.full.join() === b.full.join() &&\n    a.typed.join() === b.typed.join() &&\n    a.baseline === b.baseline &&\n    a.stem.left === b.stem.left\n  );\n}\n\nexport function CaretLetter({\n  word = \"launch\",\n  stemIndex = 0,\n  subline = \"Animated components for your next release.\",\n  accent = \"#3B5BFD\",\n  theme = \"light\",\n  speed = 1,\n}: CaretLetterProps) {\n  const frame = useCurrentFrame() * speed;\n  const { width, height } = useVideoConfig();\n  const t = THEMES[theme];\n\n  const letters = Array.from(word);\n  const stem = Math.max(0, Math.min(letters.length - 1, stemIndex));\n  const typedLetters = letters.filter((_, i) => i !== stem);\n  const fontSize = Math.round(width * 0.15);\n  const subSize = Math.round(width * 0.0165);\n  const textStyle = {\n    fontFamily: face.fontFamily,\n    fontSize,\n    fontWeight: 300,\n    letterSpacing: \"-0.035em\",\n    lineHeight: LINE_HEIGHT,\n    whiteSpace: \"pre\",\n  } as const;\n\n  const fullRef = useRef<HTMLDivElement>(null);\n  const typedRef = useRef<HTMLDivElement>(null);\n  const baseRef = useRef<HTMLSpanElement>(null);\n  const [layout, setLayout] = useState<Layout | null>(null);\n  const [handle] = useState(() => delayRender(\"caret-letter: measuring\"));\n  const released = useRef(false);\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: re-measure when the copy or size changes\n  useLayoutEffect(() => {\n    let live = true;\n    face\n      .waitUntilDone()\n      .catch(() => undefined)\n      .then(() => document.fonts.ready)\n      .then(() => {\n        if (!live) return;\n        const fullRow = fullRef.current;\n        const typedRow = typedRef.current;\n        const base = baseRef.current;\n        if (fullRow && typedRow && base) {\n          const origins = (row: HTMLDivElement) => {\n            const r = row.getBoundingClientRect();\n            const scale = r.width / row.offsetWidth || 1;\n            const spans = Array.from(row.querySelectorAll(\"[data-l]\"));\n            return {\n              xs: spans.map(\n                (el) => (el.getBoundingClientRect().left - r.left) / scale,\n              ),\n              w: row.offsetWidth,\n            };\n          };\n          const f = origins(fullRow);\n          const ty = origins(typedRow);\n          const family = face.fontFamily.includes('\"')\n            ? face.fontFamily\n            : `\"${face.fontFamily}\"`;\n          const ink = measureInk(\n            letters[stem] ?? \"l\",\n            `300 {size} ${family}`,\n            fontSize,\n          ) ?? {\n            left: fontSize * 0.06,\n            right: fontSize * 0.12,\n            top: -fontSize * 0.74,\n            bottom: 0,\n          };\n          const next: Layout = {\n            full: f.xs,\n            typed: ty.xs,\n            fullW: f.w,\n            typedW: ty.w,\n            baseline: base.offsetTop,\n            rowH: fullRow.offsetHeight,\n            stem: ink,\n          };\n          setLayout((prev) => (sameLayout(prev, next) ? prev : next));\n        }\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [word, stem, fontSize, handle]);\n\n  const L = layout;\n  const gap = fontSize * 0.05;\n  const typedEnd = (L?.typedW ?? 0) + gap * 0.4;\n\n  // Typing: the caret sweeps right, letters grow in its wake.\n  const typeX = (f: number) =>\n    interpolate(f, [TYPE, TYPE + TYPE_LEN], [0, typedEnd], {\n      ...clamp,\n      easing: EASE_TYPE,\n    });\n  // Right edge of each typed letter, for the moment the caret clears it.\n  const typedRight = (j: number) =>\n    j + 1 < typedLetters.length ? (L?.typed[j + 1] ?? 0) : (L?.typedW ?? 0);\n  const letterIn = typedLetters.map((_, j) => {\n    const left = L?.typed[j] ?? 0;\n    const edge = typedRight(j) - (typedRight(j) - left) * 0.1;\n    for (let f = TYPE; f <= TYPE + TYPE_LEN; f++)\n      if (typeX(f) >= edge) return f - 2;\n    return TYPE + TYPE_LEN;\n  });\n\n  // Slide home: the caret runs back into the word, the letters part for it.\n  const slide = interpolate(frame, [SLIDE, SLIDE + SLIDE_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_IN_OUT,\n  });\n  const open = interpolate(frame, [OPEN, OPEN + OPEN_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_IN_OUT,\n  });\n  const morph = interpolate(frame, [MORPH, MORPH + MORPH_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_OUT,\n  });\n\n  const ink = L?.stem ?? { left: 0, right: 0, top: 0, bottom: 0 };\n  const stemOrigin = L?.full[stem] ?? 0;\n  const stemCx = stemOrigin + (ink.left + ink.right) / 2;\n  // Before the stem slot opens, the typed letters to its right sit where the\n  // stem will go; the caret aims for the slot as it opens.\n  const caretHome =\n    stem < typedLetters.length\n      ? interpolate(open, [0, 1], [L?.typed[stem] ?? 0, stemCx])\n      : stemCx;\n  const caretX =\n    frame < SLIDE\n      ? typeX(frame)\n      : interpolate(slide, [0, 1], [typedEnd, caretHome]);\n\n  // Centre the visible block, like centred text growing as it is typed.\n  const typingCentre = typeX(frame) / 2;\n  const fullCentre = (L?.fullW ?? 0) / 2;\n  const centre = interpolate(slide, [0, 1], [typingCentre, fullCentre]);\n\n  // Caret blink: soft on/off while idle, solid while it moves.\n  const blinkAt = (f: number, from: number, to: number) => {\n    if (f < from || f > to) return 1;\n    const u = (f - from) % 24;\n    if (u < 13) return 1;\n    if (u < 16) return 1 - (u - 13) / 3;\n    if (u < 21) return 0;\n    return (u - 21) / 3;\n  };\n  const caretIn = interpolate(frame, [CARET_IN, CARET_IN + 10], [0, 1], {\n    ...clamp,\n    easing: EASE_OUT,\n  });\n  const blink =\n    frame < TYPE\n      ? blinkAt(frame, 8, TYPE - 4)\n      : blinkAt(frame, TYPE + TYPE_LEN + 2, SLIDE - 2);\n\n  // Caret geometry, morphing into the stem of the letter.\n  const baseline = L?.baseline ?? 0;\n  const caretW = interpolate(\n    morph,\n    [0, 1],\n    [fontSize * CARET_W, ink.right - ink.left],\n  );\n  const caretTop = interpolate(\n    morph,\n    [0, 1],\n    [baseline - fontSize * CARET_UP, baseline + ink.top],\n  );\n  const caretBottom = interpolate(\n    morph,\n    [0, 1],\n    [baseline + fontSize * CARET_DOWN, baseline + ink.bottom],\n  );\n\n  const subP = interpolate(frame, [SUB, SUB + 30], [0, 1], {\n    ...clamp,\n    easing: EASE_OUT,\n  });\n\n  const rowH = L?.rowH ?? fontSize * LINE_HEIGHT;\n  const left = width / 2 - centre;\n  const top = height / 2 - rowH / 2 - height * 0.035;\n\n  return (\n    <AbsoluteFill style={{ background: t.bg, overflow: \"hidden\" }}>\n      {/* measuring rows: never shown */}\n      <div style={{ position: \"absolute\", visibility: \"hidden\", top: 0 }}>\n        <div ref={fullRef} style={{ ...textStyle, display: \"inline-block\" }}>\n          {letters.map((ch, i) => (\n            <span key={i} data-l=\"\">\n              {ch}\n            </span>\n          ))}\n          <span\n            ref={baseRef}\n            style={{ display: \"inline-block\", width: 0, height: 0 }}\n          />\n        </div>\n        <br />\n        <div ref={typedRef} style={{ ...textStyle, display: \"inline-block\" }}>\n          {typedLetters.map((ch, i) => (\n            <span key={i} data-l=\"\">\n              {ch}\n            </span>\n          ))}\n        </div>\n      </div>\n\n      <div\n        style={{\n          position: \"absolute\",\n          left,\n          top,\n          width: 0,\n          height: rowH,\n          opacity: L ? 1 : 0,\n        }}\n      >\n        {typedLetters.map((ch, j) => {\n          const p = interpolate(\n            frame,\n            [letterIn[j] ?? TYPE, (letterIn[j] ?? TYPE) + LETTER_LEN],\n            [0, 1],\n            { ...clamp, easing: EASE_OUT },\n          );\n          const fullIndex = j < stem ? j : j + 1;\n          const x = interpolate(\n            open,\n            [0, 1],\n            [L?.typed[j] ?? 0, L?.full[fullIndex] ?? 0],\n          );\n          return (\n            <span\n              key={j}\n              style={{\n                ...textStyle,\n                position: \"absolute\",\n                left: x,\n                top: 0,\n                color: t.fg,\n                opacity: p,\n                filter: p < 1 ? `blur(${(1 - p) * 8}px)` : undefined,\n                transformOrigin: `0px ${baseline}px`,\n                transform: `scale(${0.6 + 0.4 * p})`,\n              }}\n            >\n              {ch}\n            </span>\n          );\n        })}\n\n        <div\n          style={{\n            position: \"absolute\",\n            left: caretX - caretW / 2,\n            top: caretTop,\n            width: caretW,\n            height: caretBottom - caretTop,\n            background: accent,\n            borderRadius: interpolate(morph, [0, 1], [caretW / 2, 0]),\n            opacity: caretIn * blink,\n          }}\n        />\n\n        <div\n          style={{\n            position: \"absolute\",\n            left: (L?.fullW ?? 0) / 2,\n            top: rowH + fontSize * 0.02,\n            transform: `translate(-50%, ${(1 - subP) * 12}px)`,\n            whiteSpace: \"nowrap\",\n            fontFamily: face.fontFamily,\n            fontWeight: 400,\n            fontSize: subSize,\n            letterSpacing: \"-0.005em\",\n            lineHeight: 1.4,\n            color: t.muted,\n            opacity: subP,\n            filter: `blur(${(1 - subP) * 8}px)`,\n          }}\n        >\n          {subline}\n        </div>\n      </div>\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/caret-letter.tsx"
    }
  ],
  "type": "registry:component"
}
