Skip to content

Commit

Permalink
fix: role="timer" with aria-live should announce (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 18, 2024
1 parent 0761c7d commit e8961ee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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

0 comments on commit e8961ee

Please sign in to comment.