{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "turnabout",
  "title": "Turnabout",
  "description": "\"The video works for you.\" with the last word turned to an orange accent.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/turnabout/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/DMSans\";\nimport type React from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst dmSans = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface TurnaboutProps {\n  before?: string;\n  after?: string;\n  highlight?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#2E3440\", ink: \"#FFFFFF\", accent: \"#FFB020\" },\n  light: { bg: \"#F1F2F4\", ink: \"#2E3440\", accent: \"#B86E00\" },\n} as const;\n\nconst WGHT = 500;\nconst TRACK = -0.035;\nconst PROBE = 200;\nconst BASE = 150;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst IN = 4;\nconst STAGGER = 3;\nconst ENTER = 16;\nconst TURN = 52;\nconst LIFT = 13;\nconst TRAVEL = 30;\nconst LIT = 8;\n\ninterface Tok {\n  text: string;\n  key: string;\n  start: number;\n  end: number;\n  x: number;\n  w: number;\n}\n\ninterface Line {\n  toks: Tok[];\n  width: number;\n}\n\ninterface Metrics {\n  a: Line;\n  b: Line;\n  cap: number;\n}\n\nfunction tokenize(s: string) {\n  const out: { text: string; start: number; end: number }[] = [];\n  const re = /[^\\s.,!?…;:]+|[.,!?…;:]+/g;\n  for (let m = re.exec(s); m; m = re.exec(s)) {\n    out.push({ text: m[0], start: m.index, end: m.index + m[0].length });\n  }\n  return out;\n}\n\nfunction layoutLine(s: string, em: (x: string) => number): Line {\n  const toks = tokenize(s).map((tk) => ({\n    ...tk,\n    key: tk.text.toLowerCase(),\n    x: em(s.slice(0, tk.start)),\n    w: em(tk.text),\n  }));\n  return { toks, width: em(s) };\n}\n\nfunction measure(before: string, after: string): Metrics {\n  const canvas = document.createElement(\"canvas\");\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) throw new Error(\"turnabout: no 2d context\");\n  ctx.font = `${WGHT} ${PROBE}px \"${dmSans.fontFamily}\"`;\n  const em = (x: string) =>\n    ctx.measureText(x).width / PROBE + TRACK * Array.from(x).length;\n  const cap = ctx.measureText(\"H\").actualBoundingBoxAscent / PROBE;\n  return { a: layoutLine(before, em), b: layoutLine(after, em), cap };\n}\n\nfunction useMetrics(before: string, after: string) {\n  const key = `${before}\\u0001${after}`;\n  const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n  const [handle] = useState(() => delayRender(\"turnabout: measuring\"));\n  const released = useRef(false);\n  const src = useRef({ before, after });\n  src.current = { before, after };\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      dmSans.waitUntilDone(),\n      document.fonts.load(`${WGHT} 100px \"${dmSans.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setState({ key, m: measure(src.current.before, src.current.after) });\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.m : null;\n}\n\nfunction related(a: string, b: string) {\n  if (a === b) return 2;\n  const short = a.length < b.length ? a : b;\n  const long = a.length < b.length ? b : a;\n  if (short.length >= 3 && long.startsWith(short)) return 1;\n  return 0;\n}\n\nfunction pairTokens(a: Tok[], b: Tok[]) {\n  const pairOfB = new Array<number>(b.length).fill(-1);\n  const used = new Set<number>();\n  for (const level of [2, 1]) {\n    b.forEach((tb, j) => {\n      if (pairOfB[j] >= 0) return;\n      const i = a.findIndex(\n        (ta, k) => !used.has(k) && related(ta.key, tb.key) === level,\n      );\n      if (i >= 0) {\n        pairOfB[j] = i;\n        used.add(i);\n      }\n    });\n  }\n  return pairOfB;\n}\n\ninterface Mover {\n  i: number;\n  j: number;\n  a0: number;\n  a1: number;\n  b0: number;\n  b1: number;\n  lane: number;\n}\n\nfunction clashes(p: Mover, q: Mover, gap: number) {\n  const leftA = p.a1 + gap <= q.a0;\n  const rightA = q.a1 + gap <= p.a0;\n  const leftB = p.b1 + gap <= q.b0;\n  const rightB = q.b1 + gap <= p.b0;\n  return !((leftA && leftB) || (rightA && rightB));\n}\n\nfunction assignLanes(movers: Mover[], still: number, gap: number) {\n  const order = [...movers].sort(\n    (p, q) => Math.abs(p.b0 - p.a0) - Math.abs(q.b0 - q.a0),\n  );\n  const placed: Mover[] = [];\n  for (const mv of order) {\n    const dx = mv.b0 - mv.a0;\n    const dir = dx >= 0 ? -1 : 1;\n    const lanes =\n      Math.abs(dx) < still\n        ? [0, dir, dir * 2, dir * 3]\n        : [dir, dir * 2, dir * 3, dir * 4];\n    for (const lane of lanes) {\n      if (!placed.some((o) => o.lane === lane && clashes(mv, o, gap))) {\n        mv.lane = lane;\n        break;\n      }\n      mv.lane = lane;\n    }\n    placed.push(mv);\n  }\n}\n\nexport function Turnabout({\n  before = \"You work for the video.\",\n  after = \"The video works for you.\",\n  highlight = \"you.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: TurnaboutProps) {\n  const frame = useCurrentFrame();\n  const { width: W, height: H, durationInFrames } = useVideoConfig();\n  const k = Math.min(1, durationInFrames / BASE);\n  const t = (frame * speed) / k;\n  const c = THEMES[theme];\n  const hi = accent ?? c.accent;\n  const stage =\n    background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n  const m = useMetrics(before, after);\n\n  if (!m) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const portrait = H > W * 1.1;\n  const widest = Math.max(m.a.width, m.b.width, 0.1);\n  const F = Math.min(\n    (W * (portrait ? 0.92 : 0.88)) / widest,\n    H * 0.16,\n    W * 0.12,\n  );\n  const leftA = (W - m.a.width * F) / 2;\n  const leftB = (W - m.b.width * F) / 2;\n  const base = H * 0.5 + (m.cap * F) / 2;\n  const top = base - F * 0.93;\n  const laneGap = F * 1.02;\n\n  const pairOfB = pairTokens(m.a.toks, m.b.toks);\n  const pairedA = new Set(pairOfB.filter((i) => i >= 0));\n  const movers: Mover[] = [];\n  pairOfB.forEach((i, j) => {\n    if (i < 0) return;\n    const ta = m.a.toks[i];\n    const tb = m.b.toks[j];\n    movers.push({\n      i,\n      j,\n      a0: leftA + ta.x * F,\n      a1: leftA + (ta.x + ta.w) * F,\n      b0: leftB + tb.x * F,\n      b1: leftB + (tb.x + tb.w) * F,\n      lane: 0,\n    });\n  });\n  assignLanes(movers, F * 0.3, F * 0.12);\n\n  const hiStart = highlight ? after.lastIndexOf(highlight) : -1;\n  const hiEnd = hiStart >= 0 ? hiStart + highlight.length : -1;\n  const isHi = (tk: Tok) =>\n    hiStart >= 0 && tk.start < hiEnd && tk.end > hiStart;\n\n  const t1 = TURN + LIFT * 0.6;\n  const t2 = t1 + TRAVEL;\n  const dropAt = t2 - LIFT * 0.4;\n  const landed = dropAt + LIFT;\n  const lift =\n    interpolate(t, [TURN, TURN + LIFT], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    }) -\n    interpolate(t, [dropAt, landed], [0, 1], {\n      ...CLAMP,\n      easing: Easing.bezier(0.65, 0, 0.35, 1),\n    });\n  const travel = interpolate(t, [t1, t2], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const lit = interpolate(t, [landed - 2, landed + LIT], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const swapped = t >= t1 + TRAVEL * 0.5;\n  const suffixIn = interpolate(t, [dropAt, landed], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n\n  const word = (\n    key: string,\n    text: string,\n    x: number,\n    dy: number,\n    opacity: number,\n    color: string,\n    suffix?: { text: string; opacity: number },\n  ) => (\n    <div\n      key={key}\n      style={{\n        position: \"absolute\",\n        left: 0,\n        top,\n        height: F * 1.2,\n        lineHeight: `${F * 1.2}px`,\n        whiteSpace: \"pre\",\n        fontFamily: dmSans.fontFamily,\n        fontWeight: WGHT,\n        fontSize: F,\n        letterSpacing: `${TRACK}em`,\n        fontKerning: \"normal\",\n        color,\n        opacity,\n        transform: `translate(${x.toFixed(2)}px, ${dy.toFixed(2)}px)`,\n      }}\n    >\n      {text}\n      {suffix ? (\n        <span style={{ opacity: suffix.opacity }}>{suffix.text}</span>\n      ) : null}\n    </div>\n  );\n\n  const nodes: React.ReactNode[] = [];\n\n  m.a.toks.forEach((ta, i) => {\n    const enter = interpolate(\n      t,\n      [IN + i * STAGGER, IN + i * STAGGER + ENTER],\n      [0, 1],\n      { ...CLAMP, easing: EASE_OUT },\n    );\n    if (pairedA.has(i)) return;\n    const out = interpolate(t, [TURN, TURN + LIFT], [0, 1], {\n      ...CLAMP,\n      easing: Easing.bezier(0.55, 0, 1, 0.45),\n    });\n    if (enter * (1 - out) <= 0) return;\n    nodes.push(\n      word(\n        `a${i}`,\n        ta.text,\n        leftA + ta.x * F,\n        F * 0.28 * (1 - enter) + F * 0.4 * out,\n        enter * (1 - out),\n        c.ink,\n      ),\n    );\n  });\n\n  for (const mv of movers) {\n    const ta = m.a.toks[mv.i];\n    const tb = m.b.toks[mv.j];\n    const enter = interpolate(\n      t,\n      [IN + mv.i * STAGGER, IN + mv.i * STAGGER + ENTER],\n      [0, 1],\n      { ...CLAMP, easing: EASE_OUT },\n    );\n    const x = mv.a0 + (mv.b0 - mv.a0) * travel;\n    const dy = F * 0.28 * (1 - enter) + mv.lane * laneGap * lift;\n    const color =\n      isHi(tb) && lit > 0\n        ? `color-mix(in oklab, ${hi} ${(lit * 100).toFixed(1)}%, ${c.ink})`\n        : c.ink;\n    if (ta.text === tb.text) {\n      nodes.push(word(`m${mv.j}`, tb.text, x, dy, enter, color));\n    } else if (tb.text.startsWith(ta.text)) {\n      nodes.push(\n        word(`m${mv.j}`, ta.text, x, dy, enter, color, {\n          text: tb.text.slice(ta.text.length),\n          opacity: suffixIn,\n        }),\n      );\n    } else if (ta.text.startsWith(tb.text)) {\n      nodes.push(\n        word(`m${mv.j}`, tb.text, x, dy, enter, color, {\n          text: ta.text.slice(tb.text.length),\n          opacity: 1 - suffixIn,\n        }),\n      );\n    } else {\n      nodes.push(\n        word(`m${mv.j}`, swapped ? tb.text : ta.text, x, dy, enter, color),\n      );\n    }\n  }\n\n  m.b.toks.forEach((tb, j) => {\n    if (pairOfB[j] >= 0) return;\n    const inn = interpolate(t, [dropAt, landed + 4], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n    if (inn <= 0) return;\n    const color = isHi(tb)\n      ? `color-mix(in oklab, ${hi} ${(lit * 100).toFixed(1)}%, ${c.ink})`\n      : c.ink;\n    nodes.push(\n      word(`b${j}`, tb.text, leftB + tb.x * F, F * 0.3 * (1 - inn), inn, color),\n    );\n  });\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      {nodes}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/turnabout.tsx"
    }
  ],
  "type": "registry:component"
}
