-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
3540d1c
commit c3a05d8
Showing
5 changed files
with
95 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { Props } from './'; | ||
|
||
type PropsWithoutChildren = Omit<Props, "children" | "breakpoints">; | ||
|
||
export const useBreakpoints = (props: Props): PropsWithoutChildren => { | ||
const [propsToUse, setPropsToShow] = useState<PropsWithoutChildren>(props); | ||
|
||
const { | ||
breakpoints | ||
} = props; | ||
|
||
const animationRef = useRef<number | null>(null); | ||
|
||
const requestAnimation = useCallback((): void => { | ||
if (breakpoints) { | ||
if (animationRef.current) cancelAnimationFrame(animationRef.current); | ||
animationRef.current = requestAnimationFrame( | ||
() => { | ||
const matchedBreakpoints = Object.keys(breakpoints).map((breakpoint) => { | ||
const matches = window.matchMedia(breakpoint).matches; | ||
if (matches) return breakpoint; | ||
return undefined; | ||
}).filter(Boolean) as string[]; | ||
|
||
if (matchedBreakpoints.length === 0) { | ||
setPropsToShow(props); | ||
} else { | ||
const lastMatch = matchedBreakpoints[matchedBreakpoints.length - 1]; | ||
const breakpointProps = breakpoints[lastMatch]; | ||
setPropsToShow({ | ||
...props, | ||
...breakpointProps | ||
}); | ||
} | ||
} | ||
); | ||
} | ||
}, [breakpoints]); | ||
|
||
const requestThrottledAnimation = useCallback((): void => { | ||
setTimeout(() => { | ||
requestAnimation(); | ||
}, 500); | ||
}, [requestAnimation]); | ||
|
||
useEffect(() => { | ||
if (breakpoints) { | ||
window.addEventListener('resize', requestAnimation); | ||
window.addEventListener('orientationchange', requestThrottledAnimation); | ||
requestAnimation(); | ||
} | ||
|
||
return () => { | ||
if (breakpoints) { | ||
window.removeEventListener('resize', requestAnimation); | ||
window.removeEventListener('orientationchange', requestThrottledAnimation); | ||
} | ||
}; | ||
}, [ | ||
requestAnimation, | ||
requestThrottledAnimation, | ||
breakpoints | ||
]); | ||
|
||
// @ts-ignore | ||
delete propsToUse.breakpoints; | ||
// @ts-ignore | ||
delete propsToUse.children; | ||
|
||
return propsToUse; | ||
} |
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