{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ink-highlight",
  "title": "Ink highlight",
  "description": "A mark a pen makes on words in running text: underline, double underline, circle, box, strike or hatch fill. Inline in a paragraph, one mark per line when the words wrap.",
  "dependencies": [
    "svg-scroll-draw@^2.29.0"
  ],
  "registryDependencies": [
    "https://scrollfx-ui.vercel.app/r/plotter.json",
    "https://scrollfx-ui.vercel.app/r/ink.json"
  ],
  "files": [
    {
      "path": "registry/ui/ink-highlight.tsx",
      "content": "'use client';\n\n/**\n * ink-highlight — a mark a pen makes on words in running text: an underline,\n * a double underline, a loop round them, a box, a line struck through, or\n * hatching behind. It sits inline in a paragraph and follows the words when\n * they wrap: each line the phrase takes gets its own mark, drawn in turn.\n *\n * Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx\n * Needs: react, svg-scroll-draw, lib/plotter and lib/ink (installed with it).\n * Styling: Tailwind CSS. The words keep the paragraph's own type and colour.\n *\n *   <p>Most sections <InkHighlight variant=\"circle\">sit still</InkHighlight>.</p>\n *\n * The words are plain text in the server HTML; the marks are made in the\n * browser, measured to the words, and made again when the layout changes.\n * Reduced motion shows them drawn.\n */\n\nimport { useEffect, useRef, useState, type ElementType, type ReactNode } from 'react';\nimport 'svg-scroll-draw/no-flash.css';\nimport { hatch, inkBox, inkLoop, inkStrike, inkUnderline, type Stroke } from '@/lib/plotter';\nimport { useInk, type InkTrigger } from '@/lib/ink';\n\nexport type InkHighlightVariant = 'underline' | 'double-underline' | 'circle' | 'box' | 'strike' | 'hatch-fill';\n\nexport interface InkHighlightProps {\n  children: ReactNode;\n  variant?: InkHighlightVariant;\n  /** The pen's colour. */\n  accent?: string;\n  strokeWidth?: number;\n  /** Shapes the marks; the same seed draws the same marks. */\n  seed?: number;\n  /** `scroll`: drawn as the words rise, done at mid-screen. `view`: played once as they come into view. */\n  trigger?: InkTrigger;\n  /** With `view`, how long each line's mark takes, in ms. */\n  duration?: number;\n  /** The element the words sit in: `mark` by default, `s` for a strike. */\n  as?: ElementType;\n  className?: string;\n}\n\ninterface Placed {\n  /** Where the mark's box sits, from the highlight's own origin, in px. */\n  x: number;\n  y: number;\n  w: number;\n  h: number;\n  strokes: Stroke[];\n}\n\n/** One mark per line the words take, sized to that line's box. */\nfunction place(variant: InkHighlightVariant, r: { x: number; y: number; w: number; h: number }, seed: number): Placed {\n  const { x, y, w, h } = r;\n  switch (variant) {\n    case 'underline': {\n      const box = { x: x - 3, y: y + h * 0.84, w: w + 6, h: 12 };\n      return { ...box, strokes: inkUnderline({ w: box.w, h: box.h, seed }) };\n    }\n    case 'double-underline': {\n      const box = { x: x - 3, y: y + h * 0.82, w: w + 6, h: 16 };\n      return { ...box, strokes: inkUnderline({ w: box.w, h: box.h, seed, double: true }) };\n    }\n    case 'circle': {\n      // An ellipse wide enough to hold the words' corners, or nearly.\n      const px = 3 + h * 0.12, py = h * 0.4;\n      const box = { x: x - px, y: y - py, w: w + px * 2, h: h + py * 2 };\n      return { ...box, strokes: inkLoop({ w: box.w, h: box.h, seed }) };\n    }\n    case 'box': {\n      // inkBox draws 4 px in from its box's edge: 5 px clear of the words left and right, 1 px above and below.\n      const px = 9, py = 5;\n      const box = { x: x - px, y: y - py, w: w + px * 2, h: h + py * 2 };\n      return { ...box, strokes: inkBox({ w: box.w, h: box.h, seed }) };\n    }\n    case 'strike': {\n      const box = { x: x - 3, y, w: w + 6, h };\n      return { ...box, strokes: inkStrike({ w: box.w, h: box.h, seed }) };\n    }\n    case 'hatch-fill': {\n      // Behind the lower part of the letters, as a highlighter pen sits.\n      const box = { x: x - 5, y: y + h * 0.32, w: w + 10, h: Math.round(h * 0.6) };\n      return { ...box, strokes: hatch({ w: box.w, h: box.h, gap: 5, angle: 62 }) };\n    }\n  }\n}\n\nfunction Mark({ mark, variant, accent, strokeWidth, trigger, duration }: { mark: Placed; variant: InkHighlightVariant; accent: string; strokeWidth: number; trigger: InkTrigger; duration: number }) {\n  const ref = useRef<SVGSVGElement>(null);\n  const n = mark.strokes.length;\n  // Hatching sweeps left to right; a box's sides and a double underline's strokes follow one another.\n  const spread = variant === 'hatch-fill' ? 1 : variant === 'box' ? n - 1 : n > 1 ? 0.6 : 0;\n  useInk(ref, { paths: n, spread, trigger, duration, version: mark });\n  return (\n    <svg\n      ref={ref}\n      data-scroll-draw-pending=\"\"\n      data-ink={trigger}\n      aria-hidden\n      width={mark.w}\n      height={mark.h}\n      viewBox={`0 0 ${mark.w} ${mark.h}`}\n      className={`pointer-events-none absolute overflow-visible ${variant === 'hatch-fill' ? '-z-10 opacity-80' : ''}`}\n      style={{ left: mark.x, top: mark.y }}\n      fill=\"none\"\n      stroke={accent}\n      strokeWidth={variant === 'hatch-fill' ? strokeWidth * 0.9 : strokeWidth}\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n    >\n      {mark.strokes.map((s, i) => (\n        <path key={i} d={s.d} />\n      ))}\n    </svg>\n  );\n}\n\nexport function InkHighlight({\n  children,\n  variant = 'underline',\n  accent = '#e0482a',\n  strokeWidth = 1.8,\n  seed = 1,\n  trigger = 'scroll',\n  duration = 900,\n  as,\n  className = '',\n}: InkHighlightProps) {\n  const ref = useRef<HTMLElement>(null);\n  const origin = useRef<HTMLSpanElement>(null);\n  const [marks, setMarks] = useState<Placed[]>([]);\n  const Tag = as ?? (variant === 'strike' ? 's' : 'mark');\n\n  useEffect(() => {\n    const el = ref.current, o = origin.current;\n    if (!el || !o) return;\n    let last = '';\n    const measure = () => {\n      // The origin is an empty box at left 0, top 0 of the highlight's own\n      // containing block, wherever a browser puts that for words broken over\n      // lines, so the marks land where they are measured.\n      const at = o.getBoundingClientRect();\n      const lines = [...el.getClientRects()]\n        .filter((r) => r.width > 1 && r.height > 1)\n        .map((r) => ({ x: Math.round(r.left - at.left), y: Math.round(r.top - at.top), w: Math.round(r.width), h: Math.round(r.height) }));\n      const sig = JSON.stringify(lines);\n      if (sig === last) return;\n      last = sig;\n      setMarks(lines.map((r, i) => place(variant, r, seed + i * 7)));\n    };\n    measure();\n    // Words reflow when their block changes width, or when a web font arrives.\n    let block: HTMLElement | null = el.parentElement;\n    while (block && getComputedStyle(block).display.startsWith('inline')) block = block.parentElement;\n    const ro = new ResizeObserver(measure);\n    ro.observe(block ?? document.body);\n    document.fonts?.ready.then(measure);\n    return () => ro.disconnect();\n  }, [variant, seed]);\n\n  return (\n    // <mark>'s own yellow and black are reset here; `className` goes on the\n    // words inside, so a colour passed in wins.\n    <Tag ref={ref} className={`relative bg-transparent text-inherit no-underline ${variant === 'hatch-fill' ? 'isolate' : ''}`}>\n      <span ref={origin} aria-hidden className=\"pointer-events-none absolute left-0 top-0 size-0\" />\n      {className ? <span className={className}>{children}</span> : children}\n      {marks.map((m, i) => (\n        <Mark key={`${i}:${m.x},${m.y},${m.w}x${m.h}`} mark={m} variant={variant} accent={accent} strokeWidth={strokeWidth} trigger={trigger} duration={duration} />\n      ))}\n    </Tag>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/scrollfx/ink-highlight.tsx"
    }
  ],
  "type": "registry:ui"
}