Text reveal
Short text reveals its words on request while a complete readable sentence remains present from the start.
pnpm dlx shadcn@latest add @haze/text-reveal---
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>When to use
- 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
When not to use
- Use in-view-title when entering the reading viewport should trigger the response
- Use text-morph when the content itself changes between labels
Installation
Add the Haze registry to components.json once, then install Text reveal with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/text-revealnpx shadcn@latest add @haze/text-revealyarn dlx shadcn@latest add @haze/text-revealbunx --bun shadcn@latest add @haze/text-revealFirst 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 | "Good work starts with a clear next step." | Complete readable sentence to reveal. |
| headingLevel | "h2" | "h3" | "h4" | – | Optional heading element; omitted renders a paragraph. |
| replay | boolean | false | Shows an enhanced replay button. |
| replayLabel | string | "Reveal again" | Visible replay button text. |
| class | string | – | Additional classes for the component root. |
Keyboard interactions
- Tab, Enter or Space
- Reach the optional replay button and reveal the text again.
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.
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 reveal" with `npx shadcn@latest add @haze/text-reveal`. 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 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: http://localhost:4329/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.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.