-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Changes from 1 commit
a95bd04
80420b8
25da3cf
b189668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||
/** | ||||
* Copyright (c) Facebook, Inc. and its affiliates. | ||||
* | ||||
* This source code is licensed under the MIT license found in the | ||||
* LICENSE file in the root directory of this source tree. | ||||
*/ | ||||
|
||||
import {useEffect, useState} from 'react'; | ||||
|
||||
const desktopThresholdWidth = 996; | ||||
const desktopSize = 'desktop'; | ||||
const mobileSize = 'mobile'; | ||||
|
||||
function useWindowSize() { | ||||
const isClient = typeof window === 'object'; | ||||
guillaumejacquart marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
function getSize() { | ||||
return isClient | ||||
? (window.innerWidth > desktopThresholdWidth && desktopSize) || mobileSize | ||||
: undefined; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, much clearer. I updated the code. |
||||
} | ||||
|
||||
const [windowSize, setWindowSize] = useState(getSize); | ||||
|
||||
useEffect(() => { | ||||
if (!isClient) { | ||||
return false; | ||||
} | ||||
|
||||
function handleResize() { | ||||
setWindowSize(getSize()); | ||||
} | ||||
|
||||
window.addEventListener('resize', handleResize); | ||||
return () => window.removeEventListener('resize', handleResize); | ||||
}, []); | ||||
|
||||
return windowSize; | ||||
} | ||||
|
||||
export {desktopSize, mobileSize}; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about having an object like Similar to this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice suggestion, I went and did that |
||||
export default useWindowSize; |
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.
in the same way
setWindowSize(getSize());
does not trigger useless re-renders, it's not necessary to test&& sidebarShown
because React will already noop ifsetSidebarShown(false);
is called whilesidebarShown=false
.(also you didn't capture
sidebarShown
in the deps array)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.
https://twitter.com/sebastienlorber/status/1272516868994224128
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.
Hum, that's subtle. I updated the code