Skip to content

Commit dc3f4df

Browse files
committed
feat(utils): created a new useResizeObserver implementation
Note: This is fully backwards compatible but the old API will be removed in the next major version bump. The new `useResizeObserver` implementation provides a `ref` and a `refHandler` where the `refHandler` should be passed to the DOM node that is being watched. The `target` API is kind of deprecated since it does some weird things and doesn't help with the resize observer loop behavior. When using the new API, the event handlers will now use a "subscription" model where only a single `ResizeObserver` is created instead of multiple since it helps increase performance.
1 parent deacf1c commit dc3f4df

8 files changed

Lines changed: 889 additions & 155 deletions

File tree

packages/documentation/src/components/Demos/Utils/ResizeObserverExample.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
} from "react";
99
import { Button } from "@react-md/button";
1010
import {
11-
ObservedResizeEventHandler,
11+
OnResizeObserverChange,
1212
ResizeObserver,
1313
useToggle,
1414
} from "@react-md/utils";
@@ -23,7 +23,7 @@ const DEFAULT_WIDTH = 150;
2323
interface Size {
2424
height: number;
2525
width: number;
26-
onResize: ObservedResizeEventHandler;
26+
onResize: OnResizeObserverChange;
2727
}
2828

2929
/**
@@ -36,7 +36,7 @@ function useSize(): Size {
3636
width: DEFAULT_WIDTH,
3737
});
3838

39-
const onResize: ObservedResizeEventHandler = (event) => {
39+
const onResize: OnResizeObserverChange = (event) => {
4040
const { height, width } = event;
4141
setSize({ height, width });
4242
};

packages/utils/src/layout/GridList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import cn from "classnames";
1111

1212
import applyRef from "../applyRef";
1313
import bem from "../bem";
14-
import useResizeObserver from "../sizing/useResizeObserver";
14+
import { useResizeObserver } from "../sizing/useResizeObserver";
1515
import GridListCell from "./GridListCell";
1616
import getScrollbarSize from "./scrollbarSize";
1717
import { GridListSizeProvider, GridListSize } from "./context";

packages/utils/src/sizing/ResizeObserver.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React, { ElementType, ReactElement, useCallback, useState } from "react";
22

3-
import useResizeObserver, {
4-
ObservedResizeEventHandler,
5-
ResizeObserverTarget,
6-
} from "./useResizeObserver";
3+
import { useResizeObserver, OnResizeObserverChange } from "./useResizeObserver";
4+
import { ResizeObserverTarget } from "./useResizeObserverV1";
75

86
export interface ResizeObserverProps {
97
/**
@@ -51,13 +49,16 @@ export interface ResizeObserverProps {
5149
* the next height, width, scrollHeight, scrollWidth, and the element that is
5250
* being observed.
5351
*/
54-
onResize: ObservedResizeEventHandler;
52+
onResize: OnResizeObserverChange;
5553
}
5654

5755
/**
5856
* The resize observer is used to track the size changes for a single element in
59-
* a page. This is a bit different than a normal `ResizeListener` since it does
57+
* a page. This is a bit different than a normal `ResizeListener` since it does
6058
* not rely on entire page size changes.
59+
*
60+
* @deprecated 2.3.0 You should really use the `useResizeObserver` hook instead
61+
* since it offers a lot more flexibility and functionality than this component.
6162
*/
6263
function ResizeObserver({
6364
disableHeight = false,

0 commit comments

Comments
 (0)