Rotating text ring
effect · button · delight · T0 · v1.0.0 · readyAbout
A circular badge: a line of text follows its edge and turns steadily around whatever sits in the centre (an icon, a short label). The text is real SVG text fitted exactly to the circle, and screen readers read it once.
Layers
- Native
- No gated feature: supported everywhere.
- Static
- The ring stands still.
Skin
| Variable | Default | Meaning |
|---|---|---|
--rotating-text-ring-size | 8rem | Diameter of the badge. |
--rotating-text-ring-duration | 12s | Time for one full turn. |
--rotating-text-ring-surface | #171717 | Badge background. |
--rotating-text-ring-text | #fff | Colour of the ring text. |
--rotating-text-ring-font-size | 9px | Size of the ring text in hundredths of the diameter (px in the 100-unit viewBox). |
Accessibility
Static layer: the ring stands still.
The ring is hidden from assistive technology; its text is read once from a visually hidden copy.
Sources
- reel · thedesignshop.tds · "Magnetic Buttons" reel; technique only (P6).
Code
Self-contained: copy every file, keeping the folder structure.
mechanism.module.css/* web-effects rotating-text-ring 1.0.0 · MIT-0 */
/* Mechanism of the rotating text ring (D2.8, D3.11), shared by both styling variants.
.root stacks the ring (an SVG, text on a circular path) and the centre content in one grid
cell. L1: the SVG turns with a CSS animation, which every supported browser runs, so the
native layer has no feature gate and there is no L2. L3 (base rules): the ring stands still. */
.root {
--_rotating-text-ring-size: var(--rotating-text-ring-size, 8rem);
--_rotating-text-ring-duration: var(--rotating-text-ring-duration, 12s);
--_rotating-text-ring-surface: var(--rotating-text-ring-surface, #171717);
--_rotating-text-ring-text: var(--rotating-text-ring-text, #fff);
--_rotating-text-ring-font-size: var(--rotating-text-ring-font-size, 9px);
display: inline-grid;
place-items: center;
width: var(--_rotating-text-ring-size);
height: var(--_rotating-text-ring-size);
}
.ring,
.center {
grid-area: 1 / 1;
}
.ring {
width: 100%;
height: 100%;
overflow: visible;
}
/* User units of the 100-unit viewBox: a font size of 9px is 9 % of the diameter. */
.text {
font-size: var(--_rotating-text-ring-font-size);
fill: var(--_rotating-text-ring-text);
}
/* Visually hidden, still read (the ring itself is aria-hidden). */
.label {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
@keyframes turn {
to {
transform: rotate(1turn);
}
}
/* L1 native (gated on the override attribute, D3.6a). */
:root:not([data-fx-layer]) .ring,
:root[data-fx-layer="native"] .ring {
animation: turn var(--_rotating-text-ring-duration) linear infinite;
}
.root[data-reverse] .ring {
animation-direction: reverse;
}
/* L3 under reduced motion (unless forced). */
@media (prefers-reduced-motion: reduce) {
:root:not([data-fx-layer]) .ring {
animation: none;
}
}
ring.tsx// web-effects rotating-text-ring 1.0.0 · MIT-0
import { useId } from "react";
import mechanism from "./mechanism.module.css";
// The ring itself, shared by both styling variants (P8): text on a circular path in a 100-unit
// viewBox, fitted to the full circumference with textLength, so it closes on itself.
/** Radius of the text baseline; glyphs extend outwards from it, towards the badge edge. */
const radius = 36;
const circumference = 2 * Math.PI * radius;
/** A circle as two arcs, starting at 9 o'clock and running clockwise (text reads upright on top). */
const circle = `M 50 50 m -${radius} 0 a ${radius} ${radius} 0 1 1 ${2 * radius} 0 a ${radius} ${radius} 0 1 1 -${2 * radius} 0`;
export function Ring({ text, repeat }: { text: string; repeat: number }) {
// useId's delimiters are not safe in a URL fragment; the remaining characters stay unique.
const id = `rotating-text-ring-${useId().replace(/[^\w-]/g, "")}`;
return (
<svg
className={mechanism.ring}
viewBox="0 0 100 100"
aria-hidden="true"
focusable="false"
>
<path id={id} d={circle} fill="none" />
<text className={mechanism.text}>
<textPath
href={`#${id}`}
textLength={circumference}
lengthAdjust="spacing"
>
{text.repeat(repeat)}
</textPath>
</text>
</svg>
);
}
tailwind/RotatingTextRing.tsx// web-effects rotating-text-ring 1.0.0 · MIT-0
import type { ReactNode } from "react";
import mechanism from "../mechanism.module.css";
import { Ring } from "../ring";
/**
* A round badge with `text` around its edge, turning, and `children` in the centre. `text` is
* repeated `repeat` times around the circle; end it with a separator, e.g. "View all • ".
*/
export function RotatingTextRing({
text,
repeat = 2,
reverse = false,
children,
className = "",
}: {
text: string;
repeat?: number;
reverse?: boolean;
children?: ReactNode;
className?: string;
}) {
return (
<span
data-reverse={reverse || undefined}
className={`${mechanism.root} rounded-full bg-(--_rotating-text-ring-surface) font-semibold uppercase ${className}`}
>
<Ring text={text} repeat={repeat} />
<span className={mechanism.center}>{children}</span>
<span className={mechanism.label}>{text}</span>
</span>
);
}