# Text reveal

> Short text reveals its words on request while a complete readable sentence remains present from the start.

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

## Use when
- A user action should replay a brief word reveal without delaying access to the text
- A short statement needs a restrained response to an explicit reveal event

## Avoid when
- Use in-view-title when entering the reading viewport should trigger the response
- Use text-morph when the content itself changes between labels

## Alternatives
- [in-view-title](https://hazeui.com/ui/in-view-title.md)
- [text-morph](https://hazeui.com/ui/text-morph.md)
- [section-heading](https://hazeui.com/ui/section-heading.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique caller-supplied prefix for this component and its related elements. |
| text | `string` | "Good work starts with a clear next step." | no | Complete readable sentence to reveal. |
| headingLevel | `"h2" \| "h3" \| "h4"` |  | no | Optional heading element; omitted renders a paragraph. |
| replay | `boolean` | false | no | Shows an enhanced replay button. |
| replayLabel | `string` | "Reveal again" | no | Visible replay button text. |
| class | `string` |  | no | Additional classes for the component root. |

## Accessibility
- Assistive technology reads one complete sentence; individual animated words are hidden from its reading order.
- The initial text is never hidden or staggered on page load; the host dispatches haze:reveal or enables the replay button.
- The whole response is capped at 300 ms and reduced motion leaves the text still.
- Key Tab, Enter or Space: Reach the optional replay button and reveal the text again.

## Agent prompts

### Install and use

Install the Haze UI component "Text reveal" with `npx shadcn@latest add @haze/text-reveal`. 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 TextReveal from "@/components/haze/ui/text-reveal.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-reveal.md

### Customize with real content

Adapt the Haze text-reveal ui for our product. Replace the demo values of these props with our real content: id, text, headingLevel, replay, replayLabel, class. Keep the layout, the heading level rules and these accessibility details: Assistive technology reads one complete sentence; individual animated words are hidden from its reading order. The initial text is never hidden or staggered on page load; the host dispatches haze:reveal or enables the replay button. The whole response is capped at 300 ms and reduced motion leaves the text still. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using text-reveal, check it fits: use it when a user action should replay a brief word reveal without delaying access to the text; a short statement needs a restrained response to an explicit reveal event. Avoid it when use in-view-title when entering the reading viewport should trigger the response; use text-morph when the content itself changes between labels. Alternatives: @haze/in-view-title, @haze/text-morph, @haze/section-heading.

## Example requests that should lead here
- Add a short statement whose words reveal on a button press, with the full sentence accessible immediately.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  id: string;
  text?: string;
  headingLevel?: "h2" | "h3" | "h4";
  replay?: boolean;
  replayLabel?: string;
  class?: string;
}
const { id, text = "Good work starts with a clear next step.", headingLevel, replay = false, replayLabel = "Reveal again", class: className } = Astro.props;
const Tag = headingLevel ?? "p";
const words = text.trim().split(/\s+/);
---

<div id={id} data-haze-text-reveal class={cn("grid min-w-0 justify-items-start gap-3", className)}>
  <Tag data-reveal-text class={cn("max-w-full wrap-anywhere", headingLevel ? "text-[1.375rem]/7 font-medium tracking-[-0.01em]" : "text-[0.9375rem]/6")}><span class="sr-only">{text}</span><span aria-hidden="true">{words.map((word, index) => <Fragment><span data-reveal-word class="inline-block">{word}</span>{index < words.length - 1 ? " " : ""}</Fragment>)}</span></Tag>
  {replay && <button data-reveal-again type="button" hidden class="min-h-8 cursor-pointer rounded-lg border border-frame bg-field px-3 text-[0.8125rem]/5 font-medium text-foreground transition-[background-color,box-shadow,scale] duration-150 ease-snappy hover:bg-accent hover:shadow-key active:scale-[0.97] active:duration-100 motion-reduce:active:scale-100 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&[hidden]]:hidden">{replayLabel}</button>}
</div>

<script>
  function initTextReveals() {
    document.querySelectorAll<HTMLElement>("[data-haze-text-reveal]").forEach(root => {
      if (root.hasAttribute("data-enhanced")) return;
      let running: Animation[] = [];
      const reveal = () => {
        running.forEach(animation => animation.cancel());
        running = [];
        if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
        const words = Array.from(root.querySelectorAll<HTMLElement>("[data-reveal-word]"));
        words.forEach((word, index) => running.push(word.animate([{ opacity: .4, translate: "0 6px" }, { opacity: 1, translate: "0 0" }], { duration: 200, delay: Math.min(100, index * 12), easing: "cubic-bezier(.2,.8,.2,1)" })));
      };
      const replay = root.querySelector<HTMLButtonElement>("[data-reveal-again]");
      if (replay) { replay.hidden = false; replay.addEventListener("click", reveal); }
      root.addEventListener("haze:reveal", reveal);
      root.setAttribute("data-enhanced", "");
    });
  }
  initTextReveals();
  if (!document.documentElement.hasAttribute("data-haze-text-reveal-listener")) {
    document.documentElement.setAttribute("data-haze-text-reveal-listener", "");
    document.addEventListener("astro:page-load", initTextReveals);
  }
</script>
```
