Git Product home page Git Product logo

react-resize-reporter's Introduction

react-resize-reporter

Lightweight React Component that reports width and height changes when its parent resizes.

react-resize-reporter

Install

npm install react-resize-reporter
yarn add react-resize-reporter

Usage

// <object> onresize event based implementation
import ResizeReporter from 'react-resize-reporter'
// or
// import { ResizeReporter } from 'react-resize-reporter/object'
// or the scroll event based implementation
// import { ResizeReporter } from 'react-resize-reporter/scroll'

function App() {
  return (
    <div style={{ position: 'relative' }}>
      <ResizeReporter onSizeChanged={console.log} reportInit debounce={1000} />
      <p>other stuff</p>
    </div>
  )
}

Which One Should I Use?

The <object> version is more reliable but the <object> element requires more resources (acceptable normally) and could be slow when size changes rapidly (e.g. animation) even with debounce.

Scroll event version is super lightweight and fast but in rare condition the browser may not be ready to update scroll position which may cause size changing not updating. This generally happens when the target element is frequently inserted and removed from DOM.

Props

All props are optional.

/** Report the init rendered size. Default false.  */
reportInit?: boolean
/** Debounce time in millisecond. Default no debounce. */
debounce?: number
/** Fires when width or height changes. */
onSizeChanged?: (width: number, height: number, rect: DOMRect) => void
/** Fires only when width changes. */
onWidthChanged?: (width: number, rect: DOMRect) => void
/** Fires only when height changes. */
onHeightChanged?: (height: number, rect: DOMRect) => void

The rest of the props are passed to the injected <div> or <object> element.

Limitations

Currently there is no perfect solution for detecting element resizing.

  • react-resize-detector is based on the fairly new Resize Observer API which requires an imperfect polyfill.
  • react-resize-aware injects an <object> element and listens to the resize event. Not so happy with its hook-abusing API and buggy implementation.
  • react-height relies on React lifecycle which may not truly reflect the element size changing (e.g. Animation delay or Image loading delay).

This library comes with two implementations with the same API.

The <object> version is similar to that of react-resize-aware but with cleaner implementation.

The scroll event version injects a <div> which contains two <div> children for detecting expanding and shrinking. Whenever the target resizes, one of the detectors will trigger a scroll event. The algorithm is derived from the scrolling spec which is respected by almost every browser.

This also means it comes with the same limitations as react-resize-aware:

  • The target element should be able to contain children. Replaced elements like <img> are no no. A workaround is to wrap them in a <div>.
  • The position CSS property of the target element should not be static so that the detectors (which always have the same size as the target) can use absolute to hide itself.

To trigger a "shrink-to-fit" width calculation for a block element you can apply either float, inline-block, absolutly positioning, flex or other styling methods.

react-resize-reporter's People

Contributors

crimx avatar dependabot[bot] avatar

Stargazers

Cheemi avatar Jordy (Jordan) Lee avatar  avatar

Watchers

James Cloos avatar  avatar  avatar Jordy (Jordan) Lee avatar

Forkers

kenlyau

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.