| 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470 |
120x
120x
120x
120x
120x
120x
36x
53x
53x
53x
53x
53x
5x
5x
5x
5x
48x
48x
48x
48x
53x
4x
4x
4x
4x
11x
11x
11x
9x
9x
9x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
9x
9x
9x
9x
9x
9x
9x
9x
4x
4x
9x
7x
2x
7x
7x
7x
84x
108x
36x
42x
42x
42x
42x
42x
5x
5x
37x
37x
42x
42x
2x
28x
| import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, filterTypes, omitProps } from '../../util/component-types';
import DragCaptureZone from '../DragCaptureZone/DragCaptureZone';
import { Motion, spring } from 'react-motion';
import { QUICK_SLIDE_MOTION } from '../../constants/motion-spring';
const cx = lucidClassNames.bind('&-SplitVertical');
const {
any,
bool,
func,
node,
number,
string,
oneOfType,
} = React.PropTypes;
/**
* {"categories": ["helpers"]}
*
* `SplitVertical` renders a vertical split.
*/
const SplitVertical = createClass({
displayName: 'SplitVertical',
_isPrivate: true,
propTypes: {
/**
* Appended to the component-specific class names set on the root
* element. Value is run through the `classnames` library.
*/
className: any,
/**
* Direct children must be types {Splitvertical.Leftpane, Splitvertical.Divider, Splitvertical.RightPane}.
* All content is composed as children of these respective elements.
*/
children: node,
/**
* Allows draggable resizing of the SplitVertical
*/
isResizeable: bool,
/**
* Render as expanded or collapsed.
*/
isExpanded: bool,
/**
* Allows animated expand and collapse behavior.
*/
isAnimated: bool,
/**
* Called when the user is currently resizing the split with the Divider.
*
* Signature: `(width, { event, props }) => {}`
*/
onResizing: func,
/**
* Called when the user resizes the split with the Divider.
*
* Signature: `(width, { event, props }) => {}`
*/
onResize: func,
/**
* Use this prop to shift the collapsed position by a known value.
*/
collapseShift: number,
},
components: {
/**
* Left pane of the split.
*/
LeftPane: createClass({
displayName: 'SplitVertical.LeftPane',
propTypes: {
/**
* Any valid React children.
*/
children: node,
/**
* Set width of this pane.
*/
width: oneOfType([number, string]),
/**
* Define this pane as the primary content pane. When the split is collapsed, this pane becomes full width.
*/
isPrimary: bool,
},
getDefaultProps(){
return {
isPrimary: false,
};
},
}),
/**
* Right pane of the split.
*/
RightPane: createClass({
displayName: 'SplitVertical.RightPane',
propTypes: {
/**
* Any valid React children.
*/
children: node,
/**
* Set width of this pane.
*/
width: oneOfType([number, string]),
/**
* Define this pane as the primary content pane. When the split is collapsed, this pane becomes full width.
*/
isPrimary: bool,
},
getDefaultProps(){
return {
isPrimary: false,
};
},
}),
/**
* The area that separates the split panes. Can be dragged to resize them.
*/
Divider: createClass({
displayName: 'SplitVertical.Divider',
propTypes: {
/**
* Any valid React children.
*/
children: node,
},
}),
},
getDefaultProps() {
return {
isExpanded: true,
isAnimated: false,
collapseShift: 0,
onResizing: _.noop,
onResize: _.noop,
isResizeable: true,
};
},
getInitialState() {
return {
isAnimated: false, // to ensure first render doesn't show a collapse animation
isExpanded: true,
collapseAmount: 250,
};
},
getPanes() {
const { children } = this.props;
const {
leftPane: leftPaneRef,
rightPane: rightPaneRef,
} = this.storedRefs;
const leftPaneElement = _.get(filterTypes(children, SplitVertical.LeftPane), 0, <SplitVertical.LeftPane />)
const rightPaneElement = _.get(filterTypes(children, SplitVertical.RightPane), 0, <SplitVertical.RightPane />)
let primaryElement, primaryRef;
let secondaryElement, secondaryRef;
if (leftPaneElement.props.isPrimary && !rightPaneElement.props.isPrimary) {
primaryElement = leftPaneElement;
primaryRef = leftPaneRef;
secondaryElement = rightPaneElement;
secondaryRef = rightPaneRef;
} else {
primaryElement = rightPaneElement;
primaryRef = rightPaneRef;
secondaryElement = leftPaneElement;
secondaryRef = leftPaneRef;
}
return {
left: leftPaneElement.props,
right: rightPaneElement.props,
primary: primaryElement.props,
primaryRef,
secondary: secondaryElement.props,
secondaryRef,
};
},
// Style changes to DOM nodes are updated here to shortcut the state -> render cycle for better performance. Also the Style updates in this
// function are entirely transient and can be flushed with a props update to `width`.
applyDeltaToSecondaryWidth(dX, isExpanded, secondaryStartRect, secondaryRef, secondary, right, innerRef, primaryRef, collapseShift=0) {
Eif (isExpanded) {
secondaryRef.style.flexBasis = `${secondaryStartRect.width + dX * (secondary === right ? -1 : 1)}px`;
return (secondaryStartRect.width + dX * (secondary === right ? -1 : 1));
} else {
const overlapWidth = (secondary === right ? secondaryStartRect.width + dX : secondaryStartRect.width - dX) - collapseShift;
if (overlapWidth > 0) {
this.collapseSecondary(overlapWidth);
return (secondaryStartRect.width - overlapWidth);
} else {
this.expandSecondary();
secondaryRef.style.flexBasis = `${(dX + collapseShift) * (secondary === right ? -1 : 1)}px`;
return ((dX + collapseShift) * (secondary === right ? -1 : 1));
}
}
},
expandSecondary() {
this.setState({ isExpanded: true });
},
collapseSecondary(collapseAmount) {
this.setState({ isExpanded: false, collapseAmount });
},
disableAnimation(innerRef, secondaryRef, primaryRef) {
innerRef.style.transition = 'all 0s';
secondaryRef.style.transition = 'all 0s';
primaryRef.style.transition = 'all 0s';
},
resetAnimation(innerRef, secondaryRef, primaryRef) {
innerRef.style.transition = '';
secondaryRef.style.transition = '';
primaryRef.style.transition = '';
},
handleDragStart() {
this.panes = this.getPanes();
const { secondaryRef, primaryRef } = this.panes;
this.secondaryStartRect = secondaryRef.getBoundingClientRect();
this.disableAnimation(this.storedRefs.inner, secondaryRef, primaryRef);
},
handleDrag({ dX }, { event }) {
const {
isExpanded,
collapseShift,
onResizing,
} = this.props;
const {
secondaryRef,
secondary,
right,
primaryRef,
} = this.panes;
onResizing(
this.applyDeltaToSecondaryWidth(dX, isExpanded, this.secondaryStartRect, secondaryRef, secondary, right, this.storedRefs.inner, primaryRef, collapseShift),
{ props: this.props, event }
);
},
handleDragEnd({ dX }, { event }) {
const {
isExpanded,
collapseShift,
onResize,
} = this.props;
const {
secondaryRef,
secondary,
right,
primaryRef,
} = this.panes;
onResize(
this.applyDeltaToSecondaryWidth(dX, isExpanded, this.secondaryStartRect, secondaryRef, secondary, right, this.storedRefs.inner, primaryRef, collapseShift),
{ props: this.props, event }
);
this.resetAnimation(this.storedRefs.inner, secondaryRef, primaryRef);
},
componentWillReceiveProps(nextProps) {
const {
isAnimated,
isExpanded,
collapseShift,
} = nextProps;
const {
secondaryRef,
} = this.getPanes();
if (this.props.isExpanded && !isExpanded) { // collapse secondary
const secondaryRect = secondaryRef.getBoundingClientRect();
this.collapseSecondary(secondaryRect.width - collapseShift);
} else if (!this.props.isExpanded && isExpanded) { // expand secondary
this.expandSecondary();
}
if (this.state.isAnimated !== isAnimated) {
this.setState({
isAnimated,
});
}
},
componentDidMount() {
const {
isExpanded,
isAnimated,
collapseShift,
} = this.props;
const {
primaryRef,
secondaryRef,
} = this.getPanes();
const {
inner,
} = this.storedRefs;
_.defer(() => {
this.disableAnimation(inner, secondaryRef, primaryRef);
inner.style.visibility = 'hidden';
_.defer(() => {
if (!isExpanded) { // collapse secondary
const secondaryRect = secondaryRef.getBoundingClientRect();
this.collapseSecondary(secondaryRect.width - collapseShift);
}
_.defer(() => {
if (isAnimated) {
this.setState({ isAnimated });
}
_.defer(() => {
inner.style.visibility = '';
this.resetAnimation(inner, secondaryRef, primaryRef);
});
});
});
});
},
storeRef(name) {
return (ref) => {
this.storedRefs[name] = ref;
};
},
componentWillMount() {
this.storedRefs = {};
},
render() {
const {
children,
className,
isResizeable,
...passThroughs,
} = this.props;
const {
isAnimated,
isExpanded,
collapseAmount,
} = this.state;
const {
left: leftPaneProps,
right: rightPaneProps,
secondary,
} = this.getPanes();
const dividerProps = _.get(_.first(filterTypes(children, SplitVertical.Divider)), 'props', {});
let from, to;
if (!isExpanded) {
from = { slideAmount: 0 };
to = { slideAmount: collapseAmount };
} else {
from = { slideAmount: 0 };
to = { slideAmount: 0 };
}
const isRightSecondary = rightPaneProps === secondary;
return (
<div
{...omitProps(passThroughs, SplitVertical)}
className={cx('&', {
'&-is-expanded': isExpanded,
'&-is-animated': isAnimated,
}, className)}
style={{
overflow: 'hidden',
...passThroughs.style,
}}
>
<Motion defaultStyle={from} style={isAnimated ? _.mapValues(to, (val) => (spring(val, QUICK_SLIDE_MOTION))) : to}>
{(tween) => (
<div
className={cx('&-inner')}
ref={this.storeRef('inner')}
style={{
display: 'flex',
transform: `translateX(${(isRightSecondary ? 1 : -1) * Math.round(tween.slideAmount)}px)`,
}}
>
<div
{...omitProps(leftPaneProps, SplitVertical.LeftPane)}
className={cx('&-LeftPane', {
'&-is-secondary': leftPaneProps === secondary,
}, leftPaneProps.className)}
style={{
flexGrow: (isRightSecondary ? 1 : 0),
flexShrink: (isRightSecondary ? 1 : 0),
flexBasis: _.isNil(leftPaneProps.width) ? (leftPaneProps === secondary ? 'calc(50% - 3px)' : '0%') : leftPaneProps.width,
marginLeft: (isRightSecondary ? -Math.round(tween.slideAmount) : null),
overflow: 'auto',
...leftPaneProps.style,
}}
ref={this.storeRef('leftPane')}
>{leftPaneProps.children}</div>
{isResizeable ? (
<DragCaptureZone
{...omitProps(dividerProps, SplitVertical.Divider)}
className={cx('&-Divider', '&-Divider-is-resizeable', dividerProps.className)}
onDragStart={this.handleDragStart}
onDrag={this.handleDrag}
onDragEnd={this.handleDragEnd}
style={{
width: '6px',
boxSizing: 'border-box',
...dividerProps.style,
}}
>{dividerProps.children || ' '}</DragCaptureZone>
) : (
<div
{...omitProps(dividerProps, SplitVertical.Divider)}
className={cx('&-Divider', dividerProps.className)}
>
{dividerProps.children || ' '}
</div>
)}
<div
{...omitProps(rightPaneProps, SplitVertical.RightPane)}
className={cx('&-RightPane', {
'&-is-secondary': rightPaneProps === secondary,
}, rightPaneProps.className)}
style={{
flexGrow: (!isRightSecondary ? 1 : 0),
flexShrink: (!isRightSecondary ? 1 : 0),
flexBasis: _.isNil(rightPaneProps.width) ? (rightPaneProps === secondary ? 'calc(50% - 3px)' : '0%') : rightPaneProps.width,
marginRight: (isRightSecondary ? null : -Math.round(tween.slideAmount)),
overflow: 'auto',
...rightPaneProps.style,
}}
ref={this.storeRef('rightPane')}
>{rightPaneProps.children}</div>
</div>
)}
</Motion>
</div>
);
},
});
export default SplitVertical;
|