{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"line-reveal","type":"registry:ui","title":"LineReveal","description":"Staggered line-by-line text reveal with lines computed by pretext instead of DOM splitting","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["text"],"dependencies":["@chenglou/pretext@^0.0.5"],"registryDependencies":["https://fancy-ui.rama.app/r/internals-dom.json","https://fancy-ui.rama.app/r/utils.json"],"docs":"Installs @chenglou/pretext as a dependency.","files":[{"path":"react/src/components/line-reveal/LineReveal.tsx","content":"\"use client\";\n\nimport { useEffect, useMemo, useState } from \"react\";\nimport {\n\tprepareWithSegments,\n\tlayoutWithLines,\n\ttype PreparedTextWithSegments,\n} from \"@chenglou/pretext\";\nimport { cn } from \"../../utils\";\nimport { useElementRef } from \"../../internals/dom/use-element-ref\";\nimport { useLiveRef } from \"../../internals/dom/use-live-ref\";\nimport \"./line-reveal.css\";\n\n/**\n * Props for LineReveal\n *\n * Reveals text line by line with a staggered slide-up animation.\n * Line breaks are computed with @chenglou/pretext (canvas text\n * measurement), so no DOM splitting or reflow measurement is needed.\n */\nexport interface LineRevealProps {\n\t/** Text to reveal */\n\ttext: string;\n\t/**\n\t * CSS font shorthand used for both measurement and rendering\n\t * (canvas `ctx.font` format, e.g. \"600 32px Inter, sans-serif\").\n\t * Prefer named families: `system-ui` can resolve differently between\n\t * canvas measurement and DOM rendering.\n\t */\n\tfont?: string;\n\t/** Line height in pixels (defaults to 1.2 × font size) */\n\tlineHeight?: number;\n\t/** Delay between lines in seconds */\n\tstagger?: number;\n\t/** Animation duration per line in seconds */\n\tduration?: number;\n\t/** Initial delay before the first line in seconds */\n\tdelay?: number;\n\t/** Animate only the first time the component enters the viewport */\n\tonce?: boolean;\n\t/** Additional CSS classes */\n\tclassName?: string;\n}\n\n/**\n * First `<n>px` length in a CSS font shorthand, with the optional `/line-height`\n * that may already follow it. Group 1 is the font size; the whole match is what\n * gets rewritten when the resolved line height is folded into the shorthand.\n */\nconst FONT_SIZE_PX = /(\\d+(?:\\.\\d+)?)px(?:\\s*\\/\\s*[^\\s,]+)?/;\n\nexport function LineReveal({\n\ttext,\n\t// Named families only: canvas and CSS can resolve `system-ui` to\n\t// different fonts (notably on macOS), which skews measured widths.\n\tfont = '600 32px \"Helvetica Neue\", Helvetica, Arial, sans-serif',\n\tlineHeight,\n\tstagger = 0.08,\n\tduration = 0.7,\n\tdelay = 0,\n\tonce = true,\n\tclassName,\n}: LineRevealProps) {\n\tconst fontSize = Number(FONT_SIZE_PX.exec(font)?.[1] ?? 32);\n\tconst resolvedLineHeight = lineHeight ?? Math.round(fontSize * 1.2);\n\n\t// The `font` shorthand resets `line-height`, and only the style declarations\n\t// whose value changed are re-applied on update — so a font-only change (same\n\t// px size, different weight or family) would set `font` without re-applying a\n\t// separately declared `line-height`, and the lines would drift inside their\n\t// fixed-height masks. One declaration carries both instead: the line height is\n\t// folded into the shorthand, and the `line-height` longhand is only used when\n\t// there is no px size to fold into (never both — React warns about mixing a\n\t// shorthand with a longhand for the same value). The `font` prop itself is\n\t// left untouched for canvas measurement, where `ctx.font` ignores the line\n\t// height anyway.\n\tconst foldable = FONT_SIZE_PX.test(font);\n\tconst fontWithLineHeight = foldable\n\t\t? font.replace(FONT_SIZE_PX, (_match: string, size: string) => `${size}px/${resolvedLineHeight}px`)\n\t\t: font;\n\n\tconst [container, containerRef] = useElementRef<HTMLDivElement>();\n\tconst [containerWidth, setContainerWidth] = useState(0);\n\tconst [revealed, setRevealed] = useState(false);\n\tconst [prepared, setPrepared] = useState<PreparedTextWithSegments | null>(null);\n\n\t// Measurement requires a canvas context, so this only runs in the browser\n\t// (an effect never runs during SSR). Web fonts must be loaded first or the\n\t// canvas silently measures with the fallback font.\n\tuseEffect(() => {\n\t\tlet cancelled = false;\n\n\t\t// Both steps are optional at runtime: `document.fonts` is missing wherever\n\t\t// FontFaceSet is not implemented, and measurement needs a canvas 2d context.\n\t\t// Either absence lands on the pre-measure fallback below instead of throwing\n\t\t// out of the effect and tearing the tree down.\n\t\tPromise.resolve(document.fonts?.load(font))\n\t\t\t.catch(() => {})\n\t\t\t.then(() => {\n\t\t\t\tif (cancelled) return;\n\t\t\t\ttry {\n\t\t\t\t\tsetPrepared(prepareWithSegments(text, font));\n\t\t\t\t} catch {\n\t\t\t\t\tsetPrepared(null);\n\t\t\t\t}\n\t\t\t});\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t};\n\t}, [text, font]);\n\n\t// The React counterpart of Svelte's `bind:clientWidth`: a ResizeObserver on\n\t// the same element, plus one read at attach time so the first layout does\n\t// not wait for a resize.\n\tuseEffect(() => {\n\t\tif (!container) return;\n\n\t\tconst measure = () => setContainerWidth(container.clientWidth);\n\t\tmeasure();\n\n\t\tconst observer = new ResizeObserver(measure);\n\t\tobserver.observe(container);\n\n\t\treturn () => observer.disconnect();\n\t}, [container]);\n\n\t// Pure arithmetic on cached measurements — cheap to re-run on every resize.\n\t// Memoised on exactly what the Svelte `$derived` tracks, so toggling\n\t// `revealed` re-renders without redoing the line breaking.\n\tconst layoutResult = useMemo(\n\t\t() =>\n\t\t\tprepared && containerWidth > 0\n\t\t\t\t? layoutWithLines(prepared, containerWidth, resolvedLineHeight)\n\t\t\t\t: null,\n\t\t[prepared, containerWidth, resolvedLineHeight]\n\t);\n\n\t// The Svelte source reads `once` inside the observer callback, where a prop\n\t// is a live getter — so a change takes effect without rebuilding the\n\t// observer. A live ref reproduces that; putting `once` in the dependency\n\t// array instead would tear down and re-observe, replaying the reveal.\n\tconst onceRef = useLiveRef(once);\n\n\tuseEffect(() => {\n\t\tif (!container) return;\n\n\t\tconst observer = new IntersectionObserver(\n\t\t\t(entries) => {\n\t\t\t\tfor (const entry of entries) {\n\t\t\t\t\tif (entry.isIntersecting) {\n\t\t\t\t\t\tsetRevealed(true);\n\t\t\t\t\t\tif (onceRef.current) observer.disconnect();\n\t\t\t\t\t} else if (!onceRef.current) {\n\t\t\t\t\t\tsetRevealed(false);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t{ threshold: 0.1 }\n\t\t);\n\n\t\tobserver.observe(container);\n\n\t\treturn () => observer.disconnect();\n\t}, [container, onceRef]);\n\n\treturn (\n\t\t<div\n\t\t\tref={containerRef}\n\t\t\tclassName={cn(\"line-reveal\", \"relative w-full\", className)}\n\t\t\tstyle={{\n\t\t\t\tfont: fontWithLineHeight,\n\t\t\t\t...(foldable ? null : { lineHeight: `${resolvedLineHeight}px` }),\n\t\t\t\theight: layoutResult ? `${layoutResult.height}px` : undefined,\n\t\t\t}}\n\t\t>\n\t\t\t<span className=\"sr-only\">{text}</span>\n\t\t\t{layoutResult ? (\n\t\t\t\tlayoutResult.lines.map((line, i) => (\n\t\t\t\t\t<div\n\t\t\t\t\t\tkey={i}\n\t\t\t\t\t\tclassName=\"line-mask\"\n\t\t\t\t\t\tstyle={{ height: `${resolvedLineHeight}px` }}\n\t\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<div\n\t\t\t\t\t\t\tclassName={cn(\"line\", revealed && \"revealed\")}\n\t\t\t\t\t\t\tstyle={{\n\t\t\t\t\t\t\t\ttransitionDuration: `${duration}s`,\n\t\t\t\t\t\t\t\ttransitionDelay: `${delay + i * stagger}s`,\n\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{line.text}\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n\t\t\t\t))\n\t\t\t) : (\n\t\t\t\t// SSR / pre-measure fallback: keeps text in the HTML and reserves space\n\t\t\t\t<div style={{ visibility: \"hidden\" }} aria-hidden=\"true\">\n\t\t\t\t\t{text}\n\t\t\t\t</div>\n\t\t\t)}\n\t\t</div>\n\t);\n}\n","type":"registry:ui","target":"@ui/fancy/components/line-reveal/LineReveal.tsx"},{"path":"react/src/components/line-reveal/index.ts","content":"\"use client\";\n\nexport { LineReveal } from \"./LineReveal\";\nexport type { LineRevealProps } from \"./LineReveal\";\n","type":"registry:ui","target":"@ui/fancy/components/line-reveal/index.ts"},{"path":"react/src/components/line-reveal/line-reveal.css","content":"/*\n * `.line-reveal` is a PORT-ADDED anchor (PORTING.md §Styling rule 2). The Svelte\n * source has no root class: its compiler scoped `.line-mask` / `.line` /\n * `.revealed`, which are generic enough to collide with a consumer's own rules\n * once the styles ship as plain global CSS. The anchor is the first token of the\n * root `cn()` call in LineReveal.tsx.\n */\n\n.line-reveal .line-mask {\n\toverflow: hidden;\n\t/* preserve exact spacing so rendered width matches pretext measurement */\n\twhite-space: pre;\n}\n\n.line-reveal .line {\n\topacity: 0;\n\ttransform: translateY(105%);\n\ttransition-property: transform, opacity;\n\ttransition-timing-function: cubic-bezier(0.22, 1, 0.36, 1);\n}\n\n.line-reveal .line.revealed {\n\topacity: 1;\n\ttransform: translateY(0);\n}\n\n@media (prefers-reduced-motion: reduce) {\n\t.line-reveal .line {\n\t\ttransition-duration: 0.1s !important;\n\t\ttransition-delay: 0s !important;\n\t}\n}\n","type":"registry:ui","target":"@ui/fancy/components/line-reveal/line-reveal.css"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/line-reveal?framework=react"}}