-
Notifications
You must be signed in to change notification settings - Fork 588
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
Showing
6 changed files
with
104 additions
and
31 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 |
---|---|---|
@@ -1,28 +1,48 @@ | ||
import { useState, useRef, useLayoutEffect, DOMElement } from "react"; | ||
import { throttle } from "lodash"; | ||
import { useLayoutEffect, useMemo, useRef, useState } from "react"; | ||
|
||
type Dimensions = { | ||
width: number; | ||
height: number; | ||
}; | ||
const DIMENSION_REFRESH_THROTTLE = 250; | ||
|
||
type Dimensions = { width: number; height: number }; | ||
|
||
export default function useDimensions() { | ||
const [bounds, update] = useState<Dimensions>(null); | ||
const ref = useRef<HTMLElement>(); | ||
const widthRef = useRef<HTMLElement>(); | ||
const heightRef = useRef<HTMLElement>(); | ||
|
||
function refresh() { | ||
if (!ref.current) return; | ||
const { width, height } = ref.current.getBoundingClientRect(); | ||
if (!ref.current && !widthRef.current && !heightRef.current) return; | ||
let height, width; | ||
if ((!widthRef.current || !heightRef.current) && ref.current) { | ||
height = ref.current.getBoundingClientRect().height; | ||
width = ref.current.getBoundingClientRect().width; | ||
} | ||
if (widthRef.current) { | ||
width = widthRef.current.getBoundingClientRect().width; | ||
} | ||
if (heightRef.current) { | ||
height = heightRef.current.getBoundingClientRect().height; | ||
} | ||
update({ width, height }); | ||
} | ||
|
||
const ro = new ResizeObserver(refresh); | ||
const throttledRefresh = useMemo(() => { | ||
return throttle(() => { | ||
refresh(); | ||
}, DIMENSION_REFRESH_THROTTLE); | ||
}, []); | ||
|
||
const ro = useMemo(() => { | ||
return new ResizeObserver(throttledRefresh); | ||
}, [throttledRefresh]); | ||
|
||
useLayoutEffect(() => { | ||
if (ref.current) { | ||
refresh(); | ||
ro.observe(ref.current); | ||
} | ||
}, [ref.current]); | ||
}, [ro, ref.current]); // eslint-disable-line | ||
|
||
return { bounds, ref, update, refresh }; | ||
return { bounds, ref, heightRef, widthRef, update, refresh }; | ||
} |