All Projects → maslianok → React Resize Detector

maslianok / React Resize Detector

Licence: mit
A Cross-Browser, Event-based, Element Resize Detection for React

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Resize Detector

Bild
Image processing algorithms in pure Go
Stars: ✭ 3,431 (+373.24%)
Mutual labels:  resize
Sharp
High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
Stars: ✭ 21,131 (+2814.62%)
Mutual labels:  resize
Vue Draggable Resizable Gorkys
Vue 用于可调整大小和可拖动元素的组件并支持冲突检测、元素吸附、元素对齐、辅助线
Stars: ✭ 521 (-28.14%)
Mutual labels:  resize
Fitty
✨ Makes text fit perfectly
Stars: ✭ 3,321 (+358.07%)
Mutual labels:  resize
Imaging
Imaging is a simple image processing package for Go
Stars: ✭ 4,023 (+454.9%)
Mutual labels:  resize
Mort
Storage and image processing server written in Go
Stars: ✭ 420 (-42.07%)
Mutual labels:  resize
Input Range Scss
Styling Cross-Browser Compatible Range Inputs with Sass
Stars: ✭ 272 (-62.48%)
Mutual labels:  cross-browser
Iframe Resizer
Keep same and cross domain iFrames sized to their content with support for window/content resizing, in page links, nesting and multiple iFrames
Stars: ✭ 5,585 (+670.34%)
Mutual labels:  resize
Zebra datepicker
A super-lightweight, highly configurable, cross-browser date / time picker jQuery plugin
Stars: ✭ 367 (-49.38%)
Mutual labels:  cross-browser
Vue Grid Layout
A draggable and resizable grid layout, for Vue.js.
Stars: ✭ 5,170 (+613.1%)
Mutual labels:  resize
Angular Resizable
A lightweight directive for creating resizable containers.
Stars: ✭ 317 (-56.28%)
Mutual labels:  resize
Hprose
HPROSE is short for High Performance Remote Object Service Engine. It's a serialize and RPC library, the serialize library of hprose is faster, smaller and more powerful than msgpack, the RPC library is faster, easier and more powerful than thrift.
Stars: ✭ 348 (-52%)
Mutual labels:  cross-browser
Vue Multipane
Resizable split panes for Vue.js.
Stars: ✭ 427 (-41.1%)
Mutual labels:  resize
Use Resize Observer
A React hook that allows you to use a ResizeObserver to measure an element's size.
Stars: ✭ 305 (-57.93%)
Mutual labels:  resize
Resize Observer
Polyfills the ResizeObserver API.
Stars: ✭ 540 (-25.52%)
Mutual labels:  resize
T Shirts
The first OpenSource t-shirts (probably)
Stars: ✭ 300 (-58.62%)
Mutual labels:  cross-browser
Morphext
A simple, high-performance and cross-browser jQuery rotating / carousel plugin for text phrases powered by Animate.css.
Stars: ✭ 384 (-47.03%)
Mutual labels:  cross-browser
Image Map Resizer
Responsive HTML Image Maps
Stars: ✭ 661 (-8.83%)
Mutual labels:  resize
Imagemagick
🧙‍♂️ ImageMagick 7
Stars: ✭ 6,400 (+782.76%)
Mutual labels:  resize
Embiggen Disk
embiggden-disk live-resizes a filesystem after first live-resizing any necessary layers below it: an optional LVM LV and PV, and an MBR or GPT partition table
Stars: ✭ 440 (-39.31%)
Mutual labels:  resize

Handle element resizes like it's 2021!

Live demo

Nowadays browsers support element resize handling natively using ResizeObservers. The library uses these observers to help you handle element resizes in React.

🐥 Tiny ~3kb

🐼 Written in TypeScript

🦁 Supports Function and Class Components

🐠 Used by 20k+ repositories

🦄 Generating 35M+ downloads/year

No window.resize listeners! No timeouts! No 👑 viruses! :)

TypeScript-lovers notice: starting from v6.0.0 you may safely remove @types/react-resize-detector from you deps list.

Installation

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

and

import ResizeObserver from 'react-resize-detector';

// or, in case you need to support some old browsers
import ResizeObserver from 'react-resize-detector/build/withPolyfill';

Examples

Starting from v6.0.0 there are 3 recommended ways to work with resize-detector library:

1. React hook (new in v6.0.0)

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
import { useResizeDetector } from 'react-resize-detector';

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

2. HOC pattern

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

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

