Text Loop
Text that loops endlessly along a wave, circle, infinity, arch or custom SVG path, optionally on a ribbon.
pnpm dlx shadcn@latest add @leo/text-loopProps
| Name | Type | Default | Description |
|---|---|---|---|
| className | string | undefined | - | |
| color | string | undefined | #ffffff | |
| curviness | number | undefined | 90 | |
| direction | undefined | "forward" | "reverse" | forward | |
| fontSize | number | undefined | 46 | |
| fontWeight | string | number | undefined | 800 | |
| letterSpacing | number | undefined | 2 | |
| path | string | undefined | - | |
| pauseOnHover | boolean | undefined | true | |
| ribbon | boolean | undefined | true | |
| ribbonColor | string | undefined | #5227FF | |
| ribbonWidth | number | undefined | 86 | |
| separator | string | undefined | ✦ | |
| shape | undefined | "wave" | "circle" | "infinity" | "arch" | "line" | wave | |
| speed | number | undefined | 90 | |
| style | CSSProperties | undefined | - | |
| text | string | undefined | React ✦ Bits | |
| uppercase | boolean | undefined | true |
Dependencies
- gsap@^3.13.0
Source
"use client";
import { gsap } from "gsap";
import type { CSSProperties } from "react";
import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import { cn } from "@/lib/utils";
export type TextLoopShape = "wave" | "circle" | "infinity" | "arch" | "line";
export type TextLoopDirection = "forward" | "reverse";
export interface TextLoopProps {
text?: string;
shape?: TextLoopShape;
path?: string;
speed?: number;
direction?: TextLoopDirection;
separator?: string;
curviness?: number;
fontSize?: number;
fontWeight?: number | string;
letterSpacing?: number;
uppercase?: boolean;
color?: string;
ribbon?: boolean;
ribbonColor?: string;
ribbonWidth?: number;
pauseOnHover?: boolean;
className?: string;
style?: CSSProperties;
}
interface Metrics {
length: number;
reps: number;
}
const VIEW_W = 1200;
const VIEW_H = 520;
const CX = VIEW_W / 2;
const CY = VIEW_H / 2;
const EDGE_PAD = 6;
const buildPath = (shape: TextLoopShape, curviness: number, ribbonWidth: number): string => {
const c = Math.max(0, curviness);
const room = Math.max(20, CY - Math.max(0, ribbonWidth) / 2 - EDGE_PAD);
switch (shape) {
case "circle": {
const r = Math.min(90 + c * 0.95, room);
return `M ${CX - r} ${CY} A ${r} ${r} 0 1 1 ${CX + r} ${CY} A ${r} ${r} 0 1 1 ${CX - r} ${CY} Z`;
}
case "infinity": {
const r = 150 + c * 1.4;
const h = Math.min(60 + c * 0.95, room);
return [
`M ${CX} ${CY}`,
`C ${CX + r * 0.55} ${CY - h} ${CX + r} ${CY - h} ${CX + r} ${CY}`,
`C ${CX + r} ${CY + h} ${CX + r * 0.55} ${CY + h} ${CX} ${CY}`,
`C ${CX - r * 0.55} ${CY - h} ${CX - r} ${CY - h} ${CX - r} ${CY}`,
`C ${CX - r} ${CY + h} ${CX - r * 0.55} ${CY + h} ${CX} ${CY}`,
"Z",
].join(" ");
}
case "arch": {
const rise = Math.min(120 + c * 1.1, room * 2);
return `M 120 ${CY + rise / 2} Q ${CX} ${CY - rise * 1.5} ${VIEW_W - 120} ${CY + rise / 2}`;
}
case "line":
return `M -320 ${CY} L ${VIEW_W + 320} ${CY}`;
default: {
const a = Math.min(c * 2.2, room * 2);
return `M -320 ${CY} Q -160 ${CY - a} 0 ${CY} T 320 ${CY} T 640 ${CY} T 960 ${CY} T 1280 ${CY} T ${VIEW_W + 320} ${CY}`;
}
}
};
const TextLoop = ({
text = "React ✦ Bits",
shape = "wave",
path,
speed = 90,
direction = "forward",
separator = "✦",
curviness = 90,
fontSize = 46,
fontWeight = 800,
letterSpacing = 2,
uppercase = true,
color = "#ffffff",
ribbon = true,
ribbonColor = "#5227FF",
ribbonWidth = 86,
pauseOnHover = true,
className,
style,
}: TextLoopProps) => {
const rootRef = useRef<HTMLDivElement | null>(null);
const pathRef = useRef<SVGPathElement | null>(null);
const measureRef = useRef<SVGTextElement | null>(null);
const headRef = useRef<SVGTextPathElement | null>(null);
const tailRef = useRef<SVGTextPathElement | null>(null);
const [metrics, setMetrics] = useState<Metrics>({ length: 0, reps: 1 });
const rawId = useId();
const pathId = `text-loop-${rawId.replace(/:/g, "")}`;
const d = useMemo(
() => path || buildPath(shape, curviness, ribbonWidth),
[path, shape, curviness, ribbonWidth],
);
const unit = useMemo(() => {
const base = uppercase ? String(text).toUpperCase() : String(text);
const gap = separator ? `\u00A0${separator}\u00A0` : "\u00A0\u00A0\u00A0";
return `${base}${gap}`;
}, [text, separator, uppercase]);
const textStyle = useMemo<CSSProperties>(
() => ({ fontSize: `${fontSize}px`, fontWeight, letterSpacing: `${letterSpacing}px` }),
[fontSize, fontWeight, letterSpacing],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: unit and the font settings change the measured text width, so the path is re-measured
useLayoutEffect(() => {
const pathEl = pathRef.current;
const measureEl = measureRef.current;
if (!pathEl || !measureEl) return undefined;
let cancelled = false;
const measure = () => {
if (cancelled) return;
let length = 0;
let unitWidth = 0;
try {
length = pathEl.getTotalLength();
unitWidth = measureEl.getComputedTextLength();
} catch {
return;
}
if (!length) return;
const reps = unitWidth > 0 ? Math.max(1, Math.round(length / unitWidth)) : 1;
setMetrics((prev) =>
prev.length === length && prev.reps === reps ? prev : { length, reps },
);
};
measure();
if (typeof document !== "undefined" && document.fonts?.ready) {
document.fonts.ready.then(measure).catch(() => {});
}
return () => {
cancelled = true;
};
}, [d, unit, fontSize, fontWeight, letterSpacing]);
useEffect(() => {
const { length } = metrics;
const head = headRef.current;
const tail = tailRef.current;
if (!head || !tail || !length) return undefined;
const apply = (offset: number) => {
const partner = offset >= 0 ? offset - length : offset + length;
head.setAttribute("startOffset", String(offset));
tail.setAttribute("startOffset", String(partner));
};
apply(0);
const prefersReduced =
typeof window !== "undefined" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReduced || speed <= 0) return undefined;
const state = { offset: 0 };
const tween = gsap.to(state, {
offset: direction === "reverse" ? -length : length,
duration: length / speed,
ease: "none",
repeat: -1,
onUpdate: () => apply(state.offset),
});
const root = rootRef.current;
const pause = () => tween.pause();
const resume = () => tween.resume();
if (pauseOnHover && root) {
root.addEventListener("pointerenter", pause);
root.addEventListener("pointerleave", resume);
}
return () => {
tween.kill();
if (pauseOnHover && root) {
root.removeEventListener("pointerenter", pause);
root.removeEventListener("pointerleave", resume);
}
};
}, [metrics, speed, direction, pauseOnHover]);
const loopText = unit.repeat(metrics.reps);
const fitLength = metrics.length || undefined;
return (
<div ref={rootRef} className={cn("relative w-full overflow-hidden", className)} style={style}>
<svg
className="block w-full h-auto"
viewBox={`0 0 ${VIEW_W} ${VIEW_H}`}
preserveAspectRatio="xMidYMid meet"
role="img"
aria-label={text}
>
<path
ref={pathRef}
id={pathId}
d={d}
fill="none"
stroke={ribbon ? ribbonColor : "none"}
strokeWidth={ribbon ? ribbonWidth : 0}
strokeLinecap="round"
strokeLinejoin="round"
/>
<text ref={measureRef} className="invisible pointer-events-none" style={textStyle}>
{unit}
</text>
<text
className="select-none"
style={textStyle}
fill={color}
dominantBaseline="central"
textLength={fitLength}
lengthAdjust="spacing"
>
<textPath ref={headRef} href={`#${pathId}`} startOffset={0}>
{loopText}
</textPath>
</text>
<text
className="select-none"
style={textStyle}
fill={color}
dominantBaseline="central"
textLength={fitLength}
lengthAdjust="spacing"
>
<textPath ref={tailRef} href={`#${pathId}`} startOffset={0}>
{loopText}
</textPath>
</text>
</svg>
</div>
);
};
export { TextLoop };
export default TextLoop;