Skip to content

fix: layout shift behind command+k search #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
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 = '';
}
});