Skip to content

Commit 08d50e2

Browse files
Elliott Marquezcopybara-github
Elliott Marquez
authored andcommitted
feat(icon): add aria-hidden true by default
PiperOrigin-RevId: 551024866
1 parent fb1c603 commit 08d50e2

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

icon/icon_test.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// import 'jasmine'; (google3-only)
8+
import './icon.js';
9+
10+
import {html} from 'lit';
11+
12+
import {Environment} from '../testing/environment.js';
13+
import {createTokenTests} from '../testing/tokens.js';
14+
15+
import {MdIcon} from './icon.js';
16+
17+
describe('<md-icon>', () => {
18+
const env = new Environment();
19+
20+
describe('.styles', () => {
21+
createTokenTests(MdIcon.styles);
22+
});
23+
24+
describe('accessiblity', () => {
25+
it('sets aria-hidden to true by default', async () => {
26+
const root = env.render(html`
27+
<md-icon>check</md-icon>`);
28+
const icon = root.querySelector('md-icon')!;
29+
30+
await env.waitForStability();
31+
32+
expect(icon.getAttribute('aria-hidden')).toBe('true');
33+
});
34+
35+
it('sets aria-hidden is removed when initalized as false', async () => {
36+
const root = env.render(html`
37+
<md-icon aria-hidden="false">check</md-icon>`);
38+
const icon = root.querySelector('md-icon')!;
39+
40+
await env.waitForStability();
41+
42+
expect(icon.hasAttribute('aria-hidden')).toBe(false);
43+
});
44+
45+
it('allows overriding aria-hidden after first render', async () => {
46+
const root = env.render(html`
47+
<md-icon>check</md-icon>`);
48+
const icon = root.querySelector('md-icon')!;
49+
50+
await env.waitForStability();
51+
52+
expect(icon.getAttribute('aria-hidden')).toBe('true');
53+
54+
icon.removeAttribute('aria-hidden');
55+
await env.waitForStability();
56+
57+
expect(icon.hasAttribute('aria-hidden')).toBe(false);
58+
});
59+
60+
it('overrides invalid aria-hidden values to true', async () => {
61+
const root = env.render(html`
62+
<!-- @ts-ignore:disable-next-line:no-incompatible-type-binding -->
63+
<md-icon aria-hidden="foo">check</md-icon>`);
64+
const icon = root.querySelector('md-icon')!;
65+
66+
await env.waitForStability();
67+
68+
expect(icon.getAttribute('aria-hidden')).toBe('true');
69+
});
70+
});
71+
});

icon/internal/icon.ts

+11
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ export class Icon extends LitElement {
1313
protected override render() {
1414
return html`<slot></slot>`;
1515
}
16+
17+
override connectedCallback() {
18+
super.connectedCallback();
19+
const ariaHidden = this.getAttribute('aria-hidden');
20+
if (ariaHidden === 'false') {
21+
this.removeAttribute('aria-hidden');
22+
return;
23+
}
24+
25+
this.setAttribute('aria-hidden', 'true');
26+
}
1627
}

0 commit comments

Comments
 (0)