Skip to content

Commit

Permalink
Workaround tabIndex=-1 added to document body by SvelteKit
Browse files Browse the repository at this point in the history
This ends up producing a lot of extra focus events that normally would not exist.

Fixes #36
Fixes #39
  • Loading branch information
rgossiaux committed Jan 22, 2022
1 parent ac1f86a commit bc809ef
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/lib/components/focus-trap/FocusTrap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
// Prevent programmatically escaping
function handleWindowFocus(event: FocusEvent) {
if (event.target === window.document.body) {
// Workaround for a SvelteKit issue: https://github.com/sveltejs/kit/issues/3501
return;
}
if (!enabled) return;
if (containers.size !== 1) return;
if (destroying) return;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/popover/Popover.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@
onMount(() => registerPopover?.(registerBag));
// Handle focus out
function handleFocus() {
function handleFocus(event: FocusEvent) {
if (event.target === window.document.body) {
// Workaround for a SvelteKit issue: https://github.com/sveltejs/kit/issues/3501
return;
}
if (popoverState !== PopoverStates.Open) return;
if (isFocusWithinPopoverGroup()) return;
if (!button) return;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/popover/PopoverButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
let previousActiveElementRef: Element | null =
typeof window === "undefined" ? null : document.activeElement;
function handleFocus() {
function handleFocus(event: FocusEvent) {
if (event.target === window.document.body) {
// Workaround for a SvelteKit issue: https://github.com/sveltejs/kit/issues/3501
return;
}
previousActiveElementRef = activeElementRef;
activeElementRef = document.activeElement;
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/popover/PopoverPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@
}
}
function handleFocus() {
function handleFocus(event: FocusEvent) {
if (event.target === window.document.body) {
// Workaround for a SvelteKit issue: https://github.com/sveltejs/kit/issues/3501
return;
}
if (!focus) return;
if ($api.popoverState !== PopoverStates.Open) return;
if (!$panelStore) return;
Expand Down

0 comments on commit bc809ef

Please sign in to comment.