-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
withDimensions.js
49 lines (47 loc) · 1.66 KB
/
withDimensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { compose, setPropTypes, defaultProps, withPropsOnChange } from '@nivo/recompose'
import PropTypes from 'prop-types'
import isEqual from 'lodash/isEqual'
import { marginPropType } from '../props'
import { defaultMargin } from '../defaults'
/**
* This HOC watch width, height & margin props change
* and returns new width/height plus outer dimensions.
* Using it prevent from having a new ref each time
* we pass through the component, useful for shallow comparison.
* It also add required propTypes & set default margin.
*/
export default () =>
compose(
defaultProps({
margin: defaultMargin,
}),
setPropTypes({
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
margin: marginPropType,
}),
withPropsOnChange(
(props, nextProps) =>
props.width !== nextProps.width ||
props.height !== nextProps.height ||
!isEqual(props.margin, nextProps.margin),
props => {
const margin = Object.assign({}, defaultMargin, props.margin)
return {
margin,
width: props.width - margin.left - margin.right,
height: props.height - margin.top - margin.bottom,
outerWidth: props.width,
outerHeight: props.height,
}
}
)
)