{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "draw-on",
  "type": "registry:ui",
  "title": "Draw On",
  "description": "A line drawing that draws itself as it scrolls into view: an illustration, a diagram, an arrow that points at the next thing, a signature. Wrap any inline SVG; every stroked shape inside traces its own line, one after another in document order, and filled…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "decorative",
    "effect"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/motion-token.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/effects/draw-on.tsx",
      "target": "components/ui/effects/draw-on.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/draw-on v1.0.1 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/draw-on\n// What changed since: https://ds.interlace.tools/c/draw-on#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — DrawOn\n *\n * A line drawing that draws itself as it scrolls into view: an illustration,\n * a diagram, an arrow that points at the next thing, a signature. Wrap any\n * inline SVG; every stroked shape inside traces its own line, one after\n * another in document order, and filled shapes fade in once the lines are\n * down. It is the motion of a pen on paper, so it suits a calm, editorial\n * page where line drawings stand in for photographs.\n *\n * ## Content first\n *\n * The server HTML holds the finished drawing, so a crawler, a reader without\n * scripts and a print stylesheet all get it. The browser hides the lines just\n * before the first paint and draws them as the drawing enters the viewport,\n * once. Nothing is added to or removed from the SVG, so its own `role`,\n * `aria-label` or `aria-hidden` stays exactly as you wrote it.\n *\n * ## Motion\n *\n * Each shape is measured with `getTotalLength()` and drawn by animating\n * `stroke-dashoffset` on the Web Animations API, so there are no keyframes to\n * add and no length to type by hand. When a line finishes, its dash styles are\n * removed, so the drawing ends exactly as authored (dashed strokes included).\n * Under `prefers-reduced-motion: reduce`, without IntersectionObserver, or\n * without `element.animate`, the drawing is simply there. Drawings already in\n * view on load draw straight away.\n *\n * ## Composition\n *\n *   DrawOn                              (div — data-slot=\"draw-on\", data-drawn)\n *     └─ your <svg>                     (untouched: its own a11y attributes)\n *\n * | Rule | Concept                 | Where in this file                                   |\n * | ---- | ----------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el       | `div` props                                          |\n * | R6   | data-slot per part      | draw-on                                              |\n * | R7   | cn + ...rest            | `cn(className)` + `{...props}`                       |\n * | R8   | Enums, not booleans     | `order`                                              |\n * | R25  | Client component        | the drawing runs in the browser                      |\n * | R26  | A11y from native el     | the SVG keeps its own semantics                      |\n */\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\nimport { motionEase } from '@/lib/motion-token';\n\n/** The SVG shapes that have a length to draw. */\nconst SHAPES = 'path, line, polyline, polygon, circle, ellipse, rect';\n\n\nexport type DrawOnOrder = 'sequence' | 'together';\n\nexport interface DrawOnProps extends React.ComponentPropsWithoutRef<'div'> {\n  /** `sequence` draws one line after another in document order; `together` draws them all at once. @default 'sequence' */\n  order?: DrawOnOrder;\n  /** Milliseconds each line takes to draw. @default 1200 */\n  duration?: number;\n  /** Milliseconds between the start of one line and the next, for `order=\"sequence\"`. @default 150 */\n  stagger?: number;\n  /** Milliseconds to wait after the drawing enters the viewport. @default 0 */\n  delay?: number;\n  /** How much of the drawing must be visible before it draws, from 0 to 1. @default 0.3 */\n  amount?: number;\n}\n\ninterface Stroke {\n  el: SVGGeometryElement;\n  length: number;\n}\n\nfunction isStroked(el: Element): boolean {\n  const style = getComputedStyle(el);\n  return style.stroke !== '' && style.stroke !== 'none' && parseFloat(style.strokeWidth || '1') > 0;\n}\n\nfunction isFilled(el: Element): boolean {\n  const fill = getComputedStyle(el).fill;\n  return fill !== '' && fill !== 'none' && fill !== 'transparent' && fill !== 'rgba(0, 0, 0, 0)';\n}\n\n/** Measure every shape inside and hide it, ready to draw. Returns what to animate. */\nfunction hideShapes(root: HTMLElement): { strokes: Stroke[]; fills: SVGElement[] } {\n  const strokes: Stroke[] = [];\n  const fills: SVGElement[] = [];\n  for (const el of root.querySelectorAll<SVGGeometryElement>(SHAPES)) {\n    const length = isStroked(el) && typeof el.getTotalLength === 'function' ? el.getTotalLength() : 0;\n    if (length > 0) {\n      strokes.push({ el, length });\n      el.style.strokeDasharray = `${length} ${length}`;\n      el.style.strokeDashoffset = `${length}`;\n      // A filled, stroked shape fades its fill in from halfway through its outline, so the two resolve together.\n      if (isFilled(el)) el.style.fillOpacity = '0';\n    } else if (isFilled(el)) {\n      fills.push(el);\n      el.style.opacity = '0';\n    }\n  }\n  return { strokes, fills };\n}\n\nexport function DrawOn({\n  order = 'sequence',\n  duration = 1200,\n  stagger = 150,\n  delay = 0,\n  amount = 0.3,\n  className,\n  children,\n  ...props\n}: DrawOnProps) {\n  const reduced = useReducedMotion();\n  const ref = React.useRef<HTMLDivElement>(null);\n\n  // Before paint: hide the lines, so they can draw instead of flashing.\n  React.useLayoutEffect(() => {\n    const root = ref.current;\n    if (!root || reduced || typeof root.animate !== 'function') return;\n    if (typeof IntersectionObserver === 'undefined') return;\n    // Once drawn, a prop change (or a Fast Refresh) never draws it again.\n    if (root.dataset.drawn) return;\n\n    const { strokes, fills } = hideShapes(root);\n    if (strokes.length === 0 && fills.length === 0) return;\n\n    const clear = () => {\n      for (const { el } of strokes) {\n        el.style.strokeDasharray = '';\n        el.style.strokeDashoffset = '';\n        el.style.fillOpacity = '';\n      }\n      for (const el of fills) el.style.opacity = '';\n    };\n\n    let io: IntersectionObserver | undefined;\n    const draw = (wait: number) => {\n      if (root.dataset.drawn) return;\n      root.dataset.drawn = 'true';\n      io?.disconnect();\n      const step = order === 'sequence' ? stagger : 0;\n      const end = wait + (strokes.length > 0 ? (strokes.length - 1) * step + duration : 0);\n\n      strokes.forEach(({ el, length }, i) => {\n        const start = wait + i * step;\n        const line = el.animate([{ strokeDashoffset: `${length}` }, { strokeDashoffset: '0' }], {\n          duration,\n          delay: start,\n          easing: motionEase(el, 'move'),\n          fill: 'both',\n        });\n        line.onfinish = () => {\n          el.style.strokeDasharray = '';\n          el.style.strokeDashoffset = '';\n          line.cancel();\n        };\n        if (el.style.fillOpacity === '0') {\n          el.style.fillOpacity = '';\n          el.animate([{ fillOpacity: 0 }, { fillOpacity: 1 }], {\n            duration: duration / 2,\n            delay: start + duration / 2,\n            easing: motionEase(el, 'move'),\n            fill: 'backwards',\n          });\n        }\n      });\n      for (const el of fills) {\n        el.style.opacity = '';\n        el.animate([{ opacity: 0 }, { opacity: 1 }], {\n          duration: Math.min(duration, 600),\n          delay: Math.max(wait, end - Math.min(duration, 600)),\n          easing: motionEase(el, 'move'),\n          fill: 'backwards',\n        });\n      }\n    };\n\n    // A focusable element inside (a link in a diagram) shows the drawing at once.\n    const onFocus = () => {\n      if (root.dataset.drawn) return;\n      root.dataset.drawn = 'true';\n      io?.disconnect();\n      clear();\n    };\n    root.addEventListener('focusin', onFocus);\n\n    io = new IntersectionObserver(\n      (entries) => {\n        if (entries.some((e) => e.isIntersecting)) draw(delay);\n      },\n      { threshold: Math.min(1, Math.max(0, amount)) },\n    );\n    io.observe(root);\n\n    return () => {\n      io?.disconnect();\n      root.removeEventListener('focusin', onFocus);\n      for (const { el } of strokes) for (const a of el.getAnimations?.() ?? []) a.cancel();\n      for (const el of fills) for (const a of el.getAnimations?.() ?? []) a.cancel();\n      clear();\n    };\n  }, [reduced, order, duration, stagger, delay, amount]);\n\n  return (\n    <div ref={ref} data-slot=\"draw-on\" data-order={order} className={cn(className)} {...props}>\n      {children}\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "tier": "effect",
    "client": true,
    "minViewport": null,
    "loading": false,
    "version": "1.0.1",
    "since": "2.0.0"
  },
  "docs": "## @interlace/draw-on\n\nInstalled to `components/ui/effects/draw-on.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/effects/draw-on';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/draw-on\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
