diff --git a/src/web/index.ts b/src/web/index.ts
index 47a032b..5a8cd28 100644
--- a/src/web/index.ts
+++ b/src/web/index.ts
@@ -37,9 +37,12 @@ export type Options = {
debounce?: number | { scroll: number; resize: number }
scroll?: boolean
polyfill?: { new (cb: ResizeObserverCallback): ResizeObserver }
+ dependencies?: unknown[]
}
-function useMeasure({ debounce, scroll, polyfill }: Options = { debounce: 0, scroll: false }): Result {
+function useMeasure(
+ { debounce, scroll, polyfill, dependencies = [] }: Options = { debounce: 0, scroll: false }
+): Result {
const ResizeObserver =
polyfill || (typeof window === 'undefined' ? class ResizeObserver {} : (window as any).ResizeObserver)
@@ -135,7 +138,9 @@ function useMeasure({ debounce, scroll, polyfill }: Options = { debounce: 0, scr
removeListeners()
addListeners()
}, [scroll, scrollChange, resizeChange])
-
+ useEffect(() => {
+ forceRefresh()
+ }, dependencies.concat([forceRefresh]))
// remove all listeners when the components unmounts
useEffect(() => removeListeners, [])
return [ref, bounds, forceRefresh]
diff --git a/test/src/index.tsx b/test/src/index.tsx
index ceb0997..d7ada42 100644
--- a/test/src/index.tsx
+++ b/test/src/index.tsx
@@ -22,45 +22,59 @@ function ScrollBox({ size, color, children }: { size: number | string; color: st
}
function MeasuredBox({ color }: { color: string }) {
+ const [scale, setScale] = useState(1)
+ const [scaleEnd, setScaleEnd] = useState(1)
// This line is all you need ...
- const [ref, bounds] = useMeasure({ scroll: true, debounce: { scroll: 0, resize: 0 } })
+ const [ref, bounds] = useMeasure({ scroll: true, debounce: { scroll: 0, resize: 0 }, dependencies: [scaleEnd] })
// The rest is just for effects, hover and mouse tracking
const prev = useRef(bounds)
const [big, setBig] = useState(false)
const [hovered, setHover] = useState(false)
const [xy, setXY] = useState([0, 0])
const [springs, set] = useSpring(() => Object.keys(bounds).reduce((acc, key) => ({ ...acc, [key]: 0 }), {}))
+ const toggleScale = () => {
+ setScale(scale === 1 ? 0.5 : 1)
+ }
useEffect(() => {
set(Object.keys(bounds).reduce((acc, key) => ({ ...acc, [key]: prev.current[key] !== bounds[key] ? 1 : 0 }), {}))
prev.current = { ...bounds }
}, [bounds, set])
-
+ const onTransitionEnd = () => {
+ setScaleEnd(scale)
+ }
return (
-