Typewriter
Types text out character by character with a blinking cursor. Install Typewriter from the VLLNT UI registry with the shadcn CLI.
Types text out character by character with a blinking cursor. Part of the content family in VLLNT UI, it ships as a machine-readable registry entry — copy the source directly into your app with the shadcn CLI and own it, no runtime dependency on a component library.
Preview
Switch between light and dark to inspect the embedded Storybook preview.
Installation
Add Typewriter to your project with the shadcn CLI. The source lands in your codebase, ready to adapt:
pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/typewriter.jsonSource
"use client";
import * as React from "react";
import { cn } from "../../lib/utils";
/** Props for {@link Typewriter}. */
export type TypewriterProps = React.ComponentPropsWithoutRef<"span"> & {
/** Show a blinking cursor while typing. Defaults to `true`. */
cursor?: boolean;
/** Milliseconds between characters. Defaults to `60`. */
speed?: number;
/** Text typed out character-by-character. */
text: string;
};
function usePrefersReducedMotion(): boolean {
const [reduced, setReduced] = React.useState(false);
React.useEffect(() => {
if (
typeof window === "undefined" ||
typeof window.matchMedia !== "function"
) {
return;
}
const query = window.matchMedia("(prefers-reduced-motion: reduce)");
const onChange = (): void => {
setReduced(query.matches);
};
onChange();
query.addEventListener("change", onChange);
return () => {
query.removeEventListener("change", onChange);
};
}, []);
return reduced;
}
/**
* Types out text one character at a time with a blinking terminal cursor.
*
* Respects `prefers-reduced-motion`: the full text shows at once.
*
* @example
* ```tsx
* <Typewriter text="Hello, world" />
* ```
*/
export const Typewriter = ({
className,
cursor = true,
ref,
speed = 60,
text,
...props
}: TypewriterProps & { ref?: React.Ref<HTMLSpanElement> }) => {
const reduced = usePrefersReducedMotion();
const [count, setCount] = React.useState(() => (reduced ? text.length : 0));
const [animationKey, setAnimationKey] = React.useState({ reduced, text });
if (animationKey.reduced !== reduced || animationKey.text !== text) {
setAnimationKey({ reduced, text });
setCount(reduced ? text.length : 0);
}
React.useEffect(() => {
if (reduced) {
return;
}
const timer = setInterval(() => {
setCount((current) => {
if (current >= text.length) {
clearInterval(timer);
return current;
}
return current + 1;
});
}, speed);
return () => {
clearInterval(timer);
};
}, [reduced, speed, text]);
const typing = count < text.length;
return (
<span aria-label={text} className={cn(className)} ref={ref} {...props}>
<span aria-hidden="true">{text.slice(0, count)}</span>
{cursor && typing ? (
<span
aria-hidden="true"
className="ml-0.5 inline-block w-[1ch] [animation:vllnt-terminal-cursor-blink_1s_steps(1,end)_infinite] motion-reduce:animate-none"
>
|
</span>
) : undefined}
</span>
);
};
Typewriter.displayName = "Typewriter";
Stories
Explore every variant and state in the interactive Storybook:
Preview
Switch between light and dark to inspect the embedded Storybook preview.