Skip to content

Commit 2d5ae6a

Browse files
authored
PopoverPrimitive - Implement aria-controls (#2639)
1 parent 6f5b670 commit 2d5ae6a

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

.changeset/many-apples-film.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
`Breadcrumb` - Implemented `aria-controls` in `Breadcrumb::Truncation` for a11y improvements with toggled content from `PopoverPrimitive`
6+
`Dropdown` - Implemented `aria-controls` in `Dropdown::Toggle::Button` for a11y improvements with toggled content from `PopoverPrimitive`
7+
`PopoverPrimitve` - Implemented `aria-controls` in toggle element for a11y improvements with toggled content
8+
`RichTooltip` - Removed explicitly setting `aria-controls` in `RichTooltip::Toggle` as it is now set through the `PopoverPrimitive`

packages/components/src/components/hds/popover-primitive/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,23 @@ export default class HdsPopoverPrimitive extends Component<HdsPopoverPrimitiveSi
119119
): void => {
120120
this._popoverElement = element;
121121

122-
// for the click events we don't use `onclick` event listeners, but we rely on the `popovertarget` attribute
123-
// provided by the Popover API which does all the magic for us without needing JS code
124-
// (important: to work it needs to be applied to a button)
125-
if (this.enableClickEvents) {
122+
// We need to create a popoverId in order to connect the popover and the toggle with aria-controls
123+
// and an id is needed to implement `onclick` event listeners
124+
if (this._toggleElement) {
126125
let popoverId;
127126
if (this._popoverElement.id) {
128127
popoverId = this._popoverElement.id;
129128
} else {
130-
// we need a DOM id for the `popovertarget` attribute
129+
// we need a DOM id for the `aria-controls` and `popovertarget` attributes
131130
popoverId = guidFor(this);
132131
this._popoverElement.id = popoverId;
133132
}
134-
if (this._toggleElement) {
133+
this._toggleElement.setAttribute('aria-controls', popoverId);
134+
135+
// for the click events we don't use `onclick` event listeners, but we rely on the `popovertarget` attribute
136+
// provided by the Popover API which does all the magic for us without needing JS code
137+
// (important: to work it needs to be applied to a button)
138+
if (this.enableClickEvents) {
135139
this._toggleElement.setAttribute('popovertarget', popoverId);
136140
}
137141
}

packages/components/src/components/hds/rich-tooltip/toggle.hbs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class={{this.classNames}}
88
...attributes
99
type="button"
10-
aria-controls={{@popoverId}}
1110
aria-describedby={{@popoverId}}
1211
aria-expanded={{if @isOpen "true" "false"}}
1312
{{@setupPrimitiveToggle}}

showcase/tests/integration/components/hds/popover-primitive/index-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module(
3030
const popover = this.element.querySelector('main');
3131
const popoverId = popover.id;
3232
assert.dom('button').hasAttribute('popovertarget', popoverId);
33+
assert.dom('button').hasAttribute('aria-controls', popoverId);
3334
assert.dom('main').hasAttribute('popover', 'auto');
3435
});
3536

showcase/tests/integration/components/hds/rich-tooltip/toggle-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ module('Integration | Component | hds/rich-tooltip/toggle', function (hooks) {
111111
hbs`<Hds::RichTooltip::Toggle @popoverId="popoverId" @isOpen={{true}} />`
112112
);
113113
assert.dom('.hds-rich-tooltip__toggle').hasAttribute('type', 'button');
114-
assert.dom('.hds-rich-tooltip__toggle').hasAria('controls', 'popoverId');
115114
assert.dom('.hds-rich-tooltip__toggle').hasAria('describedby', 'popoverId');
116115
assert.dom('.hds-rich-tooltip__toggle').hasAria('expanded', 'true');
117116
});

0 commit comments

Comments
 (0)