# Text shimmer

> A readable busy label passes a token-color highlight across its letters while work is active and visible.

- Kind: ui · Category: Feedback · Interactivity: script
- Install: `npx shadcn@latest add @haze/text-shimmer`
- File: `src/components/haze/ui/text-shimmer.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/text-shimmer

## Use 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 when
- Use progress when the operation has a meaningful percentage
- Use skeleton for an unknown future content shape rather than a status sentence

## Alternatives
- [spinner](https://hazeui.com/ui/spinner.md)
- [progress](https://hazeui.com/ui/progress.md)
- [skeleton](https://hazeui.com/ui/skeleton.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique caller-supplied prefix for this component and its related elements. |
| text | `string` | "Preparing your workspace" | no | Readable label; later changes use the root data-text attribute. |
| active | `boolean` | true | no | Initial busy state; later changes use data-active true or false. |
| class | `string` |  | no | 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.

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Add a readable busy label with a restrained moving text highlight that stops when data-active becomes false.

## Source

```astro
---
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>
```
