-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fa473b
commit 0d307bd
Showing
5 changed files
with
140 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
|
||
function useWindowSize(): { | ||
width: number; | ||
height: number; | ||
isMobile: boolean; | ||
isDesktop: boolean; | ||
} { | ||
// Initialize state with undefined width/height so server and client renders match | ||
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/ | ||
const [windowSize, setWindowSize] = useState<{ | ||
width: number; | ||
height: number; | ||
}>({ | ||
width: 0, | ||
height: 0 | ||
}); | ||
useEffect(() => { | ||
// Handler to call on window resize | ||
function handleResize() { | ||
// Set window width/height to state | ||
setWindowSize({ | ||
width: window.innerWidth, | ||
height: window.innerHeight | ||
}); | ||
} | ||
// Add event listener | ||
window.addEventListener("resize", handleResize); | ||
// Call handler right away so state gets updated with initial window size | ||
handleResize(); | ||
// Remove event listener on cleanup | ||
return () => window.removeEventListener("resize", handleResize); | ||
}, []); // Empty array ensures that effect is only run on mount | ||
|
||
// Memoize the result of the calculations using useMemo | ||
const memoizedWindowSize = useMemo(() => { | ||
return { | ||
...windowSize, | ||
isMobile: typeof windowSize?.width === "number" && windowSize.width < 768, | ||
isDesktop: typeof windowSize?.width === "number" && windowSize.width >= 768 | ||
}; | ||
}, [windowSize]); | ||
|
||
return memoizedWindowSize; | ||
} | ||
|
||
export { useWindowSize }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.