useSplitLines
Measures how text wraps into lines so each line can animate on its own.
For text that is revealed or styled line by line, where the lines depend on the width. Render words into a hidden node that wraps exactly as the visible text does (the same width, font and padding) and attach measureRef to it; lines then holds the words of each row. It is null until the first measurement in the browser, so render the plain text until then, and it is measured again when the container resizes and when the fonts have loaded. @khaeli/text-reveal is built on it.
Lines as the container resizes
Every task in the March release has an owner, a due date and a reviewer. The notes go out on Wednesday morning.
"use client"
import { useState, type Ref } from "react"
import { SegmentedToggleGroup } from "@/components/ui/segmented-toggle-group"
import { ToggleGroupItem } from "@/components/ui/toggle-group"
import { useSplitLines } from "@/hooks/use-split-lines"
import { cn } from "@/lib/utils"
const text =
"Every task in the March release has an owner, a due date and a reviewer. The notes go out on Wednesday morning."
const widths: Record<string, string> = { narrow: "max-w-48", medium: "max-w-72", wide: "max-w-md" }
export default function UseSplitLinesLinesAsTheContainerResizes() {
const [width, setWidth] = useState(["medium"])
const { measureRef, words, lines } = useSplitLines(text)
return (
<div className="flex w-full flex-col items-center gap-6">
<SegmentedToggleGroup
aria-label="Width of the paragraph"
value={width}
onValueChange={(next) => next.length > 0 && setWidth(next)}
>
<ToggleGroupItem value="narrow">Narrow</ToggleGroupItem>
<ToggleGroupItem value="medium">Medium</ToggleGroupItem>
<ToggleGroupItem value="wide">Wide</ToggleGroupItem>
</SegmentedToggleGroup>
<div className={cn("relative w-full", widths[width[0] ?? "medium"])}>
{/* The measuring node: one box per word, laid out as the paragraph wraps, never shown or read. */}
<span
ref={measureRef as Ref<HTMLSpanElement>}
aria-hidden="true"
className="invisible absolute inset-x-0 top-0 block pl-8"
>
{words.map((word, index) => (
<span key={index} className="inline-block whitespace-pre">
{word}{" "}
</span>
))}
</span>
<p>
{lines ? (
lines.map((line, index) => (
<span key={index} className="flex">
<span aria-hidden="true" className="w-8 shrink-0 text-muted-foreground tabular-nums">
{index + 1}
</span>
<span className="whitespace-nowrap">{line.join(" ")} </span>
</span>
))
) : (
// Until the first measurement: the same text in the same place, so nothing moves.
<span className="block pl-8">{text}</span>
)}
</p>
</div>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/use-split-linesbunx --bun shadcn@latest add @khaeli/use-split-linesInstalls with
Nothing else. This item needs no other Registry item and no npm package.
Accessible name
- Hide the measuring node with
aria-hidden: it repeats the text. Keep the lines inside one paragraph, so a screen reader reads a sentence and not a list of fragments.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/use-split-lines.json