-
-
Notifications
You must be signed in to change notification settings - Fork 507
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
Prevent scroll on body
when search is open
#715
Conversation
🦋 Changeset detectedLatest commit: 724292a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for astro-starlight ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@@ -72,13 +72,15 @@ const pagefindTranslations = { | |||
|
|||
const openModal = (event?: MouseEvent) => { | |||
dialog.showModal(); | |||
document.body.toggleAttribute('data-search-modal-open', true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the force option here, just to never get in a situation where it's the wrong way around for any weird reason.
This could work, too. Do note L82 that does the same thing.
document.body.toggleAttribute('data-search-modal-open', true); | |
document.body.toggleAttribute('data-search-modal-open'); |
Hey @itsmatteomanf thanks a lot for the PR. I was testing the deploy preview and It seems that closing the modal using the "esc" key doesn't remove the attribute |
I think that it would be worth adding a |
Thanks for the review, @kevinzunigacuellar, @dreyfus92.
I didn't want to change the actual behaviour of the search modal, but I see now that it actually does need to be intercepted as it actually sticks as locked. Gonna do the change immediately. |
Done, added a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested the Escape key workaround in Chrome and Firefox and it does not seem to work for me. Did not really investigate why but maybe it could be because keypress
is now deprecated and no longer recommended?
I guess this could be handled in the existing keydown
event listener just below the keypress
one.
One alternative solution could be the the HTMLDialogElement
close
event:
The close event is fired on an HTMLDialogElement object when the it represents has been closed.
We could handle everything in a code like this I assume:
dialog.addEventListener('close', () => {
document.body.toggleAttribute('data-search-modal-open', false);
});
This would be perfect in this case but maybe the browser support is not good enough yet?
You could actually just replace it, it does the same thing, as far as I know.
Not sure if we can classify it as supported enough, this allows you to support basically any browser. Can be added, too. But not sure it's worth doing that twice. |
I am not sure if its the same case but we already use the dialog element and its browser support is similar to the close event. Hence I think it should be safe to use. |
Indeed, great catch, I imagined In this case, I guess it would make sense to use the |
@HiDeoo @kevinzunigacuellar, moved to using that event. I left the |
Tested it in Chrome and on my mobile device (Galaxy fold). Works beautifully. Thanks a lot @itsmatteomanf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me @itsmatteomanf! Thanks for tackling and thank you to everyone reviewing along the way to polish this. Arrived at a nice solution I think.
I left one suggestion for a tiny refactor here, but overall this looks great 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again @itsmatteomanf! LGTM 🚀
What kind of changes does this PR include?
Description
This PR resolves the scroll on
body
when the search is open.From my tests, it works fine even on mobile, with the menu open.