Skip to content

Commit ad9fbca

Browse files
authored
feat: improve focus behavior in components (#7277)
**Related Issue:** None ## Summary - Updates all `setFocus()` methods to use `componentFocusable()` instead of `componentLoaded()`. - `TimePicker` component also updates in the `focusPart` private method to use `componentFocusable()`. - `TextArea` uses `componentLoaded()` outside of the `setFocus()` method. This was in the `selectText()` and `resizeObserver`. I left those them alone since they are not doing any focusing specifically. - Added `await page.waitForChanges();` to tests that required it. - action-menu - split-button - commonTests helper (removed setTimeout of 0 as well)
1 parent b08c58b commit ad9fbca

File tree

60 files changed

+134
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+134
-116
lines changed

packages/calcite-components/src/components/action-bar/action-bar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from "../../utils/conditionalSlot";
2020
import { getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
2121
import {
22-
componentLoaded,
22+
componentFocusable,
2323
LoadableComponent,
2424
setComponentLoaded,
2525
setUpLoadableComponent
@@ -243,7 +243,7 @@ export class ActionBar
243243
*/
244244
@Method()
245245
async setFocus(): Promise<void> {
246-
await componentLoaded(this);
246+
await componentFocusable(this);
247247

248248
this.el?.focus();
249249
}

packages/calcite-components/src/components/action-group/action-group.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "../../utils/conditionalSlot";
88
import { getSlotted } from "../../utils/dom";
99
import {
10-
componentLoaded,
10+
componentFocusable,
1111
LoadableComponent,
1212
setComponentLoaded,
1313
setUpLoadableComponent
@@ -123,7 +123,7 @@ export class ActionGroup
123123
/** Sets focus on the component's first focusable element. */
124124
@Method()
125125
async setFocus(): Promise<void> {
126-
await componentLoaded(this);
126+
await componentFocusable(this);
127127
this.el.focus();
128128
}
129129
// --------------------------------------------------------------------------

packages/calcite-components/src/components/action-menu/action-menu.e2e.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ describe("calcite-action-menu", () => {
300300
expect(await actionMenu.getProperty("open")).toBe(false);
301301

302302
await actionMenu.callMethod("setFocus");
303+
await page.waitForChanges();
303304

304305
await page.keyboard.press("Enter");
305-
306306
await page.waitForChanges();
307307

308308
expect(await actionMenu.getProperty("open")).toBe(true);
@@ -358,9 +358,9 @@ describe("calcite-action-menu", () => {
358358
expect(await actionMenu.getProperty("open")).toBe(false);
359359

360360
await actionMenu.callMethod("setFocus");
361+
await page.waitForChanges();
361362

362363
await page.keyboard.press("ArrowDown");
363-
364364
await page.waitForChanges();
365365

366366
expect(await actionMenu.getProperty("open")).toBe(true);
@@ -392,9 +392,9 @@ describe("calcite-action-menu", () => {
392392
expect(await actionMenu.getProperty("open")).toBe(false);
393393

394394
await actionMenu.callMethod("setFocus");
395+
await page.waitForChanges();
395396

396397
await page.keyboard.press("ArrowDown");
397-
398398
await page.waitForChanges();
399399

400400
const clickSpy = await actions[0].spyOnEvent("click");

packages/calcite-components/src/components/action-menu/action-menu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { EffectivePlacement, LogicalPlacement, OverlayPositioning } from "../../
1717
import { guid } from "../../utils/guid";
1818
import { isActivationKey } from "../../utils/key";
1919
import {
20-
componentLoaded,
20+
componentFocusable,
2121
LoadableComponent,
2222
setComponentLoaded,
2323
setUpLoadableComponent
@@ -185,7 +185,7 @@ export class ActionMenu implements LoadableComponent {
185185
/** Sets focus on the component. */
186186
@Method()
187187
async setFocus(): Promise<void> {
188-
await componentLoaded(this);
188+
await componentFocusable(this);
189189

190190
focusElement(this.menuButtonEl);
191191
}

packages/calcite-components/src/components/action-pad/action-pad.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "../../utils/conditionalSlot";
1919
import { getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
2020
import {
21-
componentLoaded,
21+
componentFocusable,
2222
LoadableComponent,
2323
setComponentLoaded,
2424
setUpLoadableComponent
@@ -186,7 +186,7 @@ export class ActionPad
186186
*/
187187
@Method()
188188
async setFocus(): Promise<void> {
189-
await componentLoaded(this);
189+
await componentFocusable(this);
190190

191191
this.el?.focus();
192192
}

packages/calcite-components/src/components/alert/alert.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "../../utils/dom";
2121
import { MenuPlacement } from "../../utils/floating-ui";
2222
import {
23-
componentLoaded,
23+
componentFocusable,
2424
LoadableComponent,
2525
setComponentLoaded,
2626
setUpLoadableComponent
@@ -380,7 +380,7 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
380380
/** Sets focus on the component's "close" button (the first focusable item). */
381381
@Method()
382382
async setFocus(): Promise<void> {
383-
await componentLoaded(this);
383+
await componentFocusable(this);
384384

385385
const alertLinkEl: HTMLCalciteLinkElement = getSlotted(this.el, { selector: "calcite-link" });
386386

packages/calcite-components/src/components/block-section/block-section.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { BlockSectionMessages } from "./assets/block-section/t9n";
2828
import { BlockSectionToggleDisplay } from "./interfaces";
2929
import { CSS, ICONS } from "./resources";
3030
import {
31-
componentLoaded,
31+
componentFocusable,
3232
LoadableComponent,
3333
setComponentLoaded,
3434
setUpLoadableComponent
@@ -105,7 +105,7 @@ export class BlockSection implements LocalizedComponent, T9nComponent, LoadableC
105105
*/
106106
@Method()
107107
async setFocus(): Promise<void> {
108-
await componentLoaded(this);
108+
await componentFocusable(this);
109109
focusFirstTabbable(this.el);
110110
}
111111

packages/calcite-components/src/components/block/block.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { Status } from "../interfaces";
3737
import { BlockMessages } from "./assets/block/t9n";
3838
import { CSS, ICONS, SLOTS } from "./resources";
3939
import {
40-
componentLoaded,
40+
componentFocusable,
4141
LoadableComponent,
4242
setComponentLoaded,
4343
setUpLoadableComponent
@@ -145,7 +145,7 @@ export class Block
145145
*/
146146
@Method()
147147
async setFocus(): Promise<void> {
148-
await componentLoaded(this);
148+
await componentFocusable(this);
149149
focusFirstTabbable(this.el);
150150
}
151151

packages/calcite-components/src/components/button/button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "../../utils/interactive";
99
import { connectLabel, disconnectLabel, getLabelText, LabelableComponent } from "../../utils/label";
1010
import {
11-
componentLoaded,
11+
componentFocusable,
1212
LoadableComponent,
1313
setComponentLoaded,
1414
setUpLoadableComponent
@@ -293,7 +293,7 @@ export class Button
293293
/** Sets focus on the component. */
294294
@Method()
295295
async setFocus(): Promise<void> {
296-
await componentLoaded(this);
296+
await componentFocusable(this);
297297

298298
this.childEl?.focus();
299299
}

packages/calcite-components/src/components/checkbox/checkbox.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import { isActivationKey } from "../../utils/key";
2727
import { connectLabel, disconnectLabel, getLabelText, LabelableComponent } from "../../utils/label";
2828
import {
29-
componentLoaded,
29+
componentFocusable,
3030
LoadableComponent,
3131
setComponentLoaded,
3232
setUpLoadableComponent
@@ -140,7 +140,7 @@ export class Checkbox
140140
/** Sets focus on the component. */
141141
@Method()
142142
async setFocus(): Promise<void> {
143-
await componentLoaded(this);
143+
await componentFocusable(this);
144144

145145
this.toggleEl?.focus();
146146
}

packages/calcite-components/src/components/chip-group/chip-group.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
} from "../../utils/interactive";
2020
import { createObserver } from "../../utils/observers";
2121
import { Scale, SelectionMode } from "../interfaces";
22-
import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable";
22+
import {
23+
componentFocusable,
24+
setComponentLoaded,
25+
setUpLoadableComponent
26+
} from "../../utils/loadable";
2327
/**
2428
* @slot - A slot for adding one or more `calcite-chip`s.
2529
*/
@@ -178,7 +182,7 @@ export class ChipGroup implements InteractiveComponent {
178182
*/
179183
@Method()
180184
async setFocus(): Promise<void> {
181-
await componentLoaded(this);
185+
await componentFocusable(this);
182186
if (!this.disabled) {
183187
(this.selectedItems[0] || this.items[0])?.setFocus();
184188
}

packages/calcite-components/src/components/chip/chip.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
disconnectConditionalSlotComponent
2323
} from "../../utils/conditionalSlot";
2424
import {
25-
componentLoaded,
25+
componentFocusable,
2626
LoadableComponent,
2727
setComponentLoaded,
2828
setUpLoadableComponent
@@ -271,7 +271,7 @@ export class Chip
271271
/** Sets focus on the component. */
272272
@Method()
273273
async setFocus(): Promise<void> {
274-
await componentLoaded(this);
274+
await componentFocusable(this);
275275
if (!this.disabled && this.interactive) {
276276
this.containerEl?.focus();
277277
} else if (!this.disabled && this.closable) {

packages/calcite-components/src/components/color-picker-hex-input/color-picker-hex-input.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Channels, RGB } from "../color-picker/interfaces";
2626
import Color from "color";
2727
import { focusElement } from "../../utils/dom";
2828
import {
29-
componentLoaded,
29+
componentFocusable,
3030
LoadableComponent,
3131
setComponentLoaded,
3232
setUpLoadableComponent
@@ -365,7 +365,7 @@ export class ColorPickerHexInput implements LoadableComponent {
365365
/** Sets focus on the component. */
366366
@Method()
367367
async setFocus(): Promise<void> {
368-
await componentLoaded(this);
368+
await componentFocusable(this);
369369

370370
focusElement(this.hexInputNode);
371371
}

packages/calcite-components/src/components/color-picker/color-picker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
} from "../../utils/interactive";
5252
import { isActivationKey } from "../../utils/key";
5353
import {
54-
componentLoaded,
54+
componentFocusable,
5555
LoadableComponent,
5656
setComponentLoaded,
5757
setUpLoadableComponent
@@ -652,7 +652,7 @@ export class ColorPicker
652652
/** Sets focus on the component's first focusable element. */
653653
@Method()
654654
async setFocus(): Promise<void> {
655-
await componentLoaded(this);
655+
await componentFocusable(this);
656656
this.el.focus();
657657
}
658658

packages/calcite-components/src/components/combobox/combobox.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
} from "../../utils/interactive";
4646
import { connectLabel, disconnectLabel, getLabelText, LabelableComponent } from "../../utils/label";
4747
import {
48-
componentLoaded,
48+
componentFocusable,
4949
LoadableComponent,
5050
setComponentLoaded,
5151
setUpLoadableComponent
@@ -347,7 +347,7 @@ export class Combobox
347347
/** Sets focus on the component. */
348348
@Method()
349349
async setFocus(): Promise<void> {
350-
await componentLoaded(this);
350+
await componentFocusable(this);
351351

352352
this.textInput?.focus();
353353
this.activeChipIndex = -1;

packages/calcite-components/src/components/date-picker/date-picker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
setEndOfDay
2222
} from "../../utils/date";
2323
import {
24-
componentLoaded,
24+
componentFocusable,
2525
LoadableComponent,
2626
setComponentLoaded,
2727
setUpLoadableComponent
@@ -193,7 +193,7 @@ export class DatePicker implements LocalizedComponent, LoadableComponent, T9nCom
193193
/** Sets focus on the component's first focusable element. */
194194
@Method()
195195
async setFocus(): Promise<void> {
196-
await componentLoaded(this);
196+
await componentFocusable(this);
197197
this.el.focus();
198198
}
199199

packages/calcite-components/src/components/dropdown-item/dropdown-item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { RequestedItem } from "../dropdown-group/interfaces";
1616
import { FlipContext, Scale, SelectionMode } from "../interfaces";
1717
import { CSS } from "./resources";
1818
import {
19-
componentLoaded,
19+
componentFocusable,
2020
LoadableComponent,
2121
setComponentLoaded,
2222
setUpLoadableComponent
@@ -102,7 +102,7 @@ export class DropdownItem implements LoadableComponent {
102102
/** Sets focus on the component. */
103103
@Method()
104104
async setFocus(): Promise<void> {
105-
await componentLoaded(this);
105+
await componentFocusable(this);
106106

107107
this.el?.focus();
108108
}

packages/calcite-components/src/components/dropdown/dropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
} from "../../utils/interactive";
4141
import { isActivationKey } from "../../utils/key";
4242
import {
43-
componentLoaded,
43+
componentFocusable,
4444
LoadableComponent,
4545
setComponentLoaded,
4646
setUpLoadableComponent
@@ -200,7 +200,7 @@ export class Dropdown
200200
/** Sets focus on the component's first focusable element. */
201201
@Method()
202202
async setFocus(): Promise<void> {
203-
await componentLoaded(this);
203+
await componentFocusable(this);
204204
this.el.focus();
205205
}
206206

packages/calcite-components/src/components/fab/fab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
updateHostInteraction
88
} from "../../utils/interactive";
99
import {
10-
componentLoaded,
10+
componentFocusable,
1111
LoadableComponent,
1212
setComponentLoaded,
1313
setUpLoadableComponent
@@ -123,7 +123,7 @@ export class Fab implements InteractiveComponent, LoadableComponent {
123123
/** Sets focus on the component. */
124124
@Method()
125125
async setFocus(): Promise<void> {
126-
await componentLoaded(this);
126+
await componentFocusable(this);
127127

128128
focusElement(this.buttonEl);
129129
}

packages/calcite-components/src/components/filter/filter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
updateHostInteraction
2121
} from "../../utils/interactive";
2222
import {
23-
componentLoaded,
23+
componentFocusable,
2424
LoadableComponent,
2525
setComponentLoaded,
2626
setUpLoadableComponent
@@ -208,7 +208,7 @@ export class Filter
208208
/** Sets focus on the component. */
209209
@Method()
210210
async setFocus(): Promise<void> {
211-
await componentLoaded(this);
211+
await componentFocusable(this);
212212

213213
this.el?.focus();
214214
}

packages/calcite-components/src/components/flow-item/flow-item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
updateHostInteraction
2020
} from "../../utils/interactive";
2121
import {
22-
componentLoaded,
22+
componentFocusable,
2323
LoadableComponent,
2424
setComponentLoaded,
2525
setUpLoadableComponent
@@ -213,7 +213,7 @@ export class FlowItem
213213
*/
214214
@Method()
215215
async setFocus(): Promise<void> {
216-
await componentLoaded(this);
216+
await componentFocusable(this);
217217

218218
const { backButtonEl, containerEl } = this;
219219

0 commit comments

Comments
 (0)