~/components/eyebrow
Eyebrow
A small mono label for above a heading that types itself out, with a block cursor, as it scrolls into view.
// 02 — selected work
Things I built
The label types itself each time it scrolls into view.
"use client";
import { useEffect, useRef, useState, type ElementType } from "react";
export type EyebrowProps = {
/** The label, e.g. "// 02 — projects". */
text: string;
/** Element to render. Use "h2"/"p" when the label is part of your heading structure. @default "p" */
as?: ElementType;
/** Type the label in when it scrolls into view. false shows it as plain text. @default true */
typing?: boolean;
/** Milliseconds per typed character. @default 32 */
speed?: number;
/** Clear the label when it leaves the viewport, so it types again next time. @default true */
replay?: boolean;
/** Type even when the user prefers reduced motion. Use only behind an explicit action. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the root element. */
className?: string;
};
/**
* A small mono label that sits above a section heading and types itself out,
* with a blinking block cursor, each time it scrolls into view.
* The full text is rendered on the server and kept in a screen-reader-only copy,
* so there is no hydration mismatch and assistive tech reads it once, whole.
*/
export function Eyebrow({
text,
as: Tag = "p",
typing = true,
speed = 32,
replay = true,
ignoreReducedMotion = false,
className,
}: EyebrowProps) {
const ref = useRef<HTMLElement>(null);
const [n, setN] = useState(text.length);
useEffect(() => {
const el = ref.current;
if (!el || !typing) return;
if (!ignoreReducedMotion && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
let id: ReturnType<typeof setInterval> | undefined;
let typed = false;
const stop = () => {
if (id) clearInterval(id);
id = undefined;
};
const io = new IntersectionObserver(
([e]) => {
if (!e.isIntersecting) {
if (replay) {
stop();
typed = false;
setN(0);
}
return;
}
if (typed) return;
typed = true;
stop();
let i = 0;
setN(0);
id = setInterval(() => {
i += 1;
setN(i);
if (i >= text.length) stop();
}, speed);
},
{ threshold: 0.8 },
);
io.observe(el);
return () => {
io.disconnect();
stop();
setN(text.length);
};
}, [text, typing, speed, replay, ignoreReducedMotion]);
const shown = Math.min(n, text.length);
return (
<Tag ref={ref} className={["font-mpk-mono text-[13px] text-mpk-dim2", className].filter(Boolean).join(" ")}>
<span className="sr-only">{text}</span>
<span aria-hidden="true">
{text.slice(0, shown)}
{shown < text.length && (
<span className="inline-block h-[1em] w-[0.55em] -mb-[0.15em] animate-mpk-blink bg-mpk-dim2" />
)}
<span className="invisible">{text.slice(shown)}</span>
</span>
</Tag>
);
}Installation
npx mpkcn add eyebrow01Add mpkcn-theme.css if you haven't yet (see Installation)
02Copy the source into your components folder
"use client";
import { useEffect, useRef, useState, type ElementType } from "react";
export type EyebrowProps = {
/** The label, e.g. "// 02 — projects". */
text: string;
/** Element to render. Use "h2"/"p" when the label is part of your heading structure. @default "p" */
as?: ElementType;
/** Type the label in when it scrolls into view. false shows it as plain text. @default true */
typing?: boolean;
/** Milliseconds per typed character. @default 32 */
speed?: number;
/** Clear the label when it leaves the viewport, so it types again next time. @default true */
replay?: boolean;
/** Type even when the user prefers reduced motion. Use only behind an explicit action. @default false */
ignoreReducedMotion?: boolean;
/** Extra classes for the root element. */
className?: string;
};
/**
* A small mono label that sits above a section heading and types itself out,
* with a blinking block cursor, each time it scrolls into view.
* The full text is rendered on the server and kept in a screen-reader-only copy,
* so there is no hydration mismatch and assistive tech reads it once, whole.
*/
export function Eyebrow({
text,
as: Tag = "p",
typing = true,
speed = 32,
replay = true,
ignoreReducedMotion = false,
className,
}: EyebrowProps) {
const ref = useRef<HTMLElement>(null);
const [n, setN] = useState(text.length);
useEffect(() => {
const el = ref.current;
if (!el || !typing) return;
if (!ignoreReducedMotion && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
let id: ReturnType<typeof setInterval> | undefined;
let typed = false;
const stop = () => {
if (id) clearInterval(id);
id = undefined;
};
const io = new IntersectionObserver(
([e]) => {
if (!e.isIntersecting) {
if (replay) {
stop();
typed = false;
setN(0);
}
return;
}
if (typed) return;
typed = true;
stop();
let i = 0;
setN(0);
id = setInterval(() => {
i += 1;
setN(i);
if (i >= text.length) stop();
}, speed);
},
{ threshold: 0.8 },
);
io.observe(el);
return () => {
io.disconnect();
stop();
setN(text.length);
};
}, [text, typing, speed, replay, ignoreReducedMotion]);
const shown = Math.min(n, text.length);
return (
<Tag ref={ref} className={["font-mpk-mono text-[13px] text-mpk-dim2", className].filter(Boolean).join(" ")}>
<span className="sr-only">{text}</span>
<span aria-hidden="true">
{text.slice(0, shown)}
{shown < text.length && (
<span className="inline-block h-[1em] w-[0.55em] -mb-[0.15em] animate-mpk-blink bg-mpk-dim2" />
)}
<span className="invisible">{text.slice(shown)}</span>
</span>
</Tag>
);
}03Update the import paths to match your project
Usage
Put it right above a section heading:
import { Eyebrow } from "@/components/mpkcn/eyebrow";
export function Work() {
return (
<section id="work">
<Eyebrow text="// 02 — selected work" />
<h2>Things I built</h2>
</section>
);
}Each time the label scrolls into view (80% visible) it clears and types itself back in at 32ms per character, with a blinking block cursor. The server renders the full text, so it is there before JavaScript runs.
Examples
Custom text
~/projects $ ls -la
Things I built
The label types itself each time it scrolls into view.
<Eyebrow text="~/projects $ ls -la" />Type once, slower
replay={false} types it only the first time; speed sets the milliseconds per character.
<Eyebrow text="// 01 — about" replay={false} speed={60} />Static label
typing={false} keeps the style and drops the animation.
<Eyebrow text="// changelog" typing={false} />As a heading
The default element is a p. Pass as when the label is part of your outline:
<Eyebrow as="h2" text="// 03 — stack" className="mb-4" />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| text | string | — | The label, e.g. "// 02 — projects". |
| as | React.ElementType | "p" | Element to render. Use "h2" or similar when the label is part of your heading structure. |
| typing | boolean | true | Type the label in when it scrolls into view. false shows it as plain text. |
| speed | number | 32 | Milliseconds per typed character. |
| replay | boolean | true | Clear the label when it leaves the viewport, so it types again next time. |
| ignoreReducedMotion | boolean | false | Type even when the user prefers reduced motion. Only use it behind an explicit action, like a "play anyway" button. |
| className | string | — | Extra classes for the root element. |
Accessibility
- Screen readers get the full label once, from a visually hidden copy. The typed characters and the cursor are
aria-hidden, so nothing is announced letter by letter. - The untyped part of the text is still laid out (invisible), so the line never changes width or wraps differently while typing.
- Under
prefers-reduced-motion: reducethe label is simply shown, with no typing and no cursor. - Server and client render the same full text, so there is no hydration mismatch.