10.0.1 ā€¢ Published 3 months ago

react-resize-detector v10.0.1

Weekly downloads
842,042
License
MIT
Repository
github
Last release
3 months ago

Handle element resizes like it's 2024!

Live demo

Modern browsers now have native support for detecting element size changes through ResizeObservers. This library utilizes ResizeObservers to facilitate managing element size changes in React applications.

šŸ„ Tiny ~2kb

šŸ¼ Written in TypeScript

šŸ  Used by 160k repositories

šŸ¦„ Produces 100 million downloads annually

No window.resize listeners! No timeouts!

Is it necessary for you to use this library?

Container queries now work in all major browsers. It's very likely you can solve your task using pure CSS.

<div class="post">
  <div class="card">
    <h2>Card title</h2>
    <p>Card content</p>
  </div>
</div>
.post {
  container-type: inline-size;
}

/* Default heading styles for the card title */
.card h2 {
  font-size: 1em;
}

/* If the container is larger than 700px */
@container (min-width: 700px) {
  .card h2 {
    font-size: 2em;
  }
}

Installation

npm i react-resize-detector
// OR
yarn add react-resize-detector

Example

import { useResizeDetector } from 'react-resize-detector';

const CustomComponent = () => {
  const { width, height, ref } = useResizeDetector();
  return <div ref={ref}>{`${width}x${height}`}</div>;
};

With props

import { useResizeDetector } from 'react-resize-detector';

const CustomComponent = () => {
  const onResize = useCallback(() => {
    // on resize logic
  }, []);

  const { width, height, ref } = useResizeDetector({
    handleHeight: false,
    refreshMode: 'debounce',
    refreshRate: 1000,
    onResize
  });

  return <div ref={ref}>{`${width}x${height}`}</div>;
};

With custom ref

It's not advised to use this approach, as dynamically mounting and unmounting the observed element could lead to unexpected behavior.

import { useResizeDetector } from 'react-resize-detector';

const CustomComponent = () => {
  const targetRef = useRef();
  const { width, height } = useResizeDetector({ targetRef });
  return <div ref={targetRef}>{`${width}x${height}`}</div>;
};

API

PropTypeDescriptionDefault
onResizeFuncFunction that will be invoked with width and height argumentsundefined
handleWidthBoolTrigger onResize on width changetrue
handleHeightBoolTrigger onResize on height changetrue
skipOnMountBoolDo not trigger onResize when a component mountsfalse
refreshModeStringPossible values: throttle and debounce See lodash docs for more information. undefined - callback will be fired for every frameundefined
refreshRateNumberUse this in conjunction with refreshMode. Important! It's a numeric prop so set it accordingly, e.g. refreshRate={500}1000
refreshOptionsObjectUse this in conjunction with refreshMode. An object in shape of { leading: bool, trailing: bool }. Please refer to lodash's docs for more infoundefined
observerOptionsObjectThese options will be used as a second parameter of resizeObserver.observe method.undefined
targetRefRefUse this prop to pass a reference to the element you want to attach resize handlers to. It must be an instance of React.useRef or React.createRef functionsundefined

Testing with Enzyme and Jest

Thanks to @Primajin for posting this snippet

const { ResizeObserver } = window;

beforeEach(() => {
  delete window.ResizeObserver;
  window.ResizeObserver = jest.fn().mockImplementation(() => ({
    observe: jest.fn(),
    unobserve: jest.fn(),
    disconnect: jest.fn()
  }));

  wrapper = mount(<MyComponent />);
});

afterEach(() => {
  window.ResizeObserver = ResizeObserver;
  jest.restoreAllMocks();
});

it('should do my test', () => {
  // [...]
});

License

MIT

ā¤ļø

Show us some love and STAR ā­ the project if you find it useful

