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-size-fetcher

v2.4.3

Published

Transparently retrieve a component size

Downloads

44

Readme

React-Size-Fetcher

ReactSizeFetcher is a simple-to-use React library to transparently and dynamically retrieve the DOM sizes of a sub-component.

It is a tiny (8kB) library.

Travis Build Version Code Coverage

Installation

npm install --save react-size-fetcher

Documentation

SizeFetcher is a Higher Order Component; by giving it a component, it will return an enhanced component.

const EnhancedComponent = SizeFetcher(ComponentToObserve, [options])

The enhanced component is special, it will be a copy of the given component but will accept a new prop called sizeChange.

<EnhancedComponent {/* ComponentToObserve Props */} sizeChange={size => console.log('Size Changed: ', size)} />

sizeChange is a function with one argument, it will be called with an Object representing the size of the component when the component did mount and when its size change.

// Size Changed: { clientWidth: 120, clientHeight: 230, scrollWidth: 120, scrollHeight: 430 }

Arguments

  • Component (React Component): This can be a React Functional or Class Component.
  • [options] (Object): Available options:
    • [noComparison] (Boolean): Default value: false. This option allow you to bypass SizeFetcher optimization. SizeFetcher usually compare all the size and do not call sizeChange if the size did not change between two updates. const EnhancedComponent = SizeFetcher(ComponentToObserve, { noComparison: true})
    • [watchSubComponents] (Array): Default value: []. This option allow you to indicate if you want to watch specific sub-components. When defined, SizeFetcher will go through the render method of each sub component of the ComponentToObserve and add a proxy that will call sizeChange function when they update. const EnhancedComponent = SizeFetcher(ComponentToObserve, { watchSubComponents: ['AccordionComponent', 'ListComponent'] })

Returns

A Higher-Order React Component that inherit from your initial component and take one more props named sizeChange. sizeChange is suceptible to be called when the component receives new props, updates its state or when the window resize.

Example

Here is a simple way to use the library:

import SizeFetcher from 'react-size-fetcher'
import ComponentToObserve from './ComponentToObserve'
const EnhancedComponent = SizeFetcher(ComponentToObserve)

class AwareComponent extends React.Component {
  constructor() {
    super()

    this.state = {
      subComponentSize: null
    }
  }
  render() {
    const { subComponentSize } = this.state

    return (
      <div>
        <h1>The size of the sub component is {JSON.stringify(subComponentSize, null, 2)}</h1>
        <EnhancedComponent sizeChange={size => this.setState({ subComponentSize: size })} {/* ComponentToObserve usual props */} />
      </div>
    )
  }
}

You can also enhance directly your ComponentToObserve by exporting if with SizeFetcher in your ComponentToObserve.js file:

export default SizeFetcher(ComponentToObserve)

or with a decorator

@SizeFetcher
class ComponentToObserve extends React.Component {
  ...

Some advanced examples can be find in the docs folder of this repository. Live example also works.

How does it work ?

SizeFetcher is what we call an inverse inheritance higher order function. It takes a component as parameters and return a component; which by inheriting from the ComponentToObserve, will be able to highjack the render of ComponentToObserve and add functions to its lifecycle.

Highjacking the render

The render highjack is usefull for SizeFetcher in two ways:

  1. To add a ref to the componentToObserve output to retrieve and store its DOM element.
  2. To enhance the rendered children if the watchSubComponents list option is not empty.

Enhancing the lifecycle

SizeFetcher will modify three basic functions of ComponentToObserve's lifecycle:

  1. componentDidMount: Add an event listener on the window resize.
  2. componentWillUnmount: Remove the event listener on the window resize.
  3. componentDidUpdate: Call the SizeFetcher returned component's private handleSizeMayHaveChanged function.

The window resize listener will also call handleSizeMayHaveChanged function on resize.

handleSizeMayHaveChanged

This function calls the given ComponentToObserve's sizeChange prop if the size of the DOM element (retrieved earlier at highjacking step 1.) did change compared to the last call values. The option noComparison will bypass the comparison and call sizeChange at every handleSizeMayHaveChange call.

Enhancing sub-components

This is were the magic happens. SizeFetcher will go through every sub component and look up for components' name that are in the watchSubComponents list and add to them a props called sizeMayChange. This props will be called everytime the sub-component updates. When this props is called, it will call the SizeFetcher returned component's private handleSizeMayHaveChanged function and trigger the sizeChange if pertinent.

Change Log

This project adheres to Semantic Versioning.

You can find every release documented on the Releases page.

License

MIT