npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-resize-reporter

v1.0.2

Published

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

Downloads

203

Readme

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.