-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathSpringGrid.jsx
More file actions
156 lines (135 loc) · 4.37 KB
/
SpringGrid.jsx
File metadata and controls
156 lines (135 loc) · 4.37 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { Component } from 'react';
import { TransitionMotion, spring } from 'react-motion';
import stripStyle from 'react-motion/lib/stripStyle';
import shallowEqual from 'shallowequal';
import omit from 'just-omit';
import { buildTransform, positionToProperties } from '../utils/transformHelpers';
import { commonPropTypes, commonDefaultProps } from '../utils/commonProps';
import assertIsElement from '../utils/assertIsElement';
export default class extends Component {
static propTypes = {
...commonPropTypes,
springConfig: React.PropTypes.shape({
stiffness: React.PropTypes.number,
damping: React.PropTypes.number,
precision: React.PropTypes.number
})
};
static defaultProps = {
...commonDefaultProps,
springConfig: { stiffness: 60, damping: 14, precision: 0.1 }
};
constructor(props) {
super(props);
this.state = this.doLayout(props);
}
componentWillReceiveProps(nextProps) {
if (!shallowEqual(nextProps, this.props)) {
this.setState(this.doLayout(nextProps));
}
}
doLayout(props) {
const items = React.Children.toArray(props.children)
.map((element) => {
assertIsElement(element);
return {
key: element.key,
data: {
element
}
};
});
const { positions, gridWidth, gridHeight } =
props.layout(items.map(item => ({
...item.data.element.props,
key: item.data.element.key
})), props);
const styles = positions.map((position, i) => ({
...items[i],
style: {
...items[i].style,
zIndex: 2,
...springify(props.entered(items[i].data.element.props,
props, { gridWidth, gridHeight }), props.springConfig),
...springify(positionToProperties(position), props.springConfig)
}
}));
return { styles, gridWidth, gridHeight };
}
willEnter = (transitionStyle) => {
const { gridWidth, gridHeight } = this.state;
return {
...stripStyle(transitionStyle.style),
zIndex: 1,
...this.props.enter(transitionStyle.data.element.props,
this.props, { gridWidth, gridHeight })
};
};
willLeave = (transitionStyle) => {
const { gridWidth, gridHeight } = this.state;
const exitStyle = this.props.exit(transitionStyle.data.element.props,
this.props, { gridWidth, gridHeight });
return {
...transitionStyle.style,
zIndex: 0,
...springify(exitStyle, this.props.springConfig)
};
};
render() {
const { component: Parent, style, perspective, lengthUnit,
angleUnit, ...rest } = omit(this.props, ['itemHeight', 'measured',
'columns', 'columnWidth', 'gutterWidth', 'gutterHeight', 'layout',
'enter', 'entered', 'exit', 'springConfig', 'duration', 'easing']);
return (
<TransitionMotion
styles={this.state.styles}
willEnter={this.willEnter}
willLeave={this.willLeave}
>
{interpolatedStyles =>
<Parent
style={{
position: 'relative',
...style,
width: `${this.state.gridWidth}${lengthUnit}`,
height: `${this.state.gridHeight}${lengthUnit}`
}}
{...rest}
>
{interpolatedStyles.map((config) => {
const { style: { opacity, zIndex }, data } = config;
const Child = data.element.type;
const transform = buildTransform(config.style, perspective, {
length: lengthUnit, angle: angleUnit
});
const itemProps = omit(data.element.props, ['itemRect', 'itemHeight']);
return (
<Child
key={data.element.key}
{...itemProps}
style={{
...itemProps.style,
position: 'absolute',
top: 0,
left: 0,
zIndex,
opacity,
transform,
WebkitTransform: transform,
msTransform: transform
}}
/>
);
})}
</Parent>
}
</TransitionMotion>
);
}
}
function springify(style, springConfig) {
return Object.keys(style).reduce((obj, key) => {
obj[key] = spring(style[key], springConfig);
return obj;
}, {});
}