-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.js
More file actions
276 lines (240 loc) · 8.36 KB
/
index.js
File metadata and controls
276 lines (240 loc) · 8.36 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Dimensions, StyleSheet, View } from 'react-native';
import {
BREAKPOINT_VALUES,
SIZE_NAMES,
HORIZONTAL,
VERTICAL,
} from '../../shared/constants';
import { ContainerSizeProp, DirectionProp } from '../../shared/props';
import { determineSizeClass } from './methods';
import { BreakpointsProp } from './props';
import SizeSubscriber from './Subscriber';
import Scrollable from './Scrollable';
const styles = StyleSheet.create({
horizontal: {
flexDirection: 'row',
alignItems: 'flex-start',
},
vertical: {
flexDirection: 'column',
alignItems: 'flex-start',
},
stretchable: {
flex: 1,
},
});
/* eslint-disable */
/**
* Grid that defines how nested `Section` and `Block` components should behave.
*
* You can determine what object is being referenced to determine size using
* `relativeTo`, as well on which sizes should size classes cascade using
* `breakpoints` and control flow of objects using `direction`.
*
* Using `relativeTo` set to 'self' can have performance impact since it must
* determine whether children components are impacted by resize.
*
* @augments {Component<{breakpoints?: Object, horizontal?: boolean, scrollable?: boolean, relativeTo?: 'window' | 'self' | 'parent', stretchable?: boolean, style?: any, children: any}>}
*/
/* eslint-enable */
class Grid extends Component {
constructor(props, context) {
super(props, context);
let width = 0;
let height = 0;
// Subscriber for components nested inside that that take grid size.
const gridComponentSizeProvider = new SizeSubscriber();
// Create subscriber used to resolve parent size dependencies in children.
let childrenReferenceSizeSubscriber;
/*
Regarding inheritance of reference size provider:
This table shows depending on current grid and parent grid what should
be provided further down the chain.
(parent -- first outer grid)
| parent | self | window |
-------|--------------------------|
parent | parent | parent | parent |
self | self | self | self |
window | null | null | null |
-------|--------------------------|
*/
if (props.relativeTo === 'window') {
// When size is not inherited from the parent, do not pass anything.
childrenReferenceSizeSubscriber = null;
// Fetch dimensions immediately and subscribe later.
({ width, height } = Dimensions.get('window'));
} else if (props.relativeTo === 'self') {
// When it is based on current element, pass on the current element.
childrenReferenceSizeSubscriber = gridComponentSizeProvider;
// Dimensions will be determined once onLayout is called.
} else if (props.relativeTo === 'parent') {
// When it is based on parent, simply pass it further down the chain.
childrenReferenceSizeSubscriber = context.referenceSizeProvider;
// When parent element is relative to window, we need to fetch dimensions
// manually just like in relativeTo='window' case, since it won't get
// re-rendered until the orientation changes so it would have the default
// values of 0, 0.
if (context.referenceSizeProvider === null) {
({ width, height } = Dimensions.get('window'));
}
// When parent element is relative to self, simply the fact that this
// component will be rendered will trigger onLayout of parent element.
}
this.state = {
gridSizeClass: this.determineSize(props.breakpoints, props.horizontal, width, height),
gridSizeProvider: gridComponentSizeProvider,
referenceSizeProvider: childrenReferenceSizeSubscriber,
};
}
getChildContext = () => ({
gridContentDirection: (this.props.horizontal ? HORIZONTAL : VERTICAL),
gridSizeClass: this.state.gridSizeClass,
gridStretch: this.props.stretchable,
gridSizeProvider: this.state.gridSizeProvider,
referenceSizeProvider: this.state.referenceSizeProvider,
});
componentDidMount() {
Dimensions.addEventListener('change', this.windowResizeHandler);
// Subscribe to parent updates if they provide them and parent provides them
if (this.props.relativeTo === 'parent') {
if (this.context.referenceSizeProvider) {
this.context.referenceSizeProvider.subscribe(this.updateSizeClass);
}
}
}
componentWillUnmount() {
Dimensions.removeEventListener('change', this.windowResizeHandler);
// On unmount we need to unsubscribe from parent subscriber.
if (this.props.relativeTo === 'parent') {
if (this.context.referenceSizeProvider) {
this.context.referenceSizeProvider.unsubscribe(this.updateSizeClass);
}
}
}
onLayoutHandler = ({ nativeEvent: { layout: { width, height } } }) => {
if (this.props.relativeTo === 'self') {
this.updateSizeClass(width, height);
}
this.updateSizeProvider(width, height);
};
/**
* Helper function that calculates all state (context) values.
*/
determineSize = (breakpoints, horizontal, width, height) => determineSizeClass(
SIZE_NAMES,
breakpoints,
(horizontal ? height : width),
);
/**
* Handler for window size changes when grid is relative to it.
*/
windowResizeHandler = ({ window: { width, height } }) => {
// Look into constructor to find more details about this implementation.
if (
(this.props.relativeTo === 'window') ||
(this.props.relativeTo === 'parent' && this.context.referenceSizeProvider === null)
) {
this.updateSizeClass(width, height);
}
};
/**
* Handler that will only update state if size really happened to avoid
* useless re-rendering.
*/
updateSizeClass = (width, height) => {
const size = this.determineSize(this.props.breakpoints, this.props.horizontal, width, height);
if (size !== this.state.gridSizeClass) {
this.setState({ gridSizeClass: size });
}
}
updateSizeProvider = (width, height) => {
// Propagate size change to subscribed entities.
this.state.gridSizeProvider.update(width, height);
}
render() {
const view = (
<View
style={[
(this.props.horizontal ? styles.horizontal : styles.vertical),
this.props.stretchable ? styles.stretchable : null,
this.props.style,
]}
onLayout={this.onLayoutHandler}
>
{this.state.gridSizeClass ? this.props.children : null}
</View>
);
if (!this.props.scrollable) {
return view;
}
return (
<Scrollable
horizontal={this.props.horizontal}
stretch={this.props.stretchable}
>
{view}
</Scrollable>
);
}
}
Grid.propTypes = {
breakpoints: BreakpointsProp,
horizontal: PropTypes.bool,
scrollable: PropTypes.bool,
relativeTo: PropTypes.oneOf(['parent', 'self', 'window']),
style: PropTypes.object,
stretchable: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]).isRequired,
};
Grid.defaultProps = {
breakpoints: BREAKPOINT_VALUES,
horizontal: false,
scrollable: false,
relativeTo: 'window',
style: {},
stretchable: false,
};
Grid.childContextTypes = {
/**
* Determines how content should flow in both Section and Block element.
* It is always based on provided direction prop.
*/
gridContentDirection: DirectionProp,
/**
* Grid will determine gridSizeClass that children will receive based
* on current grid size and merge of provided and default breakpoints.
*/
gridSizeClass: ContainerSizeProp,
/**
* Whether grid should stretch available space.
*/
gridStretch: PropTypes.bool,
/**
* Provider of width and height changes in containing grids. Components nested
* inside grid that depend on its size will subscribe to this.
*/
gridSizeProvider: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
unsubscribe: PropTypes.func.isRequired,
}),
/**
* Provider of width and height changes in grids that refer to parent.
* Grids nested inside that are relative to its parent will subscribe to this.
*/
referenceSizeProvider: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
unsubscribe: PropTypes.func.isRequired,
}),
};
Grid.contextTypes = {
referenceSizeProvider: PropTypes.shape({
subscribe: PropTypes.func.isRequired,
unsubscribe: PropTypes.func.isRequired,
}),
};
export default Grid;