Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: role="timer" with aria-live should announce #179

Merged
merged 1 commit into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/utils.ts
Expand Up @@ -81,8 +81,11 @@ export function isHidden(node: Node): boolean {
}

const role = element.getAttribute('role');
const ariaLive = element.getAttribute('aria-live');
if (role === 'marquee' || role === 'timer') {
return true;
if (ariaLive !== 'polite' && ariaLive !== 'assertive') {
return true;
}
}

return queries.closest(element, HIDDEN_QUERY) != null;
Expand Down
25 changes: 25 additions & 0 deletions test/capture-announcements.test.ts
Expand Up @@ -452,6 +452,31 @@ describe.each(OFF_CASES)('$testName', ({ name, value }) => {
});
});

describe('[role="timer"]', () => {
let cleanup: undefined | ReturnType<typeof CaptureAnnouncements>;
const onCapture = vi.fn();

afterEach(() => {
cleanup?.();
onCapture.mockReset();
});

beforeEach(() => {
cleanup = CaptureAnnouncements({ onCapture });
});

test('should announce when aria-live is set', () => {
const liveRegion = document.createElement('div');
liveRegion.setAttribute('role', 'timer');
liveRegion.setAttribute('aria-live', 'assertive');

appendToRoot(liveRegion);
liveRegion.textContent = 'Hello world';

expect(onCapture).toHaveBeenCalledWith('Hello world', 'assertive');
});
});

describe('common', () => {
let element: HTMLElement;
let cleanup: undefined | ReturnType<typeof CaptureAnnouncements>;
Expand Down
8 changes: 8 additions & 0 deletions test/utils.test.ts
Expand Up @@ -418,6 +418,14 @@ describe('isHidden', () => {
expect(isHidden(element)).toBe(true);
});

test('element with role="timer" and aria-live="assertive" is not hidden', () => {
const element = document.createElement('div');
element.setAttribute('role', 'timer');
element.setAttribute('aria-live', 'assertive');

expect(isHidden(element)).toBe(false);
});

test('element with role="status" is visible', () => {
const element = document.createElement('div');
element.setAttribute('role', 'status');
Expand Down