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

fix(v2): Add hooks to detect window resize, toggle off sidebar and navbar in desktop #2932

Merged
merged 4 commits into from
Jun 17, 2020

Conversation

guillaumejacquart
Copy link
Contributor

Motivation

Resolve #2750

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

  1. Change browser size to mobile (<=996px)
  2. Open one of the nav bar or doc sidebar using the mobile toggling button
  3. Resize to desktop width (> 996px)
  4. Observe reset of mobile navigation

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 14, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jun 14, 2020

Deploy preview for docusaurus-2 ready!

Built with commit b189668

https://deploy-preview-2932--docusaurus-2.netlify.app

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, this is what I had in mind.

Would like the && and || line changed but the rest looks good

function getSize() {
return isClient
? (window.innerWidth > desktopThresholdWidth && desktopSize) || mobileSize
: undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest extracting the fn outside of the hook + making it more "readable".

Usage of && and || is hard to follow

function getSize() {
  if (isClient) {
    return undefined;
  }
  returrn window.innerWidth > desktopThresholdWidth ? desktopSize : mobileSize;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, much clearer. I updated the code.

const windowSize = useWindowSize();

useEffect(() => {
if (windowSize === desktopSize && sidebarShown) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

in the same way setWindowSize(getSize()); does not trigger useless re-renders, it's not necessary to test && sidebarShown because React will already noop if setSidebarShown(false); is called while sidebarShown=false.

(also you didn't capture sidebarShown in the deps array)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum, that's subtle. I updated the code

return windowSize;
}

export {desktopSize, mobileSize};
Copy link
Collaborator

@slorber slorber Jun 16, 2020

Choose a reason for hiding this comment

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

What about having an object like WindowSizes exposing mobile/desktop attributes.

Similar to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice suggestion, I went and did that

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

build seems to fail due to accessing window object on server?

const windowSize = useWindowSize();

useEffect(() => {
if (windowSize === windowSizes.desktop && showResponsiveSidebar) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

here too you could remove showResponsiveSidebar (or you need to include it in useEffect deps)

if (windowSize === windowSizes.desktop) {
setSidebarShown(false);
}
}, [windowSize, sidebarShown]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

now here sidebarShown is not used in the effect anymore so it's not necessary to add it to the deps :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed ! I made the changes

@slorber slorber added bug An error in the Docusaurus core causing instability or issues with its execution difficulty: starter Issues that are starter difficulty level, e.g. minimal tweaking with a clear test plan. pr: bug fix This PR fixes a bug in a past release. labels Jun 17, 2020
@slorber slorber merged commit a3f54d7 into facebook:master Jun 17, 2020
@slorber
Copy link
Collaborator

slorber commented Jun 17, 2020

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution CLA Signed Signed Facebook CLA difficulty: starter Issues that are starter difficulty level, e.g. minimal tweaking with a clear test plan. pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mobile nav does not close when expanding width
5 participants