Skip to content

Commit 0c8f711

Browse files
authored
fix: Fix focus management for dismissible tags (#19355)
* fix: dissmissable tag focus issue * fix: removed set timeout
1 parent 9fff69c commit 0c8f711

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/web-components/src/components/tag/dismissible-tag.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ class CDSDismissibleTag extends HostListenerMixin(FocusMixin(CDSTag)) {
3535
@query('button')
3636
protected _buttonNode!: HTMLButtonElement;
3737

38+
/**
39+
* Finds the next focusable dismissible tag sibling
40+
* @returns {HTMLElement|null} The next focusable dismissible tag or null
41+
*/
42+
protected _findNextFocusableTag() {
43+
let nextElement = this.nextElementSibling;
44+
while (nextElement) {
45+
if (
46+
nextElement.tagName.toLowerCase() === `${prefix}-dismissible-tag` &&
47+
!nextElement.hasAttribute('disabled') &&
48+
(nextElement as HTMLElement).getAttribute('open') !== 'false'
49+
) {
50+
return nextElement;
51+
}
52+
nextElement = nextElement.nextElementSibling;
53+
}
54+
return null;
55+
}
56+
3857
/**
3958
* Handles `slotchange` event.
4059
*/
@@ -73,6 +92,7 @@ class CDSDismissibleTag extends HostListenerMixin(FocusMixin(CDSTag)) {
7392
if (this.disabled) {
7493
event.stopPropagation();
7594
} else if (this.open) {
95+
const nextFocusableTag = this._findNextFocusableTag();
7696
const init = {
7797
bubbles: true,
7898
cancelable: true,
@@ -90,6 +110,13 @@ class CDSDismissibleTag extends HostListenerMixin(FocusMixin(CDSTag)) {
90110
)
91111
) {
92112
this.open = false;
113+
if (nextFocusableTag) {
114+
const nextCloseIcon = (nextFocusableTag as CDSDismissibleTag)
115+
._buttonNode;
116+
if (nextCloseIcon) {
117+
nextCloseIcon.focus();
118+
}
119+
}
93120
this.dispatchEvent(
94121
new CustomEvent(
95122
(this.constructor as typeof CDSDismissibleTag).eventClose,

0 commit comments

Comments
 (0)