Skip to content
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

Merged
merged 7 commits into from
Sep 20, 2023

Conversation

itsmatteomanf
Copy link
Contributor

What kind of changes does this PR include?

  • Changes to Starlight code

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.

@changeset-bot
Copy link

changeset-bot bot commented Sep 14, 2023

🦋 Changeset detected

Latest commit: 724292a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

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

@netlify
Copy link

netlify bot commented Sep 14, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 724292a
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/650a3d56dbd433000758aed5
😎 Deploy Preview https://deploy-preview-715--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Sep 14, 2023
@@ -72,13 +72,15 @@ const pagefindTranslations = {

const openModal = (event?: MouseEvent) => {
dialog.showModal();
document.body.toggleAttribute('data-search-modal-open', true);
Copy link
Contributor Author

@itsmatteomanf itsmatteomanf Sep 14, 2023

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.

Suggested change
document.body.toggleAttribute('data-search-modal-open', true);
document.body.toggleAttribute('data-search-modal-open');

@kevinzunigacuellar
Copy link
Sponsor Member

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 data-search-modal-open.

@dreyfus92
Copy link
Member

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 data-search-modal-open.

I think that it would be worth adding a KeyboardEvent as well to the arguments of both functions. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

@itsmatteomanf
Copy link
Contributor Author

Thanks for the review, @kevinzunigacuellar, @dreyfus92.

I think that it would be worth adding a KeyboardEvent as well to the arguments of both functions.

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.

@itsmatteomanf
Copy link
Contributor Author

Done, added a preventDefault() as it does exit out of fullscreen when pressing esc to close the modal.
Now it closes the modal, and then exits fullscreen on the next press.

Copy link
Member

@dreyfus92 dreyfus92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Copy link
Member

@HiDeoo HiDeoo left a 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?

@itsmatteomanf
Copy link
Contributor Author

I guess this could be handled in the existing keydown event listener just below the keypress one.

You could actually just replace it, it does the same thing, as far as I know.
Replacing it, seems to work fine.

One alternative solution could be the the HTMLDialogElement close event:

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.

@kevinzunigacuellar
Copy link
Sponsor Member

This would be perfect in this case but maybe the browser support is not good enough yet?

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.

@HiDeoo
Copy link
Member

HiDeoo commented Sep 19, 2023

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 <dialog/> being usable before Safari 15.4 but I guess I dreamed about that ^^

In this case, I guess it would make sense to use the HTMLDialogElement close event for all document.body.toggleAttribute('data-search-modal-open', false);?

@itsmatteomanf
Copy link
Contributor Author

@HiDeoo @kevinzunigacuellar, moved to using that event.

I left the closeModal() function as it's used by the mobile button (maybe a desktop button would be good, too), but moved the removal of the attribute in the close event, for all cases.

@kevinzunigacuellar
Copy link
Sponsor Member

Tested it in Chrome and on my mobile device (Galaxy fold). Works beautifully. Thanks a lot @itsmatteomanf

Copy link
Member

@delucis delucis left a 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 🙌

packages/starlight/components/Search.astro Show resolved Hide resolved
@delucis delucis added the 🌟 patch Change that triggers a patch release label Sep 19, 2023
Copy link
Member

@delucis delucis left a 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 🚀

@delucis delucis merged commit e726155 into withastro:main Sep 20, 2023
9 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Sep 20, 2023
@itsmatteomanf itsmatteomanf deleted the i/lock-scroll-search branch October 2, 2023 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants