Todae

Todae

useAnnouncer

useAnnouncer() returns announce(message, options?), which reads message to screen readers without showing it. Use it for the result of something async that has no visible message of its own, such as "Draft saved" or "12 results".

On this page
import { Button, Stack, Text, useAnnouncer } from '@tounsoo/todae';
import { useState } from 'react';

export default function AnnounceSave() {
  const announce = useAnnouncer();
  const [count, setCount] = useState(0);
  return (
    <Stack.Root>
      <Button.Root
        onClick={() => {
          setCount((n) => n + 1);
          announce('Draft saved');
        }}
      >
        Save draft
      </Button.Root>
      <Text.Root>Announced “Draft saved” {count} {count === 1 ? 'time' : 'times'}.</Text.Root>
    </Stack.Root>
  );
}

Announced “Draft saved” 0 times.

Messages go to a visually hidden polite or assertive live region that every caller in the document shares. It is added when the first component using the hook mounts. A modal dialog makes the rest of the page inert, so while a modal in Todae's modal stack is open, messages are read from a region inside the one in front. Dialog, Drawer and BottomSheet join the stack by themselves; pass a modal <dialog> of your own to useModalLayer. announce empties the region, then writes the message a moment later, so the same message twice is read twice. A newer message of the same politeness sent before the last one is written replaces it, and a message sent just before the calling component unmounts is still written. politeness: 'assertive' interrupts the screen reader; keep it for messages that cannot wait. On the server announce does nothing, and announce is the same function on every render.

Accessible composition

Todae handles: A visually hidden region with an aria-live="polite" child and an aria-live="assertive" child, both aria-atomic="true", added on mount so it is there before the first message. announce empties the region before it writes, so a repeat is read again. While a modal in Todae's modal stack is open, it writes to a region inside the one in front.

You supply: The whole message, in the reader's language: announce reads your string as it is, so pass "12 results", not "12". A visible result where sighted users need one, since the message never shows. useModalLayer on a modal <dialog> of your own: otherwise messages sent while it is open are written outside it, where the inert page keeps them from being read.

Watch for: Text that is already in a live region, such as an Alert or a toast, is read twice if you announce it too. Don't announce on every keystroke; send one message once the result settles. Don't use it to repeat what the focused control's own name, role or state already says.

When to use

When: Async results with no visible message of their own: saves, copies, result counts, background loads finishing.

When not: A message sighted users should see too → show it on the page, for example in an Alert, which is a live region itself. Validation text → ErrorMessage.

Props

useAnnouncer props
NameTypeDescription
message Requiredstring

Text to read. The same message twice is read twice. An empty string clears the region.

options.politeness"polite" | "assertive"

polite waits for the screen reader to finish; assertive interrupts it.

Default: "polite"