Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/@react-aria/live-announcer/src/LiveAnnouncer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@ let node = null;
let clearTimeoutId = null;
const LIVEREGION_TIMEOUT_DELAY = 1000;

type AriaLive = 'assertive' | 'off' | 'polite';

interface MessageProps {
message: string,
'aria-live': AriaLive
}

/**
* Announces the message using screen reader technology.
*/
export function announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {
export function announce(
message: string,
assertiveness: AriaLive = 'assertive',
timeout = LIVEREGION_TIMEOUT_DELAY
) {
ensureInstance(announcer => announcer.announce(message, assertiveness, timeout));
}

/**
* Stops all queued announcements.
*/
export function clearAnnouncer(assertiveness) {
export function clearAnnouncer(assertiveness: AriaLive) {
ensureInstance(announcer => announcer.clear(assertiveness));
}

Expand Down Expand Up @@ -66,7 +77,7 @@ const LiveRegionAnnouncer = React.forwardRef((props, ref) => {
let [assertiveMessage, setAssertiveMessage] = useState('');
let [politeMessage, setPoliteMessage] = useState('');

let clear = (assertiveness) => {
let clear = (assertiveness: AriaLive) => {
if (!assertiveness || assertiveness === 'assertive') {
setAssertiveMessage('');
}
Expand All @@ -76,7 +87,11 @@ const LiveRegionAnnouncer = React.forwardRef((props, ref) => {
}
};

let announce = (message, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) => {
let announce = (
message,
assertiveness: AriaLive = 'assertive',
timeout = LIVEREGION_TIMEOUT_DELAY
) => {
if (clearTimeoutId) {
clearTimeout(clearTimeoutId);
clearTimeoutId = null;
Expand Down Expand Up @@ -108,7 +123,7 @@ const LiveRegionAnnouncer = React.forwardRef((props, ref) => {
);
});

function MessageAlternator({message = '', 'aria-live': ariaLive}) {
function MessageAlternator({message = '', 'aria-live': ariaLive}: MessageProps) {
let messagesRef = useRef(['', '']);
let indexRef = useRef(0);

Expand All @@ -126,7 +141,7 @@ function MessageAlternator({message = '', 'aria-live': ariaLive}) {
);
}

function MessageBlock({message = '', 'aria-live': ariaLive}) {
function MessageBlock({message = '', 'aria-live': ariaLive}: MessageProps) {
return (
<VisuallyHidden
aria-live={ariaLive}
Expand Down