-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCSSGrid.jsx
More file actions
89 lines (75 loc) · 2.35 KB
/
CSSGrid.jsx
File metadata and controls
89 lines (75 loc) · 2.35 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { Component } from 'react';
import ReactTransitionGroup from 'react-addons-transition-group';
import shallowEqual from 'shallowequal';
import omit from 'just-omit';
import { commonPropTypes, commonDefaultProps } from '../utils/commonProps';
import { cubicOut } from '../utils/easings';
import assertIsElement from '../utils/assertIsElement';
import CSSGridItem from './CSSGridItem';
export default class extends Component {
static propTypes = {
...commonPropTypes,
duration: React.PropTypes.number.isRequired,
easing: React.PropTypes.string
};
static defaultProps = {
...commonDefaultProps,
easing: cubicOut
};
constructor(props) {
super(props);
this.state = this.doLayout(props);
}
componentWillReceiveProps(nextProps) {
if (!shallowEqual(nextProps, this.props)) {
this.setState(this.doLayout(nextProps));
}
}
doLayout(props) {
const { positions, gridWidth, gridHeight } =
props.layout(React.Children.toArray(props.children)
.map((item) => {
assertIsElement(item);
return {
...item.props,
key: item.key
};
}), props);
return { gridWidth, gridHeight, positions };
}
render() {
const { component, style, children, duration,
easing, lengthUnit, ...rest } = omit(this.props, ['itemHeight', 'measured',
'columns', 'columnWidth', 'gutterWidth', 'gutterHeight', 'layout', 'enter',
'entered', 'exit', 'perspective', 'springConfig', 'angleUnit']);
const items = React.Children.toArray(children);
const { positions, gridWidth, gridHeight } = this.state;
const transition = ['opacity', 'transform'].map(prop =>
`${prop} ${duration}ms ${easing}`).join(', ');
const wrappedItems = items.map((item, i) =>
<CSSGridItem
key={item.key}
position={positions[i]}
{...this.props}
transition={transition}
gridProps={this.props}
gridState={this.state}
>
{item}
</CSSGridItem>);
return (
<ReactTransitionGroup
component={component}
style={{
position: 'relative',
...style,
width: `${gridWidth}${lengthUnit}`,
height: `${gridHeight}${lengthUnit}`
}}
{...rest}
>
{wrappedItems}
</ReactTransitionGroup>
);
}
}