Text shimmer
A readable busy label passes a token-color highlight across its letters while work is active and visible.
pnpm dlx shadcn@latest add @haze/text-shimmer---
import { cn } from "../../../lib/haze/utils";
interface Props {
id: string;
text?: string;
active?: boolean;
class?: string;
}
const { id, text = "Preparing your workspace", active = true, class: className } = Astro.props;
---
<span id={id} data-haze-text-shimmer data-text={text} data-active={active} aria-busy={active ? "true" : "false"} class={cn("inline-block max-w-full text-[0.8125rem]/5 font-medium tracking-[-0.01em]", className)}><span data-shimmer-accessible class="sr-only">{text}</span><span data-shimmer-visual aria-hidden="true" class="whitespace-pre-wrap wrap-anywhere">{Array.from(text).map(character => <span class="text-muted-foreground transition-colors duration-150 ease-smooth data-[lit]:text-foreground" data-lit={active ? undefined : ""}>{character}</span>)}</span></span>
<script>
function initTextShimmers() {
document.querySelectorAll<HTMLElement>("[data-haze-text-shimmer]").forEach(root => {
if (root.hasAttribute("data-enhanced")) return;
const accessible = root.querySelector<HTMLElement>("[data-shimmer-accessible]");
const visual = root.querySelector<HTMLElement>("[data-shimmer-visual]");
if (!accessible || !visual) return;
const motion = matchMedia("(prefers-reduced-motion: reduce)");
let visible = true;
let position = 0;
let timer: ReturnType<typeof setInterval> | undefined;
let text = root.dataset.text ?? "";
const stop = () => { if (timer !== undefined) clearInterval(timer); timer = undefined; };
const light = (all = false) => Array.from(visual.children).forEach((child, index) => child.toggleAttribute("data-lit", all || Math.abs(index - position) < 3));
const sync = () => {
stop();
const next = root.dataset.text ?? "";
if (next !== text) {
text = next;
accessible.textContent = next;
visual.replaceChildren(...Array.from(next).map(character => { const span = document.createElement("span"); span.className = "text-muted-foreground transition-colors duration-150 ease-smooth data-[lit]:text-foreground"; span.textContent = character; return span; }));
}
const active = root.dataset.active === "true";
root.setAttribute("aria-busy", String(active));
if (!active || motion.matches) { light(true); return; }
if (!visible || document.hidden || !root.isConnected) return;
light();
timer = setInterval(() => { if (!root.isConnected || document.hidden) { stop(); return; } position = (position + 1) % Math.max(1, visual.children.length + 3); light(); }, 150);
};
root.addEventListener("haze:visibility", sync);
new MutationObserver(sync).observe(root, { attributes: true, attributeFilter: ["data-active", "data-text"] });
if ("IntersectionObserver" in window) new IntersectionObserver(entries => { visible = entries[0]?.isIntersecting ?? false; sync(); }).observe(root);
motion.addEventListener("change", sync);
root.setAttribute("data-enhanced", "");
sync();
});
}
initTextShimmers();
if (!document.documentElement.hasAttribute("data-haze-text-shimmer-listener")) {
document.documentElement.setAttribute("data-haze-text-shimmer-listener", "");
document.addEventListener("astro:page-load", initTextShimmers);
document.addEventListener("visibilitychange", () => document.querySelectorAll("[data-haze-text-shimmer]").forEach(root => root.dispatchEvent(new Event("haze:visibility"))));
}
</script>When to use
- A short operation benefits from a readable busy label rather than an empty placeholder
- Loading text should stop moving when complete, offscreen or in a hidden tab
When not to use
- Use progress when the operation has a meaningful percentage
- Use skeleton for an unknown future content shape rather than a status sentence
Installation
Add the Haze registry to components.json once, then install Text shimmer with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/text-shimmernpx shadcn@latest add @haze/text-shimmeryarn dlx shadcn@latest add @haze/text-shimmerbunx --bun shadcn@latest add @haze/text-shimmerFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique caller-supplied prefix for this component and its related elements. |
| text | string | "Preparing your workspace" | Readable label; later changes use the root data-text attribute. |
| active | boolean | true | Initial busy state; later changes use data-active true or false. |
| class | string | – | Additional classes for the component root. |
Accessibility
- The real accessible label remains complete and aria-busy mirrors the active work state.
- Motion changes only between semantic text tokens; no gradient or transparent text is used.
- The highlight stops for reduced motion and pauses while offscreen or in a hidden document; the host announces completion if needed.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Text shimmer" with `npx shadcn@latest add @haze/text-shimmer`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import TextShimmer from "@/components/haze/ui/text-shimmer.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/text-shimmer.md
Customize with real content
Adapt the Haze text-shimmer ui for our product. Replace the demo values of these props with our real content: id, text, active, class. Keep the layout, the heading level rules and these accessibility details: The real accessible label remains complete and aria-busy mirrors the active work state. Motion changes only between semantic text tokens; no gradient or transparent text is used. The highlight stops for reduced motion and pauses while offscreen or in a hidden document; the host announces completion if needed. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using text-shimmer, check it fits: use it when a short operation benefits from a readable busy label rather than an empty placeholder; loading text should stop moving when complete, offscreen or in a hidden tab. Avoid it when use progress when the operation has a meaningful percentage; use skeleton for an unknown future content shape rather than a status sentence. Alternatives: @haze/spinner, @haze/progress, @haze/skeleton.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.