Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions packages/ui/src/lib/focus/focusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,22 @@ export class FocusManager {
focusElement(element: HTMLElement): void {
if (!element || !element.isConnected) return;

if (element.tabIndex !== -1) {
element.focus();
} else {
// It seems reading `document.activeElement` while handling
// a click often yields `<body>`, so we delay this one tick.
setTimeout(() => {
const activeElement = document.activeElement;
if (activeElement instanceof HTMLElement && !element.contains(activeElement)) {
activeElement.blur();
const hasFocusedChild = element.contains(activeElement);
// Don't refocus if active element is a child of the focusable,
// it helps us maintain focus on input elements.
if (!hasFocusedChild) {
if (element.tabIndex !== -1) {
element.focus();
} else if (activeElement instanceof HTMLElement) {
activeElement.blur();
}
}
}
scrollIntoViewIfNeeded(element);
scrollIntoViewIfNeeded(element);
}, 0);
}

// ============================================
Expand Down
Loading