{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ruler-align",
  "title": "Ruler Align",
  "description": "A ruler slides under a text line and straightens its letters onto the edge: 'Made to measure.'.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/ruler-align/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Geist\";\nimport { loadFont as loadMono } from \"@remotion/google-fonts/GeistMono\";\nimport { useId, 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\", { weights: [\"300\"], subsets: [\"latin\"] });\nconst mono = loadMono(\"normal\", { weights: [\"400\"], subsets: [\"latin\"] });\n\nexport interface RulerAlignProps {\n  /** The line the ruler straightens. Letters without descenders sit best on the edge. */\n  text?: string;\n  /** The ruler's straight edge. */\n  accent?: string;\n  theme?: \"dark\" | \"light\";\n  speed?: number;\n}\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0A\",\n    fg: \"#F2F2F0\",\n    rulerFill: \"rgba(255,255,255,0.045)\",\n    rulerLine: \"rgba(255,255,255,0.16)\",\n    tick: \"rgba(255,255,255,0.34)\",\n    num: \"rgba(255,255,255,0.42)\",\n  },\n  light: {\n    bg: \"#FAFAF9\",\n    fg: \"#0E0E0D\",\n    rulerFill: \"rgba(0,0,0,0.03)\",\n    rulerLine: \"rgba(0,0,0,0.14)\",\n    tick: \"rgba(0,0,0,0.36)\",\n    num: \"rgba(0,0,0,0.45)\",\n  },\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_FALL = Easing.bezier(0.5, 0, 0.9, 0.6);\nconst clamp = { extrapolateLeft: \"clamp\", extrapolateRight: \"clamp\" } as const;\n\n// Beats, in frames at 30fps (before `speed`).\nconst IN = 0;\nconst IN_STAGGER = 2;\nconst RULER_IN = 14;\nconst RULER_IN_LEN = 30;\nconst LEVEL = 44;\nconst LEVEL_LEN = 14;\nconst RISE = 56;\nconst RISE_LEN = 24;\nconst DROP = 82;\nconst DROP_STAGGER = 3;\nconst FALL = 10;\nconst TAP_1 = 112;\nconst TAP_2 = 130;\nconst TAP_LEN = 10;\nconst SLIDE_LEN = 16;\n\nconst LINE_HEIGHT = 1.2;\n/** Gap between the baseline and the ruler while it waits underneath, in em. */\nconst PARK = 0.62;\nconst RULER_H = 0.46;\n\ninterface Layout {\n  xs: number[];\n  ws: number[];\n  w: number;\n  baseline: number;\n}\n\n/** Deterministic 0..1 noise. */\nfunction hash(n: number) {\n  const s = Math.sin(n * 127.1 + 311.7) * 43758.5453;\n  return s - Math.floor(s);\n}\n\nfunction sameLayout(a: Layout | null, b: Layout) {\n  return (\n    a !== null &&\n    a.xs.join() === b.xs.join() &&\n    a.w === b.w &&\n    a.baseline === b.baseline\n  );\n}\n\n/** A quick dip and recovery of the ruler, in em (positive is down). */\nfunction tapDip(f: number, at: number) {\n  const d = f - at;\n  if (d < 0 || d > TAP_LEN) return 0;\n  if (d < 3) return 0.05 * EASE_OUT(d / 3);\n  return 0.05 * (1 - EASE_IN_OUT((d - 3) / (TAP_LEN - 3)));\n}\n\n/** The hop a letter makes when the ruler is tapped, in em (positive is up). */\nfunction hop(f: number, at: number) {\n  const d = f - at - 2;\n  if (d < 0 || d > 10) return 0;\n  return 0.07 * Math.sin((Math.PI * d) / 10);\n}\n\nexport function RulerAlign({\n  text = \"Made to measure.\",\n  accent = \"#3B5BFD\",\n  theme = \"dark\",\n  speed = 1,\n}: RulerAlignProps) {\n  const frame = useCurrentFrame() * speed;\n  const { width, height } = useVideoConfig();\n  const t = THEMES[theme];\n  const clipId = `ruler-${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n\n  const em = Math.round(width * 0.088);\n  const textStyle = {\n    fontFamily: face.fontFamily,\n    fontSize: em,\n    fontWeight: 300,\n    letterSpacing: \"-0.035em\",\n    lineHeight: LINE_HEIGHT,\n    whiteSpace: \"pre\",\n  } as const;\n\n  const chars = Array.from(text);\n  const rowRef = useRef<HTMLDivElement>(null);\n  const baseRef = useRef<HTMLSpanElement>(null);\n  const [layout, setLayout] = useState<Layout | null>(null);\n  const [handle] = useState(() => delayRender(\"ruler-align: 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    Promise.all([face.waitUntilDone(), mono.waitUntilDone()])\n      .catch(() => undefined)\n      .then(() => document.fonts.ready)\n      .then(() => {\n        if (!live) return;\n        const row = rowRef.current;\n        const base = baseRef.current;\n        if (row && base) {\n          const r = row.getBoundingClientRect();\n          const scale = r.width / row.offsetWidth || 1;\n          const spans = Array.from(row.querySelectorAll(\"[data-l]\"));\n          const next: Layout = {\n            xs: spans.map(\n              (el) => (el.getBoundingClientRect().left - r.left) / scale,\n            ),\n            ws: spans.map((el) => el.getBoundingClientRect().width / scale),\n            w: row.offsetWidth,\n            baseline: base.offsetTop,\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  }, [text, em, handle]);\n\n  const L = layout;\n  const textW = L?.w ?? 0;\n  const left = width / 2 - textW / 2;\n  const baseY = height / 2 + em * 0.2;\n  const rowTop = baseY - (L?.baseline ?? em);\n\n  // The messy line: uneven gaps, letters off the baseline, a little tilt.\n  // Neighbours alternate above and below so the line reads as jumbled.\n  let cum = 0;\n  const raw = chars.map((ch, i) => {\n    const seed = i + 1;\n    if (ch !== \" \") cum += (hash(seed * 3.7) * 0.17 - 0.035) * em;\n    const sign = i % 2 === 0 ? -1 : 1;\n    return {\n      off: cum,\n      dy: sign * (0.07 + hash(seed * 7.3) * 0.13) * em,\n      rot: (hash(seed * 5.9) - 0.5) * 14,\n    };\n  });\n  const mean = raw.reduce((a, r) => a + r.off, 0) / Math.max(1, raw.length);\n  const mess = raw.map((r) => ({ ...r, off: r.off - mean }));\n\n  // Ruler motion.\n  const rulerW = textW + em * 1.4;\n  const rulerX0 = width / 2 - rulerW / 2;\n  const inP = interpolate(frame, [RULER_IN, RULER_IN + RULER_IN_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_OUT,\n  });\n  const level = interpolate(frame, [LEVEL, LEVEL + LEVEL_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_IN_OUT,\n  });\n  const rise = interpolate(frame, [RISE, RISE + RISE_LEN], [0, 1], {\n    ...clamp,\n    easing: EASE_IN_OUT,\n  });\n  const dip = (tapDip(frame, TAP_1) + tapDip(frame, TAP_2)) * em;\n  /** Height of the ruler's top edge above the baseline (negative is below). */\n  const edge = -em * PARK * (1 - rise);\n  const rulerX = interpolate(inP, [0, 1], [-rulerW - em, rulerX0]);\n  const rulerTilt = interpolate(inP, [0, 1], [-5, -2.2]) * (1 - level);\n  const rulerTop = baseY - edge + dip;\n\n  // High letters drop onto the edge once it reaches the baseline, left to right.\n  const highOrder: number[] = [];\n  mess.forEach((m, i) => {\n    if (chars[i] !== \" \" && m.dy < 0) highOrder.push(i);\n  });\n\n  // Kerning: each tap moves the letters halfway home.\n  const slideP = (i: number, at: number) =>\n    interpolate(\n      frame,\n      [at + 2 + (i % 4), at + 2 + (i % 4) + SLIDE_LEN],\n      [0, 1],\n      { ...clamp, easing: EASE_IN_OUT },\n    );\n\n  const glyphs = chars.map((ch, i) => {\n    if (ch === \" \") return null;\n    const m = mess[i];\n    const w = L?.ws[i] ?? 0;\n    const home = left + (L?.xs[i] ?? 0);\n\n    const fade = interpolate(\n      frame,\n      [IN + i * IN_STAGGER, IN + i * IN_STAGGER + 18],\n      [0, 1],\n      { ...clamp, easing: EASE_OUT },\n    );\n\n    // Vertical: below the baseline is positive.\n    let y = m.dy;\n    let rot = m.rot;\n    let sx = 1;\n    let sy = 1;\n    if (m.dy > 0) {\n      // Low letter: the rising edge pushes it up and flattens it.\n      const lift = Math.max(0, -edge);\n      y = Math.min(m.dy, lift);\n      rot = m.rot * (y / m.dy);\n    } else {\n      const k = highOrder.indexOf(i);\n      const start = DROP + k * DROP_STAGGER;\n      const p = interpolate(frame, [start, start + FALL], [0, 1], {\n        ...clamp,\n        easing: EASE_FALL,\n      });\n      y = m.dy * (1 - p);\n      rot = m.rot * (1 - p);\n      const d = frame - (start + FALL);\n      if (d >= 0 && d < 7) {\n        const b = Math.sin((Math.PI * d) / 7) * (1 - d / 9);\n        sx = 1 + 0.025 * b;\n        sy = 1 - 0.05 * b;\n      }\n    }\n    y += dip - (hop(frame, TAP_1) + hop(frame, TAP_2)) * em;\n\n    const k1 = slideP(i, TAP_1);\n    const k2 = slideP(i, TAP_2);\n    const x = home + m.off * (1 - 0.55 * k1 - 0.45 * k2);\n\n    return (\n      <span\n        key={i}\n        style={{\n          ...textStyle,\n          position: \"absolute\",\n          left: x,\n          top: rowTop,\n          color: t.fg,\n          opacity: fade,\n          filter: fade < 1 ? `blur(${(1 - fade) * 8}px)` : undefined,\n          transformOrigin: `${w / 2}px ${L?.baseline ?? em}px`,\n          transform: `translateY(${y + (1 - fade) * em * 0.12}px) rotate(${rot}deg) scale(${sx}, ${sy})`,\n        }}\n      >\n        {ch}\n      </span>\n    );\n  });\n\n  // Ruler ticks: a long one every 10 units, mid every 5.\n  const unit = em * 0.06;\n  const ticks: { x: number; h: number; n: number | null }[] = [];\n  const count = Math.floor((rulerW - em * 0.2) / unit);\n  for (let k = 0; k <= count; k++) {\n    const long = k % 10 === 0;\n    const mid = k % 5 === 0;\n    ticks.push({\n      x: em * 0.1 + k * unit,\n      h: long ? 0.2 : mid ? 0.13 : 0.075,\n      n: long ? k / 10 : null,\n    });\n  }\n\n  const hair = Math.max(1.5, width / 720);\n\n  return (\n    <AbsoluteFill style={{ background: t.bg, overflow: \"hidden\" }}>\n      {/* measuring row: never shown */}\n      <div style={{ position: \"absolute\", visibility: \"hidden\", top: 0 }}>\n        <div ref={rowRef} style={{ ...textStyle, display: \"inline-block\" }}>\n          {chars.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      </div>\n\n      <div style={{ position: \"absolute\", inset: 0, opacity: L ? 1 : 0 }}>\n        <div\n          style={{\n            position: \"absolute\",\n            left: rulerX,\n            top: rulerTop,\n            width: rulerW,\n            height: em * RULER_H,\n            transformOrigin: \"50% 0%\",\n            transform: `rotate(${rulerTilt}deg)`,\n            opacity: inP,\n          }}\n        >\n          <svg\n            width={rulerW}\n            height={em * RULER_H}\n            style={{ display: \"block\", overflow: \"visible\" }}\n            aria-hidden\n          >\n            <defs>\n              <clipPath id={clipId}>\n                <rect\n                  x={0}\n                  y={0}\n                  width={rulerW}\n                  height={em * RULER_H}\n                  rx={em * 0.04}\n                />\n              </clipPath>\n            </defs>\n            <rect\n              x={0.5}\n              y={0.5}\n              width={rulerW - 1}\n              height={em * RULER_H - 1}\n              rx={em * 0.04}\n              fill={t.rulerFill}\n              stroke={t.rulerLine}\n              strokeWidth={1}\n            />\n            <g clipPath={`url(#${clipId})`}>\n              {ticks.map((tk) => (\n                <line\n                  key={tk.x}\n                  x1={tk.x}\n                  x2={tk.x}\n                  y1={0}\n                  y2={tk.h * em}\n                  stroke={t.tick}\n                  strokeWidth={1}\n                />\n              ))}\n              {ticks.map((tk) =>\n                tk.n === null ? null : (\n                  <text\n                    key={`n${tk.x}`}\n                    x={tk.x + em * 0.035}\n                    y={em * 0.33}\n                    fill={t.num}\n                    fontFamily={mono.fontFamily}\n                    fontSize={em * 0.095}\n                  >\n                    {tk.n}\n                  </text>\n                ),\n              )}\n            </g>\n            {/* the straight edge */}\n            <rect x={0} y={0} width={rulerW} height={hair} fill={accent} />\n          </svg>\n        </div>\n\n        {glyphs}\n      </div>\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/ruler-align.tsx"
    }
  ],
  "type": "registry:component"
}
