Skip to content

Commit

Permalink
fix: fix duplicate node in a11y tree when using aria-label on components
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 577350139
  • Loading branch information
mcreinhard authored and copybara-github committed Oct 28, 2023
1 parent 62b9bf1 commit c04dd5e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/icon_button/icon_button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ export class IconButton extends BaseComponent {
`;
}

protected override updated() {
// If the aria-label attribute is set, hide it from the a11y tree. Otherwise
// the component and its shadow DOM content show up as duplicate nodes with
// the same aria-label.
this.role = this.ariaLabel != null ? 'none' : null;
}

private renderContent() {
const icon = this.icon ||
(!this.hasLabel || this.condensed ? DEFAULT_ICON : undefined);
Expand Down
9 changes: 9 additions & 0 deletions src/icon_button/icon_button_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,13 @@ describe('IconButton', () => {
expect(el.variant).toBe('outlined');
expect(el.getAttribute('variant')).toBe('outlined');
});

it('sets role="none" on the host when aria-label is present', async () => {
const el = await prepareState(html`
<gmpx-icon-button aria-label="enter">
</gmpx-icon-button>
`);

expect(el.role).toBe('none');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ export class PlaceDirectionsButton extends PlaceDataConsumer {
`;
}

protected override updated() {
// If the aria-label attribute is set, hide it from the a11y tree. Otherwise
// the component and its shadow DOM content show up as duplicate nodes with
// the same aria-label.
this.role = this.ariaLabel != null ? 'none' : null;
}

/** @ignore */
getRequiredFields(): Array<keyof Place> {
return ['displayName', 'formattedAddress', 'location'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,16 @@ describe('PlaceDirectionsButton', () => {
expectedQuery}`);
});
});

it('sets role="none" on the host when aria-label is present', async () => {
const root = env.render(html`
<gmpx-place-directions-button aria-label="Get Directions">
</gmpx-place-directions-button>
`);
await env.waitForStability();
const el = root.querySelector<PlaceDirectionsButton>(
'gmpx-place-directions-button')!;

expect(el.role).toBe('none');
});
});

0 comments on commit c04dd5e

Please sign in to comment.