MCPcopy
hub / github.com/bvaughn/react-window / useResizeObserver

Function useResizeObserver

lib/hooks/useResizeObserver.ts:5–83  ·  view source on GitHub ↗
({
  box,
  defaultHeight,
  defaultWidth,
  disabled: disabledProp,
  element,
  mode,
  style
}: {
  box?: ResizeObserverBoxOptions;
  defaultHeight?: number;
  defaultWidth?: number;
  disabled?: boolean;
  element: HTMLElement | null;
  mode?: "only-height" | "only-width";
  style: CSSProperties | undefined;
})

Source from the content-addressed store, hash-verified

3import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
4
5export function useResizeObserver({
6 box,
7 defaultHeight,
8 defaultWidth,
9 disabled: disabledProp,
10 element,
11 mode,
12 style
13}: {
14 box?: ResizeObserverBoxOptions;
15 defaultHeight?: number;
16 defaultWidth?: number;
17 disabled?: boolean;
18 element: HTMLElement | null;
19 mode?: "only-height" | "only-width";
20 style: CSSProperties | undefined;
21}) {
22 const { styleHeight, styleWidth } = useMemo(
23 () => ({
24 styleHeight: parseNumericStyleValue(style?.height),
25 styleWidth: parseNumericStyleValue(style?.width)
26 }),
27 [style?.height, style?.width]
28 );
29
30 const [state, setState] = useState<{
31 height: number | undefined;
32 width: number | undefined;
33 }>({
34 height: defaultHeight,
35 width: defaultWidth
36 });
37
38 const disabled =
39 disabledProp ||
40 (mode === "only-height" && styleHeight !== undefined) ||
41 (mode === "only-width" && styleWidth !== undefined) ||
42 (styleHeight !== undefined && styleWidth !== undefined);
43
44 useIsomorphicLayoutEffect(() => {
45 if (element === null || disabled) {
46 return;
47 }
48
49 const resizeObserver = new ResizeObserver((entries) => {
50 for (const entry of entries) {
51 const { contentRect, target } = entry;
52 if (element === target) {
53 setState((prevState) => {
54 if (
55 prevState.height === contentRect.height &&
56 prevState.width === contentRect.width
57 ) {
58 return prevState;
59 }
60
61 return {
62 height: contentRect.height,

Callers 3

useVirtualizerFunction · 0.90
RefComposition.tsxFile · 0.85

Calls 3

parseNumericStyleValueFunction · 0.90
observeMethod · 0.80
unobserveMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…