Skip to content

Commit

Permalink
fix: prevent toolbar from stealing focus when focus has already been …
Browse files Browse the repository at this point in the history
…moved (#6934)

* fix: prevent toolbar from stealing focus when focus has already been moved in the document

* Change files

---------

Co-authored-by: Christopher Holt <=>
  • Loading branch information
chrisdholt committed Apr 5, 2024
1 parent 0c27d02 commit 7f8e2db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: prevent toolbar from stealing focus when focus has already been moved in the document",
"packageName": "@microsoft/fast-foundation",
"email": "=",
"dependentChangeType": "patch"
}
10 changes: 9 additions & 1 deletion packages/web-components/fast-foundation/src/toolbar/toolbar.ts
Expand Up @@ -9,6 +9,7 @@ import { ARIAGlobalStatesAndProperties } from "../patterns/aria-global.js";
import { StartEnd, StartEndOptions } from "../patterns/start-end.js";
import { applyMixins } from "../utilities/apply-mixins.js";
import { getDirection } from "../utilities/direction.js";
import { getRootActiveElement } from "../utilities/root-active-element.js";

/**
* Toolbar configuration options
Expand Down Expand Up @@ -258,7 +259,14 @@ export class Toolbar extends FoundationElement {
private setFocusedElement(activeIndex: number = this.activeIndex): void {
this.activeIndex = activeIndex;
this.setFocusableElements();
this.focusableElements[this.activeIndex]?.focus();
if (
this.focusableElements[this.activeIndex] &&
// Don't focus the toolbar element if some event handlers moved
// the focus on another element in the page.
this.contains(getRootActiveElement(this))
) {
this.focusableElements[this.activeIndex].focus();
}
}

/**
Expand Down
@@ -0,0 +1,10 @@
// returns the active element in the shadow context of the element in question.
export function getRootActiveElement(element: Element): Element | null {
const rootNode = element.getRootNode();

if (rootNode instanceof ShadowRoot) {
return rootNode.activeElement;
}

return document.activeElement;
}

0 comments on commit 7f8e2db

Please sign in to comment.