Skip to content

Commit

Permalink
feat(fab): set aria hidden on the icon slot if element has aria-label…
Browse files Browse the repository at this point in the history
… or label

PiperOrigin-RevId: 543531953
  • Loading branch information
Elliott Marquez authored and copybara-github committed Jun 26, 2023
1 parent 8648be6 commit fb4d9c8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
36 changes: 36 additions & 0 deletions fab/fab_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,40 @@ describe('<md-branded-fab>', () => {
expect(button.classList.contains('large')).toBeFalse();
});
});

describe('accessibility', () => {
it('sets aria-hidden on the icon slot when aria-label is set', async () => {
const {button, harness} = await setupTest();
await env.waitForStability();

const iconSlot = button.querySelector('slot[name="icon"]')!;

expect(button.hasAttribute('aria-label')).toBeFalse();
expect(iconSlot.hasAttribute('aria-hidden')).toBeFalse();

const element = harness.element;
element.ariaLabel = 'foo';
await env.waitForStability();

expect(button.hasAttribute('aria-label')).toBeTrue();
expect(iconSlot.getAttribute('aria-hidden')).toEqual('true');
});

it('sets aria-hidden on the icon slot when label is set', async () => {
const {button, harness} = await setupTest();
await env.waitForStability();
const element = harness.element;

const iconSlot = button.querySelector('slot[name="icon"]')!;

expect(!!element.label).toBeFalse();
expect(iconSlot.hasAttribute('aria-hidden')).toBeFalse();

element.label = 'foo';
await env.waitForStability();

expect(!!element.label).toBeTrue();
expect(iconSlot.getAttribute('aria-hidden')).toEqual('true');
});
});
});
8 changes: 7 additions & 1 deletion fab/lib/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ export abstract class SharedFab extends LitElement {
}

private renderIcon() {
const {ariaLabel} = this as ARIAMixinStrict;
return html`<span class="icon">
<slot name="icon" @slotchange=${this.onSlotchange}></slot>
<slot
name="icon"
aria-hidden=${
ariaLabel || this.label ? 'true' : nothing as unknown as 'false'}
@slotchange=${this.onSlotchange}>
</slot>
</span>`;
}

Expand Down

0 comments on commit fb4d9c8

Please sign in to comment.