Skip to content

Commit cbda80a

Browse files
author
Jonny Muir
committed
fix: add focus management to UUITabElement with focus and blur event listeners to avoid problems with shadow dom and detecting the active focus
1 parent 4c5ac66 commit cbda80a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/uui-tabs/lib/uui-tab.element.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,24 @@ export class UUITabElement extends ActiveMixin(LabelMixin('', LitElement)) {
6464
@property({ type: String, reflect: true })
6565
public orientation?: 'horizontal' | 'vertical' = 'horizontal';
6666

67+
#focus: boolean;
68+
6769
constructor() {
6870
super();
6971
this.addEventListener('click', this.onHostClick);
72+
this.addEventListener('focus', this.#onFocus);
73+
this.addEventListener('blur', this.#onBlur);
74+
this.#focus = false;
7075
}
7176

77+
#onFocus = () => {
78+
this.#focus = true;
79+
};
80+
81+
#onBlur = () => {
82+
this.#focus = false;
83+
};
84+
7285
private onHostClick(e: MouseEvent) {
7386
if (this.disabled) {
7487
e.preventDefault();
@@ -136,7 +149,11 @@ export class UUITabElement extends ActiveMixin(LabelMixin('', LitElement)) {
136149
*/
137150
public hasFocus() {
138151
const button = this.shadowRoot?.querySelector('#button');
139-
return document.activeElement === button || document.activeElement === this;
152+
return (
153+
this.#focus ||
154+
document.activeElement === button ||
155+
document.activeElement === this
156+
);
140157
}
141158

142159
render() {

0 commit comments

Comments
 (0)