-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): add use breakpoint hook
- Loading branch information
1 parent
303a5e8
commit 24fdf83
Showing
2 changed files
with
30 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useEffect } from 'react'; | ||
import throttle from 'lodash.throttle'; | ||
|
||
const getDeviceConfig = (width: number) => { | ||
if (width < 320) { | ||
return 'xs'; | ||
} else if (width >= 320 && width < 720) { | ||
return 'sm'; | ||
} else if (width >= 720 && width < 1024) { | ||
return 'md'; | ||
} else if (width >= 1024) { | ||
return 'lg'; | ||
} | ||
}; | ||
|
||
const useBreakpoint = () => { | ||
const [breakPoint, setBreakpoint] = useState(() => getDeviceConfig(window.innerWidth)); | ||
|
||
useEffect(() => { | ||
const calcInnerWidth = throttle(function () { | ||
setBreakpoint(getDeviceConfig(window.innerWidth)) | ||
}, 200); | ||
window.addEventListener('resize', calcInnerWidth); | ||
return () => window.removeEventListener('resize', calcInnerWidth); | ||
}, []); | ||
|
||
return breakPoint; | ||
} | ||
export default useBreakpoint; |
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