export default withResizeDetector(CustomComponent);

3. Child Function Pattern

import ReactResizeDetector from 'react-resize-detector';

// ...

<ReactResizeDetector handleWidth handleHeight>
  {({ width, height }) => <div>{`${width}x${height}`}</div>}
</ReactResizeDetector>;
Full example (Class Component)
import React, { Component } from 'react';
import { withResizeDetector } from 'react-resize-detector';

const containerStyles = {
  height: '100vh',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center'
};

class AdaptiveComponent extends Component {
  state = {
    color: 'red'
  };

  componentDidUpdate(prevProps) {
    const { width } = this.props;

    if (width !== prevProps.width) {
      this.setState({
        color: width > 500 ? 'coral' : 'aqua'
      });
    }
  }

  render() {
    const { width, height } = this.props;
    const { color } = this.state;
    return <div style={{ backgroundColor: color, ...containerStyles }}>{`${width}x${height}`}</div>;
  }
}

const AdaptiveWithDetector = withResizeDetector(AdaptiveComponent);

const App = () => {
  return (
    <div>
      <p>The rectangle changes color based on its width</p>
      <AdaptiveWithDetector />
    </div>
  );
};

export default App;
Full example (Functional Component)
import React, { useState, useEffect } from 'react';
import { withResizeDetector } from 'react-resize-detector';

const containerStyles = {
  height: '100vh',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center'
};

const AdaptiveComponent = ({ width, height }) => {
  const [color, setColor] = useState('red');

  useEffect(() => {
    setColor(width > 500 ? 'coral' : 'aqua');
  }, [width]);

  return <div style={{ backgroundColor: color, ...containerStyles }}>{`${width}x${height}`}</div>;
};

const AdaptiveWithDetector = withResizeDetector(AdaptiveComponent);

const App = () => {
  return (
    <div>
      <p>The rectangle changes color based on its width</p>
      <AdaptiveWithDetector />
    </div>
  );
};

export default App;

We still support other ways to work with this library, but in the future consider using the ones described above. Please let me know if the examples above don't fit your needs.

Performance optimization

This library uses the native ResizeObserver API.

DOM nodes get attached to ResizeObserver.observe every time the component mounts and every time any property gets changed.

It means you should try to avoid passing anonymous functions to ResizeDetector, because they will trigger the whole initialization process every time the component rerenders. Use useCallback whenever it's possible.

// WRONG - anonymous function
const { ref, width, height } = useResizeDetector({
  onResize: () => {
    // on resize logic
  }
});

// CORRECT - `useCallback` approach
const onResize = useCallback(() => {
  // on resize logic
}, []);

const { ref, width, height } = useResizeDetector({ onResize });

Refs

The below explanation doesn't apply to useResizeDetector

The library is trying to be smart and to not add any extra DOM elements to not break your layouts. That's why we use findDOMNode method to find and attach listeners to the existing DOM elements. Unfortunately, this method has been deprecated and throws a warning in StrictMode.

For those who wants to avoid this warning we are introducing an additional property - targetRef. You have to set this prop as a ref of your target DOM element and the library will use this reference instead of serching the DOM element with help of findDOMNode

HOC pattern example
import { withResizeDetector } from 'react-resize-detector';

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

export default withResizeDetector(CustomComponent);
Child Function Pattern example
import ReactResizeDetector from 'react-resize-detector';

// ...

<ReactResizeDetector handleWidth handleHeight>
  {({ width, height, targetRef }) => <div ref={targetRef}>{`${width}x${height}`}</div>}
</ReactResizeDetector>;

API

Prop Type Description Default
onResize Func Function that will be invoked with width and height arguments undefined
handleWidth Bool Trigger onResize on width change true
handleHeight Bool Trigger onResize on height change true
skipOnMount Bool Do not trigger onResize when a component mounts false
refreshMode String Possible values: throttle and debounce See lodash docs for more information. undefined - callback will be fired for every frame undefined
refreshRate Number Use this in conjunction with refreshMode. Important! It's a numeric prop so set it accordingly, e.g. refreshRate={500} 1000
refreshOptions Object Use this in conjunction with refreshMode. An object in shape of { leading: bool, trailing: bool }. Please refer to lodash's docs for more info undefined
observerOptions Object These options will be used as a second parameter of resizeObserver.observe method. undefined
targetRef Ref Use 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 functions undefined

License

MIT

❤️

Show us some love and STAR ⭐ the project if you find it useful

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].