Typewriter Text
Typewriter Text is a React and Tailwind CSS text component from SmoothUI, licensed MIT. Copy it and paste it into your project.
SmoothUI
MIT
text
What it is
Typewriter Text is a React and Tailwind CSS text component from SmoothUI, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
No packages beyond the project baseline
Licence
This component comes from SmoothUI and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Typewriter Text
import type React from "react";
import { useEffect, useRef, useState } from "react";
function useReducedMotion() {
const [shouldReduceMotion, setShouldReduceMotion] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
setShouldReduceMotion(mediaQuery.matches);
const handleChange = (e: MediaQueryListEvent) => {
setShouldReduceMotion(e.matches);
};
mediaQuery.addEventListener("change", handleChange);
return () => mediaQuery.removeEventListener("change", handleChange);
}, []);
return shouldReduceMotion;
}
export interface TypewriterTextProps {
children: string;
className?: string;
loop?: boolean;
speed?: number;
}
const LOOP_RESTART_DELAY_MS = 1000;
const TypewriterText: React.FC<TypewriterTextProps> = ({
children,
speed = 50,
loop = false,
className = "",
}) => {
const [displayed, setDisplayed] = useState("");
const index = useRef(0);
const timeout = useRef<NodeJS.Timeout | null>(null);
const shouldReduceMotion = useReducedMotion();
useEffect(() => {
if (shouldReduceMotion) {
// Show full text immediately when reduced motion is enabled
setDisplayed(children);
return;
}
setDisplayed("");
index.current = 0;
function type() {
setDisplayed(children.slice(0, index.current + 1));
if (index.current < children.length - 1) {
index.current++;
timeout.current = setTimeout(type, speed);
} else if (loop) {
timeout.current = setTimeout(() => {
setDisplayed("");
index.current = 0;
type();
}, LOOP_RESTART_DELAY_MS);
}
}
type();
return () => {
if (timeout.current) {
clearTimeout(timeout.current);
}
};
}, [children, speed, loop, shouldReduceMotion]);
return <span className={className}>{displayed}</span>;
};
export default TypewriterText;