Skip to content

Commit d1a3b5c

Browse files
author
Jonny Muir
committed
fix: update focus handling for tabs to prevent initial focus. I.e. make it focusable, don't automatically focus
1 parent 348ba03 commit d1a3b5c

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class UUITabGroupElement extends LitElement {
7575
this.removeEventListener('keydown', this.#onKeyDown);
7676
}
7777

78-
#setFocusable(tab: UUITabElement | null, focus: boolean = true) {
78+
#setFocusable(tab: UUITabElement | null, focus: boolean = false) {
7979
if (tab) {
8080
// Reset tabindex for all tabs
8181
this.#tabElements.forEach(t => {

packages/uui-tabs/lib/uui-tabs.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('UuiTab', () => {
7474
});
7575

7676
it('focuses and activates next tab on ArrowRight', async () => {
77-
element.focus(); // Focus the tab group
77+
tabs[2].setFocusable(true); // Focus the tab group
7878
await element.updateComplete;
7979

8080
const event = new KeyboardEvent('keydown', {
@@ -91,7 +91,7 @@ describe('UuiTab', () => {
9191
});
9292

9393
it('focuses and activates previous tab on ArrowLeft', async () => {
94-
element.focus();
94+
tabs[2].setFocusable(true);
9595
await element.updateComplete;
9696

9797
const event = new KeyboardEvent('keydown', {
@@ -108,7 +108,7 @@ describe('UuiTab', () => {
108108
});
109109

110110
it('focuses and activates first tab on Home', async () => {
111-
element.focus();
111+
tabs[2].setFocusable(true);
112112
await element.updateComplete;
113113

114114
const event = new KeyboardEvent('keydown', {
@@ -125,7 +125,7 @@ describe('UuiTab', () => {
125125
});
126126

127127
it('focuses and activates last tab on End', async () => {
128-
element.focus();
128+
tabs[2].focus();
129129
await element.updateComplete;
130130

131131
const event = new KeyboardEvent('keydown', {
@@ -142,7 +142,7 @@ describe('UuiTab', () => {
142142
});
143143

144144
it('wraps focus from last to first tab with ArrowRight', async () => {
145-
element.focus();
145+
tabs[2].setFocusable(true);
146146
await element.updateComplete;
147147

148148
// Set focus to last tab
@@ -169,7 +169,7 @@ describe('UuiTab', () => {
169169
});
170170

171171
it('activates the focused tab on Space or Enter', async () => {
172-
element.focus();
172+
tabs[2].setFocusable(true);
173173
await element.updateComplete;
174174

175175
const event = new KeyboardEvent('keydown', {
@@ -234,10 +234,10 @@ describe('UuiTab', () => {
234234
expect(window.location.hash).to.equal('#content');
235235
});
236236

237-
it('sets tabindex="0" on the first tab when no tab is active', async () => {
237+
it('does not focus the first tab on initialization, only sets tabindex="0"', async () => {
238238
element = await fixture(html`
239239
<uui-tab-group>
240-
<uui-tab href="/" label="Content">Content</uui-tab>
240+
<uui-tab label="Content">Content</uui-tab>
241241
<uui-tab label="Packages">Packages</uui-tab>
242242
<uui-tab label="Media">Media</uui-tab>
243243
</uui-tab-group>
@@ -248,6 +248,7 @@ describe('UuiTab', () => {
248248
'#button',
249249
) as HTMLButtonElement;
250250

251-
expect(firstTabButton.getAttribute('tabindex')).to.equal('0');
251+
expect(tabs[0].hasFocus()).to.be.false; // Assert that the button is not focused
252+
expect(firstTabButton.getAttribute('tabindex')).to.equal('0'); // Assert that the tabindex is set to 0
252253
});
253254
});

0 commit comments

Comments
 (0)