Skip to content

LiveRegion

SM · 950B gzip budget

A declarative live region for components that track their own message as state.

Category
Feedback & status
Budget
sm, single-purpose control
External runtime
React / React DOM only
axe fixtureSSR rendersource scanbundle budget

Preview

Live fixture
Preview locale

Try the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.

Install

npx shadcn@latest add https://gear5-ui.vercel.app/r/live-region.json

Copies the source into your project. Pulls in 1 shared registry item: visually-hidden.

New here? Set up Tailwind and import aliases first →

Source & props

Edit on GitHub ↗

Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.

"use client";

import { useEffect, useState } from "react";
import { VisuallyHidden } from "./visually-hidden";

export interface LiveRegionProps {
  message: string;
  politeness?: "polite" | "assertive";
}

/**
 * A declarative live region for a component that already tracks its own
 * message as state, as an alternative to calling the imperative `announce()`
 * function.
 *
 * Renders empty on mount and only starts reflecting `message` one commit
 * later — the same mount-before-fill requirement `announce()` documents:
 * a screen reader must see this node exist before its text first changes, or
 * most implementations never announce it at all.
 */
export function LiveRegion({ message, politeness = "polite" }: LiveRegionProps) {
  const [content, setContent] = useState("");

  useEffect(() => {
    // Deliberately not "just render `message` directly" — the whole point of
    // this component is the one-commit delay between mount and first fill
    // that makes screen readers actually announce the change.
    // eslint-disable-next-line react-hooks/set-state-in-effect
    setContent(message);
  }, [message]);

  return (
    <VisuallyHidden as="div">
      <div role={politeness === "assertive" ? "alert" : "status"} aria-live={politeness} aria-atomic="true">
        {content}
      </div>
    </VisuallyHidden>
  );
}

What CI checks

Quality contract
  • Bundled, minified and gzipped against its tier budget
  • Audited by axe in the state previewed above
  • Rendered through react-dom/server with no browser globals
  • Scanned for network calls, dangerous sinks, and unguarded animation