Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-container-dimensions

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-container-dimensions

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)

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-0.2%
Maintainers
1
Weekly downloads
 
Created
Source

react-container-dimensions Build Status

Wrapper component that detects parent (container) element resize and passes new dimensions down the tree. Based on [element-resize-detector] (https://github.com/wnr/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.

Usage

  • Wrap your existing components. Children component will recieve width and 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 ...

It does not create a new element in the DOM but relies on the parentNode which must be present. This means it doesn't require its own CSS to do the job and leaves it up to you. So, basically, it acts as a middleware to pass your styled component dimensions to your children components. This makes it very easy to integrate with your existing code base.

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:

and a few others...

Keywords

FAQs

Package last updated on 08 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc