Skip to content

Commit

Permalink
feat(icon): add aria-hidden true by default
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 551024866
  • Loading branch information
Elliott Marquez authored and copybara-github committed Jul 25, 2023
1 parent fb1c603 commit 08d50e2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
71 changes: 71 additions & 0 deletions icon/icon_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

// import 'jasmine'; (google3-only)
import './icon.js';

import {html} from 'lit';

import {Environment} from '../testing/environment.js';
import {createTokenTests} from '../testing/tokens.js';

import {MdIcon} from './icon.js';

describe('<md-icon>', () => {
const env = new Environment();

describe('.styles', () => {
createTokenTests(MdIcon.styles);
});

describe('accessiblity', () => {
it('sets aria-hidden to true by default', async () => {
const root = env.render(html`
<md-icon>check</md-icon>`);
const icon = root.querySelector('md-icon')!;

await env.waitForStability();

expect(icon.getAttribute('aria-hidden')).toBe('true');
});

it('sets aria-hidden is removed when initalized as false', async () => {
const root = env.render(html`
<md-icon aria-hidden="false">check</md-icon>`);
const icon = root.querySelector('md-icon')!;

await env.waitForStability();

expect(icon.hasAttribute('aria-hidden')).toBe(false);
});

it('allows overriding aria-hidden after first render', async () => {
const root = env.render(html`
<md-icon>check</md-icon>`);
const icon = root.querySelector('md-icon')!;

await env.waitForStability();

expect(icon.getAttribute('aria-hidden')).toBe('true');

icon.removeAttribute('aria-hidden');
await env.waitForStability();

expect(icon.hasAttribute('aria-hidden')).toBe(false);
});

it('overrides invalid aria-hidden values to true', async () => {
const root = env.render(html`
<!-- @ts-ignore:disable-next-line:no-incompatible-type-binding -->
<md-icon aria-hidden="foo">check</md-icon>`);
const icon = root.querySelector('md-icon')!;

await env.waitForStability();

expect(icon.getAttribute('aria-hidden')).toBe('true');
});
});
});
11 changes: 11 additions & 0 deletions icon/internal/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ export class Icon extends LitElement {
protected override render() {
return html`<slot></slot>`;
}

override connectedCallback() {
super.connectedCallback();
const ariaHidden = this.getAttribute('aria-hidden');
if (ariaHidden === 'false') {
this.removeAttribute('aria-hidden');
return;
}

this.setAttribute('aria-hidden', 'true');
}
}

0 comments on commit 08d50e2

Please sign in to comment.