Skip to content

Commit

Permalink
fix: prevent layout shift behind command+k search (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaevupoiss authored Oct 23, 2024
1 parent 5d5429a commit 8788302
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/site-kit/src/lib/search/SearchBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ It appears when the user clicks on the `Search` component or presses the corresp
let search: any = $state(null);
let recent_searches: any[] = $state([]);
let last_scroll_position: number | null = null;
let worker: Worker;
let ready = $state(false);
Expand Down Expand Up @@ -68,9 +70,9 @@ It appears when the user clicks on the `Search` component or presses the corresp
async function close() {
if ($searching) {
$searching = false;
const scroll = -parseInt(document.body.style.top || '0');
const scroll = last_scroll_position || 0;
last_scroll_position = null;
document.body.style.position = '';
document.body.style.top = '';
document.body.tabIndex = -1;
document.body.focus();
document.body.removeAttribute('tabindex');
Expand Down Expand Up @@ -108,10 +110,7 @@ It appears when the user clicks on the `Search` component or presses the corresp
$effect(() => {
if ($searching) {
document.body.style.top = `-${window.scrollY}px`;
document.body.style.position = 'fixed';
$overlay_open = true;
last_scroll_position = window.scrollY;
}
});
</script>
Expand Down
2 changes: 2 additions & 0 deletions packages/site-kit/src/lib/stores/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ overlay_open.subscribe((value) => {
if (value) {
// Disable root from scrolling
document.documentElement.style.overflow = 'hidden';
document.documentElement.style.scrollbarGutter = 'stable';
} else {
// Enable root to scroll
document.documentElement.style.overflow = '';
document.documentElement.style.scrollbarGutter = '';
}
});

0 comments on commit 8788302

Please sign in to comment.