Skip to content

Commit 7f8e2db

Browse files
authored
fix: prevent toolbar from stealing focus when focus has already been moved (#6934)
* fix: prevent toolbar from stealing focus when focus has already been moved in the document * Change files --------- Co-authored-by: Christopher Holt <=>
1 parent 0c27d02 commit 7f8e2db

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: prevent toolbar from stealing focus when focus has already been moved in the document",
4+
"packageName": "@microsoft/fast-foundation",
5+
"email": "=",
6+
"dependentChangeType": "patch"
7+
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ARIAGlobalStatesAndProperties } from "../patterns/aria-global.js";
99
import { StartEnd, StartEndOptions } from "../patterns/start-end.js";
1010
import { applyMixins } from "../utilities/apply-mixins.js";
1111
import { getDirection } from "../utilities/direction.js";
12+
import { getRootActiveElement } from "../utilities/root-active-element.js";
1213

1314
/**
1415
* Toolbar configuration options
@@ -258,7 +259,14 @@ export class Toolbar extends FoundationElement {
258259
private setFocusedElement(activeIndex: number = this.activeIndex): void {
259260
this.activeIndex = activeIndex;
260261
this.setFocusableElements();
261-
this.focusableElements[this.activeIndex]?.focus();
262+
if (
263+
this.focusableElements[this.activeIndex] &&
264+
// Don't focus the toolbar element if some event handlers moved
265+
// the focus on another element in the page.
266+
this.contains(getRootActiveElement(this))
267+
) {
268+
this.focusableElements[this.activeIndex].focus();
269+
}
262270
}
263271

264272
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// returns the active element in the shadow context of the element in question.
2+
export function getRootActiveElement(element: Element): Element | null {
3+
const rootNode = element.getRootNode();
4+
5+
if (rootNode instanceof ShadowRoot) {
6+
return rootNode.activeElement;
7+
}
8+
9+
return document.activeElement;
10+
}

0 commit comments

Comments
 (0)