Most sections sit still. This one is marked up as you read, the way a proof comes back from an editor.
- 1children · the words
- 2variant · underline
Schedule of props
| Mark | Prop | Type | Default | Description |
|---|---|---|---|---|
| P1 | children* | ReactNode | — | — |
| P2 | variant | InkHighlightVariant | 'underline' | — |
| P3 | accent | string | '#e0482a' | The pen's colour. |
| P4 | strokeWidth | number | 1.8 | — |
| P5 | seed | number | 1 | Shapes the marks; the same seed draws the same marks. |
| P6 | trigger | InkTrigger | 'scroll' | `scroll`: drawn as the words rise, done at mid-screen. `view`: played once as they come into view. |
| P7 | duration | number | 900 | With `view`, how long each line's mark takes, in ms. |
| P8 | as | ElementType | — | The element the words sit in: `mark` by default, `s` for a strike. |
| P9 | className | string | '' | — |
* required
Notes
- 1.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.
- 2.The words and content it marks stay in the server HTML. The marks are measured to them in the browser, and made again when the layout changes.
- 3.Tied to the scroll by default: the last stroke lands as the drawing reaches the middle of the screen. With
trigger="view"it plays once as it comes into view instead. Reduced motion shows it drawn.
- 4.Detail A, Underline. One stroke under the words, pressing in from the left and lifting at the end.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function UnderlineExample() { return ( <p className={P}> Most sections sit still. This one is <InkHighlight>marked up as you read</InkHighlight>, the way a proof comes back from an editor. </p> ); }
- 4.Detail B, Double underline. A second, shorter stroke under the first, for the word that matters most.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function DoubleUnderlineExample() { return ( <p className={P}> Ship it on Friday, but <InkHighlight variant="double-underline">only if the tests pass</InkHighlight>. </p> ); }
- 4.Detail C, Circle. A loop round the words, a little more than one turn, its end passing outside its start.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function CircleExample() { return ( <p className={P}> Three plans, and most teams start on <InkHighlight variant="circle" seed={4}>Studio</InkHighlight>. </p> ); }
- 4.Detail D, Box. Four strokes ruled round the words, one after another, crossing at the corners.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function BoxExample() { return ( <p className={P}> Every chart opens into <InkHighlight variant="box" seed={3}>the rows behind it</InkHighlight>, and every row into the event that made it. </p> ); }
- 4.Detail E, Strike. A line struck through, for a price that changed or a claim withdrawn. Rendered as <s>.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function StrikeExample() { return ( <p className={P}> Plans from <InkHighlight variant="strike">$24</InkHighlight> $18 a month, billed yearly. </p> ); }
- 4.Detail F, Hatch fill. Diagonal hatching behind the lower half of the letters, plotted left to right.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; const P = 'max-w-2xl text-pretty font-serif text-[clamp(1.6rem,3vw,2.3rem)] leading-[1.3] tracking-[-0.015em]'; export function HatchFillExample() { return ( <p className={P}> Drawings are made in the browser; <InkHighlight variant="hatch-fill">the words stay in the HTML</InkHighlight>, for search and for screen readers. </p> ); }
- 4.Detail G, Across lines. A phrase that wraps gets a mark for each line it takes, drawn one after the other as each line reaches the middle of the screen.
- 5.Usage, as drawn:
import { InkHighlight } from '@/components/scrollfx/ink-highlight'; export function AcrossLinesExample() { return ( <p className="max-w-md text-pretty text-[17px] leading-relaxed text-muted-foreground"> The marks are measured to the words and made again when the layout changes, so{' '} <InkHighlight variant="underline" seed={9} className="text-foreground">a phrase that breaks over two or three lines in a narrow column</InkHighlight>{' '} is underlined on each of them, and{' '} <InkHighlight variant="hatch-fill" seed={2} className="text-foreground">hatching follows it round the line break</InkHighlight> too. </p> ); }
View source · components/scrollfx/ink-highlight.tsx
'use client';
/**
* ink-highlight — a mark a pen makes on words in running text: an underline,
* a double underline, a loop round them, a box, a line struck through, or
* hatching behind. It sits inline in a paragraph and follows the words when
* they wrap: each line the phrase takes gets its own mark, drawn in turn.
*
* Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx
* Needs: react, svg-scroll-draw, lib/plotter and lib/ink (installed with it).
* Styling: Tailwind CSS. The words keep the paragraph's own type and colour.
*
* <p>Most sections <InkHighlight variant="circle">sit still</InkHighlight>.</p>
*
* The words are plain text in the server HTML; the marks are made in the
* browser, measured to the words, and made again when the layout changes.
* Reduced motion shows them drawn.
*/
import { useEffect, useRef, useState, type ElementType, type ReactNode } from 'react';
import 'svg-scroll-draw/no-flash.css';
import { hatch, inkBox, inkLoop, inkStrike, inkUnderline, type Stroke } from '@/lib/plotter';
import { useInk, type InkTrigger } from '@/lib/ink';
export type InkHighlightVariant = 'underline' | 'double-underline' | 'circle' | 'box' | 'strike' | 'hatch-fill';
export interface InkHighlightProps {
children: ReactNode;
variant?: InkHighlightVariant;
/** The pen's colour. */
accent?: string;
strokeWidth?: number;
/** Shapes the marks; the same seed draws the same marks. */
seed?: number;
/** `scroll`: drawn as the words rise, done at mid-screen. `view`: played once as they come into view. */
trigger?: InkTrigger;
/** With `view`, how long each line's mark takes, in ms. */
duration?: number;
/** The element the words sit in: `mark` by default, `s` for a strike. */
as?: ElementType;
className?: string;
}
interface Placed {
/** Where the mark's box sits, from the highlight's own origin, in px. */
x: number;
y: number;
w: number;
h: number;
strokes: Stroke[];
}
/** One mark per line the words take, sized to that line's box. */
function place(variant: InkHighlightVariant, r: { x: number; y: number; w: number; h: number }, seed: number): Placed {
const { x, y, w, h } = r;
switch (variant) {
case 'underline': {
const box = { x: x - 3, y: y + h * 0.84, w: w + 6, h: 12 };
return { ...box, strokes: inkUnderline({ w: box.w, h: box.h, seed }) };
}
case 'double-underline': {
const box = { x: x - 3, y: y + h * 0.82, w: w + 6, h: 16 };
return { ...box, strokes: inkUnderline({ w: box.w, h: box.h, seed, double: true }) };
}
case 'circle': {
// An ellipse wide enough to hold the words' corners, or nearly.
const px = 3 + h * 0.12, py = h * 0.4;
const box = { x: x - px, y: y - py, w: w + px * 2, h: h + py * 2 };
return { ...box, strokes: inkLoop({ w: box.w, h: box.h, seed }) };
}
case 'box': {
// inkBox draws 4 px in from its box's edge: 5 px clear of the words left and right, 1 px above and below.
const px = 9, py = 5;
const box = { x: x - px, y: y - py, w: w + px * 2, h: h + py * 2 };
return { ...box, strokes: inkBox({ w: box.w, h: box.h, seed }) };
}
case 'strike': {
const box = { x: x - 3, y, w: w + 6, h };
return { ...box, strokes: inkStrike({ w: box.w, h: box.h, seed }) };
}
case 'hatch-fill': {
// Behind the lower part of the letters, as a highlighter pen sits.
const box = { x: x - 5, y: y + h * 0.32, w: w + 10, h: Math.round(h * 0.6) };
return { ...box, strokes: hatch({ w: box.w, h: box.h, gap: 5, angle: 62 }) };
}
}
}
function Mark({ mark, variant, accent, strokeWidth, trigger, duration }: { mark: Placed; variant: InkHighlightVariant; accent: string; strokeWidth: number; trigger: InkTrigger; duration: number }) {
const ref = useRef<SVGSVGElement>(null);
const n = mark.strokes.length;
// Hatching sweeps left to right; a box's sides and a double underline's strokes follow one another.
const spread = variant === 'hatch-fill' ? 1 : variant === 'box' ? n - 1 : n > 1 ? 0.6 : 0;
useInk(ref, { paths: n, spread, trigger, duration, version: mark });
return (
<svg
ref={ref}
data-scroll-draw-pending=""
data-ink={trigger}
aria-hidden
width={mark.w}
height={mark.h}
viewBox={`0 0 ${mark.w} ${mark.h}`}
className={`pointer-events-none absolute overflow-visible ${variant === 'hatch-fill' ? '-z-10 opacity-80' : ''}`}
style={{ left: mark.x, top: mark.y }}
fill="none"
stroke={accent}
strokeWidth={variant === 'hatch-fill' ? strokeWidth * 0.9 : strokeWidth}
strokeLinecap="round"
strokeLinejoin="round"
>
{mark.strokes.map((s, i) => (
<path key={i} d={s.d} />
))}
</svg>
);
}
export function InkHighlight({
children,
variant = 'underline',
accent = '#e0482a',
strokeWidth = 1.8,
seed = 1,
trigger = 'scroll',
duration = 900,
as,
className = '',
}: InkHighlightProps) {
const ref = useRef<HTMLElement>(null);
const origin = useRef<HTMLSpanElement>(null);
const [marks, setMarks] = useState<Placed[]>([]);
const Tag = as ?? (variant === 'strike' ? 's' : 'mark');
useEffect(() => {
const el = ref.current, o = origin.current;
if (!el || !o) return;
let last = '';
const measure = () => {
// The origin is an empty box at left 0, top 0 of the highlight's own
// containing block, wherever a browser puts that for words broken over
// lines, so the marks land where they are measured.
const at = o.getBoundingClientRect();
const lines = [...el.getClientRects()]
.filter((r) => r.width > 1 && r.height > 1)
.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) }));
const sig = JSON.stringify(lines);
if (sig === last) return;
last = sig;
setMarks(lines.map((r, i) => place(variant, r, seed + i * 7)));
};
measure();
// Words reflow when their block changes width, or when a web font arrives.
let block: HTMLElement | null = el.parentElement;
while (block && getComputedStyle(block).display.startsWith('inline')) block = block.parentElement;
const ro = new ResizeObserver(measure);
ro.observe(block ?? document.body);
document.fonts?.ready.then(measure);
return () => ro.disconnect();
}, [variant, seed]);
return (
// <mark>'s own yellow and black are reset here; `className` goes on the
// words inside, so a colour passed in wins.
<Tag ref={ref} className={`relative bg-transparent text-inherit no-underline ${variant === 'hatch-fill' ? 'isolate' : ''}`}>
<span ref={origin} aria-hidden className="pointer-events-none absolute left-0 top-0 size-0" />
{className ? <span className={className}>{children}</span> : children}
{marks.map((m, i) => (
<Mark key={`${i}:${m.x},${m.y},${m.w}x${m.h}`} mark={m} variant={variant} accent={accent} strokeWidth={strokeWidth} trigger={trigger} duration={duration} />
))}
</Tag>
);
}
Parts list
| Item | Part, as installed |
|---|---|
| 1 | components/scrollfx/ink-highlight.tsxThe component |
| 2 | lib/ink.tsThe pen hook the ink pieces share, and Hershey’s script |
| 3 | lib/plotter.tsStroke geometry: hatching, sketched lines, scroll windows |
| 4 | svg-scroll-draw ^2.29.0Dependency, from npm |
ViewUnderline
Pens1ink2vermilion
Install
npx shadcn@latest add
https://scrollfx-ui.vercel.app/r/ink-highlight.jsonTitle
Ink highlight
scrollfx · Ink- Dwg no.
- ink-highlight
- Sheet
- 08/14
- Scale
- 1:1
- Rev
- 2.29.0
- Drawn by
- svg-scroll-draw
- Licence
- MIT