MCPcopy
hub / github.com/pmndrs/react-spring / useResize

Function useResize

packages/core/src/hooks/useResize.ts:37–79  ·  view source on GitHub ↗
({
  container,
  ...springOptions
}: UseResizeOptions)

Source from the content-addressed store, hash-verified

35 * @returns {SpringValues<{width: number; height: number;}>} SpringValues the collection of values returned from the inner hook
36 */
37export const useResize = ({
38 container,
39 ...springOptions
40}: UseResizeOptions): SpringValues<{
41 width: number
42 height: number
43}> => {
44 const [sizeValues, api] = useSpring(
45 () => ({
46 width: 0,
47 height: 0,
48 ...springOptions,
49 }),
50 []
51 )
52
53 useIsomorphicLayoutEffect(() => {
54 const cleanupScroll = onResize(
55 ({ width, height }) => {
56 api.start({
57 width,
58 height,
59 immediate:
60 sizeValues.width.get() === 0 ||
61 sizeValues.height.get() === 0 ||
62 springOptions.immediate === true,
63 })
64 },
65 { container: container?.current || undefined }
66 )
67
68 return () => {
69 /**
70 * Stop the springs on unmount.
71 */
72 each(Object.values(sizeValues), value => value.stop())
73
74 cleanupScroll()
75 }
76 }, [])
77
78 return sizeValues
79}

Callers

nothing calls this directly

Calls 6

useSpringFunction · 0.90
onResizeFunction · 0.90
eachFunction · 0.90
startMethod · 0.65
getMethod · 0.65
stopMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…