Skip to content

Commit

Permalink
Prevent scroll on body when search is open (#715)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Zuniga Cuellar <[email protected]>
Co-authored-by: Paul Valladares <[email protected]>
Co-authored-by: HiDeoo <[email protected]>
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
5 people authored Sep 20, 2023
1 parent 8b6bb87 commit e726155
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-steaks-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

feat: prevent scroll on body when search is open
15 changes: 11 additions & 4 deletions packages/starlight/components/Search.astro
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,23 @@ const pagefindTranslations = {

const openModal = (event?: MouseEvent) => {
dialog.showModal();
document.body.toggleAttribute('data-search-modal-open', true);
this.querySelector('input')?.focus();
event?.stopPropagation();
window.addEventListener('click', onClick);
};

const closeModal = () => {
dialog.close();
window.removeEventListener('click', onClick);
};
const closeModal = () => dialog.close();

openBtn.addEventListener('click', openModal);
openBtn.disabled = false;
closeBtn.addEventListener('click', closeModal);

dialog.addEventListener('close', () => {
document.body.toggleAttribute('data-search-modal-open', false);
window.removeEventListener('click', onClick);
});

// Listen for `/` and `cmd + k` keyboard shortcuts.
window.addEventListener('keydown', (e) => {
const isInput =
Expand Down Expand Up @@ -230,6 +233,10 @@ const pagefindTranslations = {
</style>

<style is:global>
[data-search-modal-open] {
overflow: hidden;
}

#starlight__search {
--sl-search-result-spacing: calc(1.25rem * var(--pagefind-ui-scale));
--sl-search-result-pad-inline-start: calc(3.75rem * var(--pagefind-ui-scale));
Expand Down

0 comments on commit e726155

Please sign in to comment.