Fix issue detail main-pane focus on navigation

This commit is contained in:
dotta 2026-04-08 09:03:24 -05:00
parent e21e442033
commit 59d913d04b
3 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,21 @@
export function shouldFocusMainContentAfterNavigation(
mainElement: HTMLElement | null,
activeElement: Element | null,
): boolean {
if (!(mainElement instanceof HTMLElement)) return false;
if (!(activeElement instanceof HTMLElement)) return true;
if (!document.contains(activeElement)) return true;
if (activeElement === document.body || activeElement === document.documentElement) return true;
return !mainElement.contains(activeElement);
}
export function scheduleMainContentFocus(mainElement: HTMLElement | null): () => void {
if (!(mainElement instanceof HTMLElement)) return () => {};
const frame = window.requestAnimationFrame(() => {
if (!shouldFocusMainContentAfterNavigation(mainElement, document.activeElement)) return;
mainElement.focus({ preventScroll: true });
});
return () => window.cancelAnimationFrame(frame);
}