@simonri/recharts@procore/labs-data-table@preply/chat@my-sahab/mui@storybook/addon-jest@storybook/addon-a11yatx-filemanager@productfy/commoncreative-gallery-shonp@hacktion/test-library@data-driven/components@ventusrisk/jslib@bytap/rechartsehacellsstuff-ui2alanzouteststserver-frontend-comp-testcl2-frontjw-react-naver-maps@mimshins/sonnatcontent-client@graphique/ggshaman-online-meeting@walkme/wm-uibokabord-web-componentsreact-echarts-resizeablereact-image-leaflet@tomasmoosee/renderer@tomasmoosee/shared@tomasmoosee/widget-editor@donghakang/react-cornerstone-viewportreact-scrl@factoryfour/react-patternshms-video-reactcognitiv-ui@axiomhq/axiom-elementsyaguara-recharts@visualbi/ibcs-charts@react-max/modal@hopara/hopara@knapsack/app-uifurcata-connector@enclave/layout@everything-registry/sub-chunk-2607dt-library@xrhcc-ui/basemsg-odds-summary-femsg-odds-summary-fe-test-1rljd-mobilejuicyfrontdernetsoft-component-librarysaagie-uidouhub-ui-webdouhub-ui-web-platformdouhub-ui-web-premiumkinetic-uisd-react-uikiali-uidora-designdh-rechartsjingsheng-datasheet-grid@component-controls/blocksreact-prettier-code-tagreact-sandbox-editor@deep-foundation/deepcasereact2-cornerstone-viewportreact6145react2-vtkjs-viewportreact-ol-wrapperreact-omni-tablereact-naver-maps2iris_shared_kitinforiver-enterpriseinforiver-standard@deepcase/deeplinks-app@decathlon/react-table@deepmala0410/emilus-template2@deepmala0410/emilus-template3@deepmala0410/using-emilus-library@deepmala0410/using-emilus-library-package@deepmala0410/using-emilus-library-package-1@deepmala0410/emilus@deepmala0410/emilus-app@deepmala0410/emilus-library@deepmala0410/emilus-temp@deepmala0410/emilus-template@deepmala0410/emilus-template1@deepmala0410/emilus-template4@deepmala0410/emilus-temporary@deepmala0410/emiluslibrary@deepmala0410/emiluslibrary1@deepmala0410/emiluslibrary2@deffets/viewerreact-photo-gallery-tsreact-recycled-list@digiforce-cloud/x6-react-componentsdash-core-componentsdash_express_componentsdash_luckysheetdecharts
10.0.0

3 months ago

10.0.1

3 months ago

10.0.0-beta.2

3 months ago

9.1.2-beta.0

4 months ago

10.0.0-beta.1

4 months ago

9.1.1

4 months ago

10.0.0-beta.0

4 months ago

8.1.0

1 year ago

9.0.0

1 year ago

8.0.5

1 year ago

8.0.4

1 year ago

9.1.0

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

8.0.3

1 year ago

8.0.2

1 year ago

7.0.2

2 years ago

7.0.1

2 years ago

7.1.2

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

7.0.0

2 years ago

6.7.8

2 years ago

6.7.7

2 years ago

6.7.6

3 years ago

6.7.5

3 years ago

6.7.4

3 years ago

6.7.3

3 years ago

6.7.0-rc.0

3 years ago

6.7.0

3 years ago

6.7.2

3 years ago

6.7.1

3 years ago

6.6.5

3 years ago

6.6.4

3 years ago

6.6.3

3 years ago

6.6.2

3 years ago

6.6.1

3 years ago

6.6.0

3 years ago

6.5.0

3 years ago

6.3.3

3 years ago

6.4.0-rc.0

3 years ago

6.3.2

3 years ago

6.3.0

3 years ago

6.3.1

3 years ago

6.2.1

3 years ago

6.2.0

3 years ago

6.1.0

3 years ago

6.0.1-rc.5

3 years ago

6.0.1-rc.4

3 years ago

6.0.1-rc.2

3 years ago

6.0.1-rc.3

3 years ago

6.0.1-rc.1

3 years ago

6.0.0-alpha.1

3 years ago

6.0.0-alpha.0

3 years ago

5.2.0

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.7

4 years ago

5.0.6

4 years ago

5.0.5

4 years ago

5.0.4

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.2.3

4 years ago

4.2.2

4 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.4

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.4.0

5 years ago

3.3.1

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.1.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago