Skip to content
Closed
Show file tree
Hide file tree
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
60 changes: 56 additions & 4 deletions src/lib/data-grid-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface LiveRegion {
element: HTMLElement;
pending: string | null;
timer: ReturnType<typeof setTimeout> | null;
stopWaiting: (() => void) | null;
}

interface Announcer {
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface DataGridMessages {

const ANNOUNCE_DELAY_MS = 150;
const CLEAR_DELAY_MS = 5000;
const MENU_CLOSE_TIMEOUT_MS = 1000;

let announcer: Announcer | null = null;

Expand All @@ -70,14 +72,20 @@ export function announce(

const region = current.regions[assertiveness];
region.pending = message;
if (region.timer !== null) clearTimeout(region.timer);
region.timer = setTimeout(() => flush(current, region), delay);
cancelFlush(region);
region.timer = setTimeout(
() =>
assertiveness === "polite"
? flushOutsideMenu(current, region, delay)
: flush(current, region),
delay,
);
}

export function destroyAnnouncer() {
if (!announcer) return;
for (const region of Object.values(announcer.regions)) {
if (region.timer !== null) clearTimeout(region.timer);
cancelFlush(region);
}
for (const timer of announcer.clearTimers) clearTimeout(timer);
announcer.root.remove();
Expand Down Expand Up @@ -198,7 +206,51 @@ function createRegion(root: HTMLElement, assertiveness: Assertiveness) {
element.setAttribute("aria-relevant", "additions");
element.dataset.liveAnnouncer = assertiveness;
root.appendChild(element);
return { element, pending: null, timer: null };
return { element, pending: null, timer: null, stopWaiting: null };
}

function cancelFlush(region: LiveRegion) {
if (region.timer !== null) clearTimeout(region.timer);
region.timer = null;
region.stopWaiting?.();
}

// VoiceOver drops a polite message that lands just before a closing menu
// returns focus to its trigger, so the message waits for focus to leave it.
function flushOutsideMenu(
current: Announcer,
region: LiveRegion,
delay: number,
) {
region.timer = null;
if (!getIsInMenu(document.activeElement)) {
flush(current, region);
return;
}

function onFocusIn(event: FocusEvent) {
if (getIsInMenu(event.target)) return;
stopWaiting();
region.timer = setTimeout(() => flush(current, region), delay);
}

const timeout = setTimeout(() => {
stopWaiting();
flush(current, region);
}, MENU_CLOSE_TIMEOUT_MS);

function stopWaiting() {
document.removeEventListener("focusin", onFocusIn);
clearTimeout(timeout);
region.stopWaiting = null;
}

document.addEventListener("focusin", onFocusIn);
region.stopWaiting = stopWaiting;
}

function getIsInMenu(target: EventTarget | null) {
return target instanceof Element && target.closest("[role=menu]") !== null;
}

function flush(current: Announcer, region: LiveRegion) {
Expand Down
104 changes: 104 additions & 0 deletions src/lib/test/data-grid-announcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,112 @@ describe("announce", () => {
announce("");
expect(document.querySelector("[data-live-announcer]")).toBeNull();
});

describe("from a menu", () => {
afterEach(() => {
document.body.replaceChildren();
});

it("waits until focus returns to the trigger", () => {
const { trigger, item } = renderMenu();
item.focus();

announce("Sorted by Name, ascending");
vi.advanceTimersByTime(500);
expect(getRegion("polite")?.textContent).toBe("");

trigger.focus();
expect(getRegion("polite")?.textContent).toBe("");
vi.advanceTimersByTime(150);
expect(getRegion("polite")?.textContent).toBe(
"Sorted by Name, ascending",
);
});

it("reads the message if focus never leaves the menu", () => {
const { item } = renderMenu();
item.focus();

announce("Sorted by Name, ascending");
vi.advanceTimersByTime(150 + 999);
expect(getRegion("polite")?.textContent).toBe("");
vi.advanceTimersByTime(1);
expect(getRegion("polite")?.textContent).toBe(
"Sorted by Name, ascending",
);
});

it("reads only the newest message after a wait", () => {
const { trigger, item } = renderMenu();
item.focus();

announce("Sorted by Name, ascending");
vi.advanceTimersByTime(150);
announce("Sorted by Name, descending");
vi.advanceTimersByTime(150);
trigger.focus();
vi.advanceTimersByTime(1000);

const messages = [...(getRegion("polite")?.children ?? [])].map(
(node) => node.textContent,
);
expect(messages).toEqual(["Sorted by Name, descending"]);
});

it("keeps waiting while focus moves within the menu", () => {
const { trigger, item, otherItem } = renderMenu();
item.focus();

announce("Sorted by Name, ascending");
vi.advanceTimersByTime(150);
otherItem.focus();
vi.advanceTimersByTime(500);
expect(getRegion("polite")?.textContent).toBe("");

trigger.focus();
vi.advanceTimersByTime(150);
expect(getRegion("polite")?.textContent).toBe(
"Sorted by Name, ascending",
);
});

it("reads assertive messages without waiting", () => {
const { item } = renderMenu();
item.focus();

announce("Paste failed", { assertiveness: "assertive" });
vi.advanceTimersByTime(150);
expect(getRegion("assertive")?.textContent).toBe("Paste failed");
});

it("stops waiting when destroyed", () => {
const { trigger, item } = renderMenu();
item.focus();

announce("Sorted by Name, ascending");
vi.advanceTimersByTime(150);
destroyAnnouncer();
trigger.focus();
vi.advanceTimersByTime(1000);

expect(document.querySelector("[data-live-announcer]")).toBeNull();
});
});
});

function renderMenu() {
const trigger = document.createElement("button");
const menu = document.createElement("div");
menu.setAttribute("role", "menu");
const item = document.createElement("div");
item.setAttribute("role", "menuitemcheckbox");
item.tabIndex = -1;
const otherItem = item.cloneNode() as HTMLElement;
menu.append(item, otherItem);
document.body.append(trigger, menu);
return { trigger, item, otherItem };
}

describe("sort messages", () => {
it("names the column and direction", () => {
expect(
Expand Down
Loading