-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
useScreenResize
hook (SFS-1509) (#2487)
## What's the purpose of this pull request? This PR intends to add a hook to resize depending on the current screen size. It's related to the performance POC #2404. ## How it works? It checks the user's screen size (or manual browser resizing) and returns the value corresponding if is mobile or not. ### Starters Deploy Preview vtex-sites/starter.store#562
- Loading branch information
Showing
6 changed files
with
72 additions
and
16 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
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,43 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
// We are using the Moto G Power device measurement as a reference (mobile), as used by PageSpeed Insights. | ||
const MAX_MOBILE_WIDTH = 420 | ||
const MAX_TABLET_WIDTH = 768 | ||
const MIN_NOTEBOOK_WIDTH = 1280 | ||
|
||
function useScreenResize() { | ||
const [isMobile, setIsMobile] = useState(undefined) | ||
const [isTablet, setIsTablet] = useState(undefined) | ||
const [isDesktop, setIsDesktop] = useState(undefined) | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
setIsMobile(window.innerWidth <= MAX_MOBILE_WIDTH) | ||
setIsTablet( | ||
window.innerWidth > MAX_MOBILE_WIDTH && | ||
window.innerWidth <= MAX_TABLET_WIDTH | ||
) | ||
setIsDesktop(window.innerWidth >= MIN_NOTEBOOK_WIDTH) | ||
} | ||
|
||
handleResize() | ||
|
||
window.addEventListener('resize', handleResize) | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize) | ||
} | ||
}, []) | ||
|
||
return { | ||
isMobile, | ||
isTablet, | ||
isDesktop, | ||
loading: | ||
isMobile === undefined || | ||
isTablet === undefined || | ||
isDesktop === undefined, | ||
} | ||
} | ||
|
||
export default useScreenResize |