Giter Site home page Giter Site logo

warmleaf / react-resize-detector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maslianok/react-resize-detector

0.0 2.0 0.0 655 KB

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

Home Page: http://maslianok.github.io/react-resize-detector/

License: MIT License

JavaScript 100.00%

react-resize-detector's Introduction

Handle element resizes like it's 2018!

Nowadays browsers start supporting element resize handling natively using ResizeObserver. And we use this feature (with polyfill) to help you handle element resizes in React.

⚠️ This change intriduced in v.2.0.0

For older implementations please checkout this branch v.1.1.0

Demo

Local demo:

git clone https://github.com/maslianok/react-resize-detector.git
cd react-resize-detector/examples
npm i && npm start

Installation

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

Example

callback

import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';

class App extends PureComponent {
  render() {
    return (
      <div>
        ...
        <ReactResizeDetector handleWidth handleHeight onResize={this.onResize} />
      </div>
    );
  }

  onResize = () => {
    ...
  }
}

render(<App />, document.getElementById('root'));

render chidlren function

import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';

class App extends PureComponent {
  render() {
    return (
      <div>
        ...
        <ReactResizeDetector handleWidth handleHeight>
          {(width, height) => (<div>{width}x{height}</div>)}
        </ReactResizeDetector>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

render chidlren react element

import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';

const ChildrenComponent = ({ width, height }) => (<div>{width}x{height}</div>);

class App extends PureComponent {
  render() {
    return (
      <div>
        ...
        <ReactResizeDetector handleWidth handleHeight>
          <ChildrenComponent />
        </ReactResizeDetector>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

custom

import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';

const ChildrenComponent = ({ width, height }) => (<div>{width}x{height}</div>);

class App extends PureComponent {
  render() {
    return (
      <div>
        ...
        <ReactResizeDetector handleWidth handleHeight onResize={this.onResize}>
          {(width, height) => (<div>{width}x{height}</div>)}
          <ChildrenComponent />
        </ReactResizeDetector>
      </div>
    );
  }

  onResize = () => {
    ...
  }
}

render(<App />, document.getElementById('root'));

Note: You can use multiple render methods for one call. When using render children, in mount children component will not received width and height value because window not resized, you should add a default value.

API

onResize

(Func) Function that will be invoked with width and height arguments.

handleWidth

(Bool) Trigger onResize on width change. Default: false.

handleHeight

(Bool) Trigger onResize on height change. Default: false.

skipOnMount

(Bool) Do not trigger onResize when a component mounts. Default: false.

resizableElementId

(String) Id of the element we want to observe. If no one provided, parentElement of the component will be used. Default: ``.

refreshMode

(String) Possible values: throttle and debounce See lodash docs for more information. Default: undefined - means that callback will be fired as often as ResizeObserver allows

refreshRate

(Number) Makes sense only when refreshMode is set. Default: 1000. Important! It's numeric prop so set it correctly, e.g. refreshRate={500}

License

MIT

react-resize-detector's People

Contributors

maslianok avatar lh0x00 avatar moledj avatar benjavr avatar codywalker avatar cesarp avatar remotezygote avatar matsilva avatar mherold avatar

Watchers

James Cloos avatar Orlo Wang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.