Skip to content

Commit 97720da

Browse files
Fix toolbar focus
1 parent c2221f5 commit 97720da

File tree

1 file changed

+9
-5
lines changed
  • packages/web-components/fast-foundation/src/toolbar

1 file changed

+9
-5
lines changed

packages/web-components/fast-foundation/src/toolbar/toolbar.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Toolbar extends FoundationElement {
7676

7777
set activeIndex(value: number) {
7878
if (this.$fastController.isConnected) {
79-
this._activeIndex = limit(0, this.focusableElements.length - 1, value);
79+
this._activeIndex = limit(0, this.focusableElements?.length > 0 ? this.focusableElements.length - 1 : 0, value);
8080
Observable.notify(this, "activeIndex");
8181
}
8282
}
@@ -94,7 +94,7 @@ export class Toolbar extends FoundationElement {
9494
*
9595
* @internal
9696
*/
97-
private focusableElements: HTMLElement[];
97+
private focusableElements: HTMLElement[] = [];
9898

9999
/**
100100
* The orientation of the toolbar.
@@ -157,6 +157,9 @@ export class Toolbar extends FoundationElement {
157157
public connectedCallback() {
158158
super.connectedCallback();
159159
this.direction = getDirection(this);
160+
if (!this.slottedItems?.length && !this.childItems?.length) {
161+
this.reduceFocusableElements();
162+
}
160163
}
161164

162165
/**
@@ -206,7 +209,7 @@ export class Toolbar extends FoundationElement {
206209
}
207210

208211
const nextIndex = this.activeIndex + incrementer;
209-
if (this.focusableElements[nextIndex]) {
212+
if (this.focusableElements?.length > 0 && this.focusableElements[nextIndex]) {
210213
e.preventDefault();
211214
}
212215

@@ -222,7 +225,7 @@ export class Toolbar extends FoundationElement {
222225
protected get allSlottedItems(): (HTMLElement | Node)[] {
223226
return [
224227
...this.start.assignedElements(),
225-
...this.slottedItems,
228+
...(this.slottedItems ?? []),
226229
...this.end.assignedElements(),
227230
];
228231
}
@@ -260,6 +263,7 @@ export class Toolbar extends FoundationElement {
260263
this.activeIndex = activeIndex;
261264
this.setFocusableElements();
262265
if (
266+
this.focusableElements?.length > 0 &&
263267
this.focusableElements[this.activeIndex] &&
264268
// Don't focus the toolbar element if some event handlers moved
265269
// the focus on another element in the page.
@@ -313,7 +317,7 @@ export class Toolbar extends FoundationElement {
313317
* @internal
314318
*/
315319
private setFocusableElements(): void {
316-
if (this.$fastController.isConnected && this.focusableElements.length > 0) {
320+
if (this.$fastController.isConnected && this.focusableElements?.length > 0) {
317321
this.focusableElements.forEach((element, index) => {
318322
element.tabIndex = this.activeIndex === index ? 0 : -1;
319323
});

0 commit comments

Comments
 (0)