├── .babelrc
├── .gitignore
├── .prettierrc
├── CHANGELOG.md
├── LICENSE
├── README.md
├── package.json
├── rollup.config.js
├── site
├── .gitignore
├── .prettierrc
├── gatsby-config.js
├── package.json
├── src
│ ├── app.css
│ └── pages
│ │ └── index.js
└── yarn.lock
├── src
├── Measure.js
├── __tests__
│ ├── Measure.test.js
│ └── __snapshots__
│ │ └── Measure.test.js.snap
├── get-content-rect.js
├── get-types.js
├── get-window-of.js
├── index.js
└── with-content-rect.js
└── yarn.lock
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | ["@babel/preset-env", { "loose": true, "modules": false }],
4 | ["@babel/preset-react", { "useBuiltIns": true }]
5 | ],
6 | "plugins": [["@babel/plugin-proposal-class-properties", { "loose": true }]],
7 | "env": {
8 | "test": {
9 | "plugins": ["@babel/transform-modules-commonjs"]
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | npm-debug.log
4 | dist
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "es5",
4 | "semi": false
5 | }
6 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## CHANGELOG
2 |
3 | ### 2.5.2
4 |
5 | Default to global (possibly polyfilled) ResizeObserver if the local window does not have ResizeObserver #148
6 |
7 | ### 2.5.1
8 |
9 | Use `ResizeObserver` of local `window` object #147
10 |
11 | ### 2.5.0
12 |
13 | Set `innerRef` before observing [#140](https://github.com/souporserious/react-measure/pull/140)
14 |
15 | ### 2.4.0
16 |
17 | Use the local `window` object of the observed node [#146](https://github.com/souporserious/react-measure/pull/146)
18 |
19 | ### 2.3.0
20 |
21 | Fix regression with `contentRect.bounds` not getting called properly
22 |
23 | Note that `onResize` will get called twice when first mounting now
24 |
25 | ### 2.2.6
26 |
27 | Fix initial `onResize` delay caused by `requestAnimationFrame` [#135](https://github.com/souporserious/react-measure/pull/135)
28 |
29 | ### 2.2.5
30 |
31 | Fix sporadic `ResizeObserver` loop limit exceeded error when using `onResize` [#133](https://github.com/souporserious/react-measure/pull/133)
32 |
33 | ### 2.2.4
34 |
35 | Only observe one element, add tests [#130](https://github.com/souporserious/react-measure/pull/130)
36 |
37 | ### 2.2.3
38 |
39 | Fix not passing `ResizeObserver` `entries` to `measure` method [#125](https://github.com/souporserious/react-measure/pull/125)
40 |
41 | Add support for `createRef` [#126](https://github.com/souporserious/react-measure/pull/126)
42 |
43 | ### 2.2.2
44 |
45 | Add `@babel/runtime` as a dependency
46 |
47 | ### 2.2.1
48 |
49 | Fix `ResizeObserver` callback error
50 |
51 | Fix `eslint` warnings
52 |
53 | ### 2.2.0
54 |
55 | Remove `componentWillMount` for React >16 StrictMode compliance
56 | [#121](https://github.com/souporserious/react-measure/pull/121)
57 |
58 | Upgrade `get-node-dimensions` package to `1.2.1`
59 |
60 | Upgrade `prop-types` package to `15.6.2`
61 |
62 | Fixes `disconnect` being used instead of `unobserve` for `ResizeObserver`
63 |
64 | ### 2.1.3
65 |
66 | Update `resize-observer-polyfill`
67 | [#88](https://github.com/souporserious/react-measure/pull/88)
68 |
69 | Added handling when `getComputedStyle` returns `null`
70 | [#89](https://github.com/souporserious/react-measure/pull/89)
71 |
72 | Call `setState` within `requestAnimationFrame` to prevent infinite loop
73 | [#118](https://github.com/souporserious/react-measure/pull/118)
74 |
75 | ### 2.1.2
76 |
77 | Move children propType from with-content-rect to Measure
78 | [#117](https://github.com/souporserious/react-measure/pull/117)
79 |
80 | ### 2.1.1
81 |
82 | Allow children to be any element
83 | [#78](https://github.com/souporserious/react-measure/pull/78)
84 |
85 | ### 2.1.0
86 |
87 | Disconnect and de-initialize resize observer on unmount
88 | [#112](https://github.com/souporserious/react-measure/pull/112)
89 |
90 | Remove `babel-plugin-add-module-exports`
91 |
92 | ### 2.0.2
93 |
94 | Disconnect correct node within `handleRef`
95 | [#51](https://github.com/souporserious/react-measure/pull/51)
96 |
97 | ### 2.0.1
98 |
99 | Observe and disconnect ResizeObserver in ref callback
100 |
101 | ### 2.0.0
102 |
103 | Complete rewrite. Check README for new docs.
104 |
105 | Most transitions from the old API should be easy. You just need to pass a ref
106 | callback down now. If you have any issues please feel free to file an issue.
107 |
108 | ### 1.4.7
109 |
110 | Update to use separate prop-types package as per React 15.5 deprecation
111 | [#43](https://github.com/souporserious/react-measure/pull/43#pullrequestreview-32216767)
112 |
113 | ### 1.4.6
114 |
115 | Update to `resize-observer-polyfill` 1.4.1
116 |
117 | ### 1.4.5
118 |
119 | Update to `resize-observer-polyfill` 1.3.1 to fix Webpack 2 issues
120 | [#29](https://github.com/souporserious/react-measure/issues/29)
121 |
122 | Remove monkey patch for importing `resize-observer-polyfill`
123 |
124 | ### 1.4.4
125 |
126 | Use ResizeObserver.default if available. This fixes older browsers in the local
127 | dev environment.
128 |
129 | ### 1.4.3
130 |
131 | Requiring default export of `resize-observer-polyfill`
132 | [#28](https://github.com/souporserious/react-measure/pull/28)
133 |
134 | ### 1.4.2
135 |
136 | Only require ResizeObserver polyfill when window is available
137 |
138 | ### 1.4.1
139 |
140 | Cleanup old element-resize-detector code
141 | [#23](https://github.com/souporserious/react-measure/pull/23)
142 |
143 | ### 1.4.0
144 |
145 | Moved away from element-resize-detector in favor of
146 | [resize-observer-polyfill](https://github.com/que-etc/resize-observer-polyfill)
147 | 🎉
148 |
149 | ### 1.3.1
150 |
151 | Fixes exception when changing key of rendered child
152 | [#19](https://github.com/souporserious/react-measure/issues/19)
153 |
154 | ### 1.3.0
155 |
156 | Update get-node-dimensions to 1.2.0
157 |
158 | Use `includeMargin` to account for margins when calculating dimensions now
159 |
160 | ### 1.2.2
161 |
162 | Fix dist build
163 |
164 | ### 1.2.1
165 |
166 | Ensure `setState` is not called after unmounting
167 | [#18](https://github.com/souporserious/react-measure/pull/18)
168 |
169 | ### 1.2.0
170 |
171 | Provide dimension defaults
172 |
173 | ### 1.1.0
174 |
175 | Update get-node-dimensions to 1.1.0
176 |
177 | ### 1.0.0
178 |
179 | Update get-node-dimensions to 1.0.0
180 |
181 | `accurate` renamed to `useClone`
182 |
183 | Added `cloneOptions` prop that gets passed to `getNodeDimensions`
184 |
185 | Fixed build to not include `get-node-dimensions` library
186 |
187 | Removed bower support
188 |
189 | ### 0.5.1
190 |
191 | Use properties instead of constructor
192 |
193 | When unmounting, call uninstall in addition to removeAllListeners
194 | [#15](https://github.com/souporserious/react-measure/pull/15)
195 |
196 | ### 0.5.0
197 |
198 | Moved dimension calculations to its own library
199 |
200 | Cleaned up build files for NPMCDN
201 |
202 | ### 0.4.2
203 |
204 | Removed old code from `lib` folder
205 |
206 | Make sure `package.json` cleans `lib` folder on each build
207 |
208 | ### 0.4.1
209 |
210 | Fixed dist build
211 |
212 | Updated to latest element-resize-detector
213 |
214 | ### 0.4.0
215 |
216 | Moved away from MutationObserver's in favor of
217 | [element-resize-detector](https://github.com/wnr/element-resize-detector)
218 |
219 | Added a more convenient API by allowing child functions
220 | [#11](https://github.com/souporserious/react-measure/issues/11)
221 |
222 | `measure` is now a public method available on the Measure component
223 |
224 | `accurate` prop now returns both cloned element width and height
225 |
226 | `shouldMeasure` now accepts only a boolean
227 |
228 | Removed `lodash.debounce` dependency
229 |
230 | ### 0.3.5
231 |
232 | Fixed bug in IE with accurate height calculation when checking for children
233 | nodes.
234 |
235 | Fixed
236 | [deprecation notice](https://www.chromestatus.com/features/5724912467574784)
237 | when calculating SVG dimensions.
238 |
239 | Removed `react-addons-shallow-compare` dependency.
240 |
241 | Moved `react` and `react-dom` packages into peer dependencies.
242 |
243 | ### 0.3.4
244 |
245 | Fix server-side rendering
246 |
247 | ### 0.3.3
248 |
249 | Added public method `getDimensions`
250 |
251 | Clone nodes without any children
252 |
253 | Fixed calculating measurements on resize
254 |
255 | ### 0.3.2
256 |
257 | Patch to fix `shallowCompare` so bower works.
258 |
259 | Added a resize handler to measure component changes on window resize.
260 |
261 | ### 0.3.1
262 |
263 | Renamed `onChange` prop to `onMeasure`
264 |
265 | Added `shouldMeasure` prop, similar to componentShouldUpdate. It determines
266 | whether or not the `onMeasure` callback will fire, useful for perf and not
267 | performing measurements if you don't need to.
268 |
269 | Fixed updating of `config` prop to disconnect and reconnect a new
270 | MutationObserver with the new configuration
271 |
272 | Fixed updaing of `whitelist` & `blacklist` props to use new values
273 |
274 | ### 0.3.0
275 |
276 | Rebuilt from the ground up
277 |
278 | No more cloning of elements!
279 |
280 | Optimized to touch the DOM as least as possible
281 |
282 | `clone`, `forceAutoHeight`, `collection` props removed
283 |
284 | `config` prop added, accepts a
285 | [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#MutationObserverInit)
286 | configuration
287 |
288 | `accurate` prop added, use to get an accurate measurement, only height supported
289 | right now
290 |
291 | ### 0.2.0
292 |
293 | Upgraded to React 0.14.0
294 |
295 | ### 0.1.3
296 |
297 | Added `forceAutoHeight` prop to help with proper height calculation when
298 | children heights are animating
299 |
300 | ### 0.1.2
301 |
302 | Clone prop now exposed to allow optional cloning of component
303 |
304 | Defaults to false which could potentially break components relying on cloned
305 | calculations
306 |
307 | ### 0.1.1
308 |
309 | Set width/height to auto on clone no matter what to get a true dimension
310 |
311 | Append clone directly after original instead of the end of its parent
312 |
313 | Portal now gets destroyed after measurements have been calculated
314 |
315 | ### 0.1.0
316 |
317 | Rewritten to be more React friendly
318 |
319 | Measure component no longer accepts a child function, instead get dimensions by
320 | setting state in onChange callback
321 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 React Measure authors
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 Measure
2 |
3 | [](https://badge.fury.io/js/react-measure)
4 | [](https://david-dm.org/souporserious/react-measure)
5 |
6 | Compute measurements of React components. Uses a
7 | [`ResizeObserver`](https://developers.google.com/web/updates/2016/10/resizeobserver)
8 | to detect when an element's dimensions have changed.
9 |
10 | Includes a
11 | [polyfill for `ResizeObserver`](https://github.com/que-etc/resize-observer-polyfill)
12 | in unsupported browsers.
13 |
14 | ## Install
15 |
16 | `yarn add react-measure`
17 |
18 | `npm install react-measure --save`
19 |
20 | ```html
21 |
22 | (UMD library exposed as `ReactMeasure`)
23 | ```
24 |
25 | ## Measure Component
26 |
27 | Wrap any child component and calculate its client rect.
28 |
29 | ### Props
30 |
31 | #### `client`: PropTypes.bool
32 |
33 | Adds the following to `contentRect.client` returned in the child function.
34 |
35 | [clientTop](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop),
36 | [clientLeft](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientLeft),
37 | [clientWidth](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth),
38 | and
39 | [clientHeight](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight).
40 |
41 | #### `offset`: PropTypes.bool
42 |
43 | Adds the following to `contentRect.offset` returned in the child function.
44 |
45 | [offsetTop](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop),
46 | [offsetLeft](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft),
47 | [offsetWidth](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth),
48 | and
49 | [offsetHeight](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight).
50 |
51 | #### `scroll`: PropTypes.bool
52 |
53 | Adds the following to `contentRect.scroll` returned in the child function.
54 |
55 | [scrollTop](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop),
56 | [scrollLeft](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft),
57 | [scrollWidth](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth),
58 | and
59 | [scrollHeight](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight).
60 |
61 | #### `bounds`: PropTypes.bool
62 |
63 | Uses
64 | [getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect)
65 | to calculate the element rect and add it to `contentRect.bounds` returned in the
66 | child function.
67 |
68 | #### `margin`: PropTypes.bool
69 |
70 | Uses
71 | [getComputedStyle](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle)
72 | to calculate margins and add it to `contentRect.margin` returned in the child
73 | function.
74 |
75 | #### `innerRef`: PropTypes.func
76 |
77 | Use this to access the internal component `ref`.
78 |
79 | #### `onResize`: PropTypes.func
80 |
81 | Callback invoked when either element width or height have changed. Note that this will be called twice on mount to get the initial values. The first call will come from `componentDidMount` while the second call will come from the `ResizeObserver`.
82 |
83 | #### `children`: PropTypes.func
84 |
85 | Children must be a function. Will receive the following object shape:
86 |
87 | - `measureRef`: must be passed down to your component's ref in order to obtain a
88 | proper node to measure
89 |
90 | - `measure`: use to programmatically measure your component, calls the internal
91 | `measure` method in `withContentRect`
92 |
93 | - `contentRect`: this will contain any of the following allowed rects from
94 | above: `client`, `offset`, `scroll`, `bounds`, or `margin`. It will also
95 | include `entry` from the `ResizeObserver` when available.
96 |
97 | ### Example
98 |
99 | ```javascript
100 | import Measure from 'react-measure'
101 | import classNames from 'classnames'
102 |
103 | class ItemToMeasure extends Component {
104 | state = {
105 | dimensions: {
106 | width: -1,
107 | height: -1,
108 | },
109 | }
110 |
111 | render() {
112 | const { width, height } = this.state.dimensions
113 | const className = classNames(width < 400 && 'small-width-modifier')
114 |
115 | return (
116 | {
119 | this.setState({ dimensions: contentRect.bounds })
120 | }}
121 | >
122 | {({ measureRef }) => (
123 |
124 | I can do cool things with my dimensions now :D
125 | {height > 250 && (
126 |
Render responsive content based on the component size!
127 | )}
128 |
129 | )}
130 |
131 | )
132 | }
133 | }
134 | ```
135 |
136 | ## withContentRect(types) HoC
137 |
138 | A higher-order component that provides dimensions to the wrapped component.
139 | Accepts `types`, which determines what measurements are returned, similar to
140 | above. Then returns a function to pass the component you want measured.
141 |
142 | Pass an array or single value of either `client`, `offset`, `scroll`, `bounds`,
143 | or `margin` to calculate and receive those measurements as the prop
144 | `contentRect` in your wrapped component. You can also use the `measure` function
145 | passed down to programmatically measure your component if you need to. And
146 | finally, remember to pass down the `measureRef` to the component you want
147 | measured.
148 |
149 | Passes down the same props as the `Measure` child function above, `measureRef`,
150 | `measure`, and `contentRect`.
151 |
152 | Fun fact, the `Measure` component is a thin wrapper around `withContentRect`.
153 | Just check
154 | [the source](https://github.com/souporserious/react-measure/blob/master/src/Measure.js).
155 | This means your wrapped component will accept the same props as `Measure` does
156 | 😊
157 |
158 | ### Example
159 |
160 | ```javascript
161 | import { withContentRect } from 'react-measure'
162 |
163 | const ItemToMeasure = withContentRect('bounds')(
164 | ({ measureRef, measure, contentRect }) => (
165 |
10 | The path of the righteous man is beset on all sides by the iniquities of
11 | the selfish and the tyranny of evil men. Blessed is he who, in the name
12 | of charity and good will, shepherds the weak through the valley of
13 | darkness, for he is truly his brother's keeper and the finder of lost
14 | children. And I will strike down upon thee with great vengeance and
15 | furious anger those who would attempt to poison and destroy My brothers.
16 | And you will know My name is the Lord when I lay My vengeance upon thee.{' '}
17 |