├── .babelrc
├── .editorconfig
├── .eslintrc
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── index.d.ts
├── package.json
├── src
└── index.js
├── testSetup.js
├── tests
└── index.js
└── wallaby.config.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "react",
4 | "es2015",
5 | "stage-1"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 2
6 | continuation_indent_size = 2
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 |
12 | [*.js]
13 | quote_type = single
14 | spaces_around_operators = true
15 |
16 | [*.md]
17 | trim_trailing_whitespace = false
18 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "eslint-config-okonet",
3 | "env": {
4 | "browser": true,
5 | "mocha": true
6 | },
7 | "plugins": [
8 | "import"
9 | ],
10 | "settings": {
11 | "import/resolve": {
12 | "moduleDirectory": ["node_modules"]
13 | }
14 | },
15 | "rules": {
16 | "react/no-find-dom-node": 0,
17 | "import/no-extraneous-dependencies": 0
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 |
17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18 | .grunt
19 |
20 | # node-waf configuration
21 | .lock-wscript
22 |
23 | # Compiled binary addons (http://nodejs.org/api/addons.html)
24 | build/Release
25 |
26 | # Dependency directory
27 | node_modules
28 |
29 | # Optional npm cache directory
30 | .npm
31 |
32 | # Optional REPL history
33 | .node_repl_history
34 |
35 | .idea
36 |
37 | lib
38 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | cache:
3 | directories:
4 | - node_modules
5 | notifications:
6 | email: false
7 | node_js:
8 | - '6'
9 | before_script:
10 | - npm prune
11 | after_success:
12 | - npm run semantic-release
13 | branches:
14 | except:
15 | - /^v\d+\.\d+\.\d+$/
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Andrey Okonetchnikov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-container-dimensions [](https://travis-ci.org/okonet/react-container-dimensions) [](https://badge.fury.io/js/react-container-dimensions)
2 | Wrapper component that detects parent (container) element resize and passes new dimensions down the
3 | tree. Based on [element-resize-detector](https://github.com/wnr/element-resize-detector).
4 |
5 | `npm install --save react-container-dimensions`
6 |
7 | It is especially useful when you create components with dimensions that change over
8 | time and you want to explicitely pass the container dimensions to the children. For example, SVG
9 | visualization needs to be updated in order to fit into container.
10 |
11 | It uses [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) and passes values for all `top`, `right`, `bottom`, `left`, `width`, `height` CSs attributes down the tree.
12 |
13 | ## Usage
14 |
15 | * Wrap your existing components. Children component will recieve `top`, `right`, `bottom`, `left`, `width`, `height` as props.
16 |
17 | ```jsx
18 |
19 |
20 |
21 | ```
22 |
23 | * 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.
24 |
25 | ```jsx
26 |
27 | { ({ height }) => }
28 |
29 | ```
30 |
31 | ## How is it different from [similar_component_here]
32 |
33 | *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.
34 |
35 | 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_.
36 |
37 | So this won't work anymore:
38 |
39 | ```html
40 |
41 |
42 |
...
43 |
44 |
45 | ```
46 |
47 | `react-container-dimensions` doesn't change the resulting HTML markup, so it remains:
48 |
49 | ```html
50 |
51 |
...
52 |
53 | ```
54 |
55 | ## Example
56 |
57 | 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
58 |
59 | ### Before (static)
60 |
61 | 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).
62 |
63 | ```jsx
64 | export const myVis = () => (
65 |
66 |
69 |
70 | )
71 | ```
72 |
73 | ### After (dynamic)
74 |
75 | This will resize and re-render the SVG each time the `div` dimensions are changed. For instance, when you change CSS for `.myStyles`.
76 |
77 | ```jsx
78 | import ContainerDimensions from 'react-container-dimensions'
79 |
80 | export const myVis = () => (
81 |