-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Expand file tree
/
Copy pathSlider.js
More file actions
640 lines (577 loc) · 17.4 KB
/
Slider.js
File metadata and controls
640 lines (577 loc) · 17.4 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import React, {Component, PropTypes} from 'react';
import keycode from 'keycode';
import transitions from '../styles/transitions';
import FocusRipple from '../internal/FocusRipple';
/**
* Verifies min/max range.
* @param {Object} props Properties of the React component.
* @param {String} propName Name of the property to validate.
* @param {String} componentName Name of the component whose property is being validated.
* @returns {Object} Returns an Error if min >= max otherwise null.
*/
const minMaxPropType = (props, propName, componentName) => {
const error = PropTypes.number(props, propName, componentName);
if (error !== null) return error;
if (props.min >= props.max) {
const errorMsg = (propName === 'min') ? 'min should be less than max' : 'max should be greater than min';
return new Error(errorMsg);
}
};
/**
* Verifies value is within the min/max range.
* @param {Object} props Properties of the React component.
* @param {String} propName Name of the property to validate.
* @param {String} componentName Name of the component whose property is being validated.
* @returns {Object} Returns an Error if the value is not within the range otherwise null.
*/
const valueInRangePropType = (props, propName, componentName) => {
const error = PropTypes.number(props, propName, componentName);
if (error !== null) return error;
const value = props[propName];
if (value < props.min || props.max < value) {
return new Error(`${propName} should be within the range specified by min and max`);
}
};
const getStyles = (props, context, state) => {
const {slider} = context.muiTheme;
const fillGutter = slider.handleSize / 2;
const disabledGutter = slider.trackSize + slider.handleSizeDisabled / 2;
const calcDisabledSpacing = props.disabled ? ` - ${disabledGutter}px` : '';
const styles = {
slider: {
touchCallout: 'none',
userSelect: 'none',
cursor: 'default',
height: slider.handleSizeActive,
position: 'relative',
marginTop: 24,
marginBottom: 48,
},
track: {
position: 'absolute',
top: (slider.handleSizeActive - slider.trackSize) / 2,
left: 0,
width: '100%',
height: slider.trackSize,
},
filledAndRemaining: {
position: 'absolute',
top: 0,
height: '100%',
transition: transitions.easeOut(null, 'margin'),
},
handle: {
boxSizing: 'border-box',
position: 'absolute',
cursor: 'pointer',
pointerEvents: 'inherit',
top: 0,
left: state.percent === 0 ? '0%' : `${(state.percent * 100)}%`,
zIndex: 1,
margin: `${(slider.trackSize / 2)}px 0 0 0`,
width: slider.handleSize,
height: slider.handleSize,
backgroundColor: slider.selectionColor,
backgroundClip: 'padding-box',
border: '0px solid transparent',
borderRadius: '50%',
transform: 'translate(-50%, -50%)',
transition:
`${transitions.easeOut('450ms', 'background')}, ${
transitions.easeOut('450ms', 'border-color')}, ${
transitions.easeOut('450ms', 'width')}, ${
transitions.easeOut('450ms', 'height')}`,
overflow: 'visible',
outline: 'none',
},
handleWhenDisabled: {
boxSizing: 'content-box',
cursor: 'not-allowed',
backgroundColor: slider.trackColor,
width: slider.handleSizeDisabled,
height: slider.handleSizeDisabled,
border: 'none',
},
handleWhenPercentZero: {
border: `${slider.trackSize}px solid ${slider.handleColorZero}`,
backgroundColor: slider.handleFillColor,
boxShadow: 'none',
},
handleWhenPercentZeroAndDisabled: {
cursor: 'not-allowed',
width: slider.handleSizeDisabled,
height: slider.handleSizeDisabled,
},
handleWhenPercentZeroAndFocused: {
border: `${slider.trackSize}px solid ${slider.trackColorSelected}`,
},
handleWhenActive: {
width: slider.handleSizeActive,
height: slider.handleSizeActive,
},
ripple: {
height: slider.handleSize,
width: slider.handleSize,
overflow: 'visible',
},
rippleWhenPercentZero: {
top: -slider.trackSize,
left: -slider.trackSize,
},
rippleInner: {
height: '300%',
width: '300%',
top: -slider.handleSize,
left: -slider.handleSize,
},
rippleColor: {
fill: state.percent === 0 ? slider.handleColorZero : slider.rippleColor,
},
};
styles.filled = Object.assign({}, styles.filledAndRemaining, {
left: 0,
backgroundColor: (props.disabled) ? slider.trackColor : slider.selectionColor,
marginRight: fillGutter,
width: `calc(${(state.percent * 100)}%${calcDisabledSpacing})`,
});
styles.remaining = Object.assign({}, styles.filledAndRemaining, {
right: 0,
backgroundColor: (state.hovered || state.focused) &&
!props.disabled ? slider.trackColorSelected : slider.trackColor,
marginLeft: fillGutter,
width: `calc(${((1 - state.percent) * 100)}%${calcDisabledSpacing})`,
});
return styles;
};
class Slider extends Component {
static propTypes = {
/**
* The default value of the slider.
*/
defaultValue: valueInRangePropType,
/**
* Describe the slider.
*/
description: PropTypes.string,
/**
* Disables focus ripple if set to true.
*/
disableFocusRipple: PropTypes.bool,
/**
* If true, the slider will not be interactable.
*/
disabled: PropTypes.bool,
/**
* An error message for the slider.
*/
error: PropTypes.string,
/**
* The maximum value the slider can slide to on
* a scale from 0 to 1 inclusive. Cannot be equal to min.
*/
max: minMaxPropType,
/**
* The minimum value the slider can slide to on a scale
* from 0 to 1 inclusive. Cannot be equal to max.
*/
min: minMaxPropType,
/**
* The name of the slider. Behaves like the name attribute
* of an input element.
*/
name: PropTypes.string,
/** @ignore */
onBlur: PropTypes.func,
/**
* Callback function that is fired when the user changes the slider's value.
*/
onChange: PropTypes.func,
/**
* Callback function that is fired when the slider has begun to move.
*/
onDragStart: PropTypes.func,
/**
* Callback function that is fried when the slide has stopped moving.
*/
onDragStop: PropTypes.func,
/** @ignore */
onFocus: PropTypes.func,
/**
* Whether or not the slider is required in a form.
*/
required: PropTypes.bool,
/**
* The granularity the slider can step through values.
*/
step: PropTypes.number,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* The value of the slider.
*/
value: valueInRangePropType,
};
static defaultProps = {
disabled: false,
disableFocusRipple: false,
max: 1,
min: 0,
required: true,
step: 0.01,
style: {},
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
active: false,
dragging: false,
focused: false,
hovered: false,
percent: 0,
value: 0,
};
componentWillMount() {
let value = this.props.value;
if (value === undefined) {
value = this.props.defaultValue !== undefined ? this.props.defaultValue : this.props.min;
}
let percent = (value - this.props.min) / (this.props.max - this.props.min);
if (isNaN(percent)) percent = 0;
this.setState({
percent: percent,
value: value,
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.value !== undefined && !this.state.dragging) {
this.setValue(nextProps.value);
}
}
onHandleTouchStart = (event) => {
if (document) {
document.addEventListener('touchmove', this.dragTouchHandler, false);
document.addEventListener('touchup', this.dragTouchEndHandler, false);
document.addEventListener('touchend', this.dragTouchEndHandler, false);
document.addEventListener('touchcancel', this.dragTouchEndHandler, false);
}
this.onDragStart(event);
// Cancel scroll and context menu
event.preventDefault();
};
onHandleMouseDown = (event) => {
if (document) {
document.addEventListener('mousemove', this.dragHandler, false);
document.addEventListener('mouseup', this.dragEndHandler, false);
// Cancel text selection
event.preventDefault();
// Set focus manually since we called preventDefault()
this.refs.handle.focus();
}
this.onDragStart(event);
};
onHandleKeyDown = (event) => {
const {min, max, step} = this.props;
let action;
switch (keycode(event)) {
case 'page down':
case 'left':
case 'down':
action = 'decrease';
break;
case 'page up':
case 'right':
case 'up':
action = 'increase';
break;
case 'home':
action = 'home';
break;
case 'end':
action = 'end';
break;
}
if (action) {
let newValue;
let newPercent;
// Cancel scroll
event.preventDefault();
// When pressing home or end the handle should be taken to the
// beginning or end of the track respectively
switch (action) {
case 'decrease':
newValue = Math.max(min, this.state.value - step);
newPercent = (newValue - min) / (max - min);
break;
case 'increase':
newValue = Math.min(max, this.state.value + step);
newPercent = (newValue - min) / (max - min);
break;
case 'home':
newValue = min;
newPercent = 0;
break;
case 'end':
newValue = max;
newPercent = 1;
break;
}
// We need to use toFixed() because of float point errors.
// For example, 0.01 + 0.06 = 0.06999999999999999
if (this.state.value !== newValue) {
this.setState({
percent: newPercent,
value: parseFloat(newValue.toFixed(5)),
}, () => {
if (this.props.onChange) this.props.onChange(event, this.state.value);
});
}
}
};
dragHandler = (event) => {
if (this.dragRunning) {
return;
}
this.dragRunning = true;
requestAnimationFrame(() => {
this.onDragUpdate(event, event.clientX - this.getTrackLeft());
this.dragRunning = false;
});
};
dragTouchHandler = (event) => {
if (this.dragRunning) {
return;
}
this.dragRunning = true;
requestAnimationFrame(() => {
this.onDragUpdate(event, event.touches[0].clientX - this.getTrackLeft());
this.dragRunning = false;
});
};
dragEndHandler = (event) => {
if (document) {
document.removeEventListener('mousemove', this.dragHandler, false);
document.removeEventListener('mouseup', this.dragEndHandler, false);
}
this.onDragStop(event);
};
dragTouchEndHandler = (event) => {
if (document) {
document.removeEventListener('touchmove', this.dragTouchHandler, false);
document.removeEventListener('touchup', this.dragTouchEndHandler, false);
document.removeEventListener('touchend', this.dragTouchEndHandler, false);
document.removeEventListener('touchcancel', this.dragTouchEndHandler, false);
}
this.onDragStop(event);
};
getValue() {
return this.state.value;
}
setValue(i) {
// calculate percentage
let percent = (i - this.props.min) / (this.props.max - this.props.min);
if (isNaN(percent)) percent = 0;
// update state
this.setState({
value: i,
percent: percent,
});
}
getPercent() {
return this.state.percent;
}
setPercent(percent, callback) {
const value = this.alignValue(this.percentToValue(percent));
const {min, max} = this.props;
const alignedPercent = (value - min) / (max - min);
if (this.state.value !== value) {
this.setState({value: value, percent: alignedPercent}, callback);
}
}
clearValue() {
this.setValue(this.props.min);
}
alignValue(val) {
const {step, min} = this.props;
const alignValue = Math.round((val - min) / step) * step + min;
return parseFloat(alignValue.toFixed(5));
}
handleTouchStart = (event) => {
if (!this.props.disabled && !this.state.dragging) {
const pos = event.touches[0].clientX - this.getTrackLeft();
this.dragX(event, pos);
// Since the touch event fired for the track and handle is child of
// track, we need to manually propagate the event to the handle.
this.onHandleTouchStart(event);
}
};
handleFocus = (event) => {
this.setState({focused: true});
if (this.props.onFocus) this.props.onFocus(event);
};
handleBlur = (event) => {
this.setState({focused: false, active: false});
if (this.props.onBlur) this.props.onBlur(event);
};
handleMouseDown = (event) => {
if (!this.props.disabled && !this.state.dragging) {
const pos = event.clientX - this.getTrackLeft();
this.dragX(event, pos);
// Since the click event fired for the track and handle is child of
// track, we need to manually propagate the event to the handle.
this.onHandleMouseDown(event);
}
};
handleMouseUp = () => {
if (!this.props.disabled) this.setState({active: false});
};
handleMouseEnter = () => {
this.setState({hovered: true});
};
handleMouseLeave = () => {
this.setState({hovered: false});
};
getTrackLeft() {
return this.refs.track.getBoundingClientRect().left;
}
onDragStart(event) {
this.setState({
dragging: true,
active: true,
});
if (this.props.onDragStart) this.props.onDragStart(event);
}
onDragStop(event) {
this.setState({
dragging: false,
active: false,
});
if (this.props.onDragStop) this.props.onDragStop(event);
}
onDragUpdate(event, pos) {
if (!this.state.dragging) return;
if (!this.props.disabled) this.dragX(event, pos);
}
dragX(event, pos) {
const max = this.refs.track.clientWidth;
if (pos < 0) pos = 0; else if (pos > max) pos = max;
this.updateWithChangeEvent(event, pos / max);
}
updateWithChangeEvent(event, percent) {
this.setPercent(percent, () => {
if (this.props.onChange) this.props.onChange(event, this.state.value);
});
}
percentToValue(percent) {
return percent * (this.props.max - this.props.min) + this.props.min;
}
render() {
const {
description,
disabled,
disableFocusRipple,
error,
max,
min,
name,
required,
step,
style,
...other,
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context, this.state);
const sliderStyles = styles.slider;
let handleStyles = {};
let percent = this.state.percent;
if (percent > 1) percent = 1; else if (percent < 0) percent = 0;
if (percent === 0) {
handleStyles = Object.assign(
{},
styles.handle,
styles.handleWhenPercentZero,
this.state.active && styles.handleWhenActive,
(this.state.hovered || this.state.focused) && !disabled &&
styles.handleWhenPercentZeroAndFocused,
disabled && styles.handleWhenPercentZeroAndDisabled
);
} else {
handleStyles = Object.assign(
{},
styles.handle,
this.state.active && styles.handleWhenActive,
disabled && styles.handleWhenDisabled
);
}
const rippleStyle = Object.assign(
{},
styles.ripple,
percent === 0 && styles.rippleWhenPercentZero
);
const rippleShowCondition = (this.state.hovered || this.state.focused) && !this.state.active;
let focusRipple;
if (!disabled && !disableFocusRipple) {
focusRipple = (
<FocusRipple
ref="focusRipple"
key="focusRipple"
style={rippleStyle}
innerStyle={styles.rippleInner}
show={rippleShowCondition}
muiTheme={this.context.muiTheme}
color={styles.rippleColor.fill}
/>
);
}
let handleDragProps;
if (!disabled) {
handleDragProps = {
onTouchStart: this.onHandleTouchStart,
onMouseDown: this.onHandleMouseDown,
onKeyDown: this.onHandleKeyDown,
};
}
return (
<div {...other} style={prepareStyles(Object.assign({}, style))}>
<span>{description}</span>
<span>{error}</span>
<div
style={prepareStyles(sliderStyles)}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onMouseDown={this.handleMouseDown}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
onMouseUp={this.handleMouseUp}
onTouchStart={this.handleTouchStart}
>
<div ref="track" style={prepareStyles(styles.track)}>
<div style={prepareStyles(styles.filled)}></div>
<div style={prepareStyles(styles.remaining)}></div>
<div
ref="handle"
style={prepareStyles(handleStyles)}
tabIndex={0}
{...handleDragProps}
>
{focusRipple}
</div>
</div>
</div>
<input
ref="input"
type="hidden"
name={name}
value={this.state.value}
required={required}
min={min}
max={max}
step={step}
/>
</div>
);
}
}
export default Slider;