-
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.
Updated dependencies and fixed select list
- Loading branch information
Showing
4 changed files
with
207 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import {MutableRefObject, useLayoutEffect, useState} from "react"; | ||
import {useDebouncedCallback} from "use-debounce"; | ||
import {MutableRefObject, useCallback, useLayoutEffect, useState} from "react"; | ||
import {useDebounce} from "usehooks-ts"; | ||
|
||
const useIsCentered = (ref: MutableRefObject<any>) => { | ||
const [isCentered, setIsCentered] = useState(false) | ||
const [isCentered, setIsCentered] = useState<boolean>(false) | ||
const debouncedIsCentered = useDebounce<boolean>(isCentered); | ||
|
||
const resizeHandler = useDebouncedCallback(() => { | ||
const resizeHandler = useCallback(() => { | ||
if (ref?.current) { | ||
const boundingBox = ref.current.getBoundingClientRect(); | ||
setIsCentered(Math.abs((window.innerWidth - boundingBox.width) / 2 - boundingBox.x) <= 10); | ||
} | ||
}, 200) | ||
|
||
}, [ref]); | ||
|
||
useLayoutEffect(() => { | ||
resizeHandler(); | ||
window.addEventListener('resize', resizeHandler); | ||
return () => window.removeEventListener('resize', resizeHandler); | ||
}, [ref]) | ||
}, [ref, resizeHandler]) | ||
|
||
return isCentered; | ||
return debouncedIsCentered; | ||
} | ||
|
||
export default useIsCentered; |
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
Oops, something went wrong.