@@ -119,19 +119,23 @@ export default class HdsPopoverPrimitive extends Component<HdsPopoverPrimitiveSi
119
119
) : void => {
120
120
this . _popoverElement = element ;
121
121
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 ) {
126
125
let popoverId ;
127
126
if ( this . _popoverElement . id ) {
128
127
popoverId = this . _popoverElement . id ;
129
128
} else {
130
- // we need a DOM id for the `popovertarget` attribute
129
+ // we need a DOM id for the `aria-controls` and ` popovertarget` attributes
131
130
popoverId = guidFor ( this ) ;
132
131
this . _popoverElement . id = popoverId ;
133
132
}
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 ) {
135
139
this . _toggleElement . setAttribute ( 'popovertarget' , popoverId ) ;
136
140
}
137
141
}
0 commit comments