Fix initial focus issues with Dialog

Fixes #15
This commit is contained in:
Ryan Gossiaux
2021-12-14 13:19:58 -08:00
parent a647f7eb87
commit 02bb968396

View File

@@ -7,7 +7,7 @@
FocusResult, FocusResult,
} from "$lib/utils/focus-management"; } from "$lib/utils/focus-management";
import { contains } from "$lib/internal/dom-containers"; import { contains } from "$lib/internal/dom-containers";
import { afterUpdate, onMount, onDestroy } from "svelte"; import { afterUpdate, onMount, onDestroy, tick } from "svelte";
export let containers: Set<HTMLElement>; export let containers: Set<HTMLElement>;
export let enabled: boolean = true; export let enabled: boolean = true;
@@ -20,7 +20,12 @@
let previousActiveElement: HTMLElement | null = null; let previousActiveElement: HTMLElement | null = null;
function handleFocus() { let initial = true;
async function handleFocus() {
if (initial) {
await tick();
initial = false;
}
if (!enabled) return; if (!enabled) return;
if (containers.size !== 1) return; if (containers.size !== 1) return;
let { initialFocus } = options; let { initialFocus } = options;
@@ -71,7 +76,14 @@
afterUpdate(() => (enabled ? handleFocus() : restore())); afterUpdate(() => (enabled ? handleFocus() : restore()));
onDestroy(restore); // When this component is being destroyed, focusElement is called
// before handleWindowFocus is removed, so in the svelte port we add this
// to ignore the handler.
let destroying = false;
onDestroy(() => {
destroying = true;
restore();
});
// Handle Tab & Shift+Tab keyboard events // Handle Tab & Shift+Tab keyboard events
function handleWindowKeyDown(event: KeyboardEvent) { function handleWindowKeyDown(event: KeyboardEvent) {
@@ -99,6 +111,7 @@
function handleWindowFocus(event: FocusEvent) { function handleWindowFocus(event: FocusEvent) {
if (!enabled) return; if (!enabled) return;
if (containers.size !== 1) return; if (containers.size !== 1) return;
if (destroying) return;
let previous = previousActiveElement; let previous = previousActiveElement;
if (!previous) return; if (!previous) return;