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
24 changes: 23 additions & 1 deletion src/services/aria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ class Aria {
this.controller = controller;
}

private firstMessageSent = false;
private firstMessageTimeout: number | null = null;

attach() {
const container = this.controller.container;
if (this.span.parentNode !== container) {
// Append the element empty first to ensure screen readers detect the live region
// before any content is added. This fixes the issue where the first alert isn't announced.
domFrag(container).prepend(domFrag(this.span));
}
}
Expand Down Expand Up @@ -89,7 +94,24 @@ class Aria {
if (this.controller.options.logAriaAlerts && this.msg) {
console.log(this.msg);
}
this.span.textContent = this.msg;

// For the first message, use a 50ms delay to ensure the empty live region
// is registered with screen readers before adding content. Screen readers need
// actual time (not just a different execution context) to process the empty element.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if we get multiple messages coming in within the first 50ms? it seems like they might then come in out of order

// We debounce to ensure that if multiple messages arrive within the first 50ms,
// only the final message is announced (avoiding out-of-order announcements).
if (!this.firstMessageSent) {
if (this.firstMessageTimeout !== null) {
clearTimeout(this.firstMessageTimeout);
}
this.firstMessageTimeout = setTimeout(() => {
this.firstMessageSent = true;
this.firstMessageTimeout = null;
this.span.textContent = this.msg;
}, 50);
} else {
this.span.textContent = this.msg;
}
}
}
return this.clear();
Expand Down
Loading