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

@jakerambo/react-container-dimensions

v0.0.0-development

Published

Wrapper component that detects element resize and passes new dimensions down the tree. Based on [element-resize-detector](https://github.com/wnr/element-resize-detector)

Downloads

5

Readme

react-container-dimensions Build Status npm version

Wrapper component that detects parent (container) element resize and passes new dimensions down the tree. Based on element-resize-detector.

npm install --save react-container-dimensions

It is especially useful when you create components with dimensions that change over time and you want to explicitely pass the container dimensions to the children. For example, SVG visualization needs to be updated in order to fit into container.

It uses getBoundingClientRect() and passes values for all top, right, bottom, left, width, height CSs attributes down the tree.

Usage

  • Wrap your existing components. Children component will recieve top, right, bottom, left, width, height as props.
<ContainerDimensions>
    <MyComponent/>
</ContainerDimensions>    
  • Use a function to pass width or height explicitely or do some calculation. Function callback will be called with an object { width: number, height: number } as an argument and it expects the output to be a React Component or an element.
<ContainerDimensions>
    { ({ height }) => <MyComponent height={height}/> }
</ContainerDimensions>    

How is it different from [similar_component_here]

It does not create a new element in the DOM but relies on the parentNode which must be present. So, basically, it acts as a middleware to pass the dimensions of your styled component to your children components. This makes it very easy to integrate with your existing code base.

For example, if your parent container has display: flex, only adjacent children will be affected by this rule. This means if your children rely on flex CSS property, you can't wrap it in a div anymore since this will break the flexbox flow.

So this won't work anymore:

<div style="display: flex">
    <div>
        <div style="flex: 1">...</div>
    </div>
</div>

react-container-dimensions doesn't change the resulting HTML markup, so it remains:

<div style="display: flex">
    <div style="flex: 1">...</div>
</div>

Example

Let's say you want your SVG visualization to always fit into the container. In order for SVG to scale elements properly it is required that width and height attributes are properly set on the svg element. Imagine the following example

Before (static)

It's hard to keep dimensions of the container and the SVG in sync. Especially, when you want your content to be resplonsive (or dynamic).

export const myVis = () => (
    <div className="myStyles">
        <svg width={600} height={400}>
            {/* SVG contents */}
        </svg>  
    <div>
)

After (dynamic)

This will resize and re-render the SVG each time the div dimensions are changed. For instance, when you change CSS for .myStyles.

import ContainerDimensions from 'react-container-dimensions'

export const myVis = () => (
    <div className="myStyles">
        <ContainerDimensions>
            { ({ width, height }) => 
                <svg width={width} height={height}>
                    {/* SVG contents */}
                </svg>  
            }
        </ContainerDimensions>
    <div>
)

Other similar projects:

  • https://github.com/digidem/react-dimensions
  • https://github.com/maslianok/react-resize-detector
  • https://github.com/Xananax/react-size
  • https://github.com/joeybaker/react-element-query

and a few others...