-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathSlider.js
714 lines (687 loc) · 21 KB
/
Slider.js
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { chainPropTypes } from '@mui/utils';
import { generateUtilityClasses, isHostComponent } from '@mui/base';
import SliderUnstyled, {
SliderValueLabelUnstyled,
sliderUnstyledClasses,
getSliderUtilityClass,
} from '@mui/base/SliderUnstyled';
import { alpha, lighten, darken } from '@mui/system';
import useThemeProps from '../styles/useThemeProps';
import styled, { slotShouldForwardProp } from '../styles/styled';
import useTheme from '../styles/useTheme';
import capitalize from '../utils/capitalize';
export const sliderClasses = {
...sliderUnstyledClasses,
...generateUtilityClasses('MuiSlider', [
'colorPrimary',
'colorSecondary',
'thumbColorPrimary',
'thumbColorSecondary',
'sizeSmall',
'thumbSizeSmall',
]),
};
export const SliderRoot = styled('span', {
name: 'MuiSlider',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
const marks =
ownerState.marksProp === true && ownerState.step !== null
? [...Array(Math.floor((ownerState.max - ownerState.min) / ownerState.step) + 1)].map(
(_, index) => ({
value: ownerState.min + ownerState.step * index,
}),
)
: ownerState.marksProp || [];
const marked = marks.length > 0 && marks.some((mark) => mark.label);
return [
styles.root,
styles[`color${capitalize(ownerState.color)}`],
ownerState.size !== 'medium' && styles[`size${capitalize(ownerState.size)}`],
marked && styles.marked,
ownerState.orientation === 'vertical' && styles.vertical,
ownerState.track === 'inverted' && styles.trackInverted,
ownerState.track === false && styles.trackFalse,
];
},
})(({ theme, ownerState }) => ({
borderRadius: 12,
boxSizing: 'content-box',
display: 'inline-block',
position: 'relative',
cursor: 'pointer',
touchAction: 'none',
color: theme.palette[ownerState.color].main,
WebkitTapHighlightColor: 'transparent',
...(ownerState.orientation === 'horizontal' && {
height: 4,
width: '100%',
padding: '13px 0',
// The primary input mechanism of the device includes a pointing device of limited accuracy.
'@media (pointer: coarse)': {
// Reach 42px touch target, about ~8mm on screen.
padding: '20px 0',
},
...(ownerState.size === 'small' && {
height: 2,
}),
...(ownerState.marked && {
marginBottom: 20,
}),
}),
...(ownerState.orientation === 'vertical' && {
height: '100%',
width: 4,
padding: '0 13px',
// The primary input mechanism of the device includes a pointing device of limited accuracy.
'@media (pointer: coarse)': {
// Reach 42px touch target, about ~8mm on screen.
padding: '0 20px',
},
...(ownerState.size === 'small' && {
width: 2,
}),
...(ownerState.marked && {
marginRight: 44,
}),
}),
'@media print': {
colorAdjust: 'exact',
},
[`&.${sliderClasses.disabled}`]: {
pointerEvents: 'none',
cursor: 'default',
color: theme.palette.grey[400],
},
[`&.${sliderClasses.dragging}`]: {
[`& .${sliderClasses.thumb}, & .${sliderClasses.track}`]: {
transition: 'none',
},
},
}));
export const SliderRail = styled('span', {
name: 'MuiSlider',
slot: 'Rail',
overridesResolver: (props, styles) => styles.rail,
})(({ ownerState }) => ({
display: 'block',
position: 'absolute',
borderRadius: 'inherit',
backgroundColor: 'currentColor',
opacity: 0.38,
...(ownerState.orientation === 'horizontal' && {
width: '100%',
height: 'inherit',
top: '50%',
transform: 'translateY(-50%)',
}),
...(ownerState.orientation === 'vertical' && {
height: '100%',
width: 'inherit',
left: '50%',
transform: 'translateX(-50%)',
}),
...(ownerState.track === 'inverted' && {
opacity: 1,
}),
}));
export const SliderTrack = styled('span', {
name: 'MuiSlider',
slot: 'Track',
overridesResolver: (props, styles) => styles.track,
})(({ theme, ownerState }) => {
const color = // Same logic as the LinearProgress track color
theme.palette.mode === 'light'
? lighten(theme.palette[ownerState.color].main, 0.62)
: darken(theme.palette[ownerState.color].main, 0.5);
return {
display: 'block',
position: 'absolute',
borderRadius: 'inherit',
border: '1px solid currentColor',
backgroundColor: 'currentColor',
transition: theme.transitions.create(['left', 'width', 'bottom', 'height'], {
duration: theme.transitions.duration.shortest,
}),
...(ownerState.size === 'small' && {
border: 'none',
}),
...(ownerState.orientation === 'horizontal' && {
height: 'inherit',
top: '50%',
transform: 'translateY(-50%)',
}),
...(ownerState.orientation === 'vertical' && {
width: 'inherit',
left: '50%',
transform: 'translateX(-50%)',
}),
...(ownerState.track === false && {
display: 'none',
}),
...(ownerState.track === 'inverted' && {
backgroundColor: color,
borderColor: color,
}),
};
});
export const SliderThumb = styled('span', {
name: 'MuiSlider',
slot: 'Thumb',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.thumb,
styles[`thumbColor${capitalize(ownerState.color)}`],
ownerState.size !== 'medium' && styles[`thumbSize${capitalize(ownerState.size)}`],
];
},
})(({ theme, ownerState }) => ({
position: 'absolute',
width: 20,
height: 20,
boxSizing: 'border-box',
borderRadius: '50%',
outline: 0,
backgroundColor: 'currentColor',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
transition: theme.transitions.create(['box-shadow', 'left', 'bottom'], {
duration: theme.transitions.duration.shortest,
}),
...(ownerState.size === 'small' && {
width: 12,
height: 12,
}),
...(ownerState.orientation === 'horizontal' && {
top: '50%',
transform: 'translate(-50%, -50%)',
}),
...(ownerState.orientation === 'vertical' && {
left: '50%',
transform: 'translate(-50%, 50%)',
}),
'&:before': {
position: 'absolute',
content: '""',
borderRadius: 'inherit',
width: '100%',
height: '100%',
boxShadow: theme.shadows[2],
...(ownerState.size === 'small' && {
boxShadow: 'none',
}),
},
'&::after': {
position: 'absolute',
content: '""',
borderRadius: '50%',
// 42px is the hit target
width: 42,
height: 42,
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
},
[`&:hover, &.${sliderClasses.focusVisible}`]: {
boxShadow: `0px 0px 0px 8px ${alpha(theme.palette[ownerState.color].main, 0.16)}`,
'@media (hover: none)': {
boxShadow: 'none',
},
},
[`&.${sliderClasses.active}`]: {
boxShadow: `0px 0px 0px 14px ${alpha(theme.palette[ownerState.color].main, 0.16)}`,
},
[`&.${sliderClasses.disabled}`]: {
'&:hover': {
boxShadow: 'none',
},
},
}));
export const SliderValueLabel = styled(SliderValueLabelUnstyled, {
name: 'MuiSlider',
slot: 'ValueLabel',
overridesResolver: (props, styles) => styles.valueLabel,
})(({ theme, ownerState }) => ({
[`&.${sliderClasses.valueLabelOpen}`]: {
transform: 'translateY(-100%) scale(1)',
},
zIndex: 1,
whiteSpace: 'nowrap',
...theme.typography.body2,
fontWeight: 500,
transition: theme.transitions.create(['transform'], {
duration: theme.transitions.duration.shortest,
}),
top: -10,
transformOrigin: 'bottom center',
transform: 'translateY(-100%) scale(0)',
position: 'absolute',
backgroundColor: theme.palette.grey[600],
borderRadius: 2,
color: theme.palette.common.white,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
padding: '0.25rem 0.75rem',
...(ownerState.size === 'small' && {
fontSize: theme.typography.pxToRem(12),
padding: '0.25rem 0.5rem',
}),
'&:before': {
position: 'absolute',
content: '""',
width: 8,
height: 8,
bottom: 0,
left: '50%',
transform: 'translate(-50%, 50%) rotate(45deg)',
backgroundColor: 'inherit',
},
}));
export const SliderMark = styled('span', {
name: 'MuiSlider',
slot: 'Mark',
shouldForwardProp: (prop) => slotShouldForwardProp(prop) && prop !== 'markActive',
overridesResolver: (props, styles) => styles.mark,
})(({ theme, ownerState, markActive }) => ({
position: 'absolute',
width: 2,
height: 2,
borderRadius: 1,
backgroundColor: 'currentColor',
...(ownerState.orientation === 'horizontal' && {
top: '50%',
transform: 'translate(-1px, -50%)',
}),
...(ownerState.orientation === 'vertical' && {
left: '50%',
transform: 'translate(-50%, 1px)',
}),
...(markActive && {
backgroundColor: theme.palette.background.paper,
opacity: 0.8,
}),
}));
export const SliderMarkLabel = styled('span', {
name: 'MuiSlider',
slot: 'MarkLabel',
shouldForwardProp: (prop) => slotShouldForwardProp(prop) && prop !== 'markLabelActive',
overridesResolver: (props, styles) => styles.markLabel,
})(({ theme, ownerState, markLabelActive }) => ({
...theme.typography.body2,
color: theme.palette.text.secondary,
position: 'absolute',
whiteSpace: 'nowrap',
...(ownerState.orientation === 'horizontal' && {
top: 30,
transform: 'translateX(-50%)',
'@media (pointer: coarse)': {
top: 40,
},
}),
...(ownerState.orientation === 'vertical' && {
left: 36,
transform: 'translateY(50%)',
'@media (pointer: coarse)': {
left: 44,
},
}),
...(markLabelActive && {
color: theme.palette.text.primary,
}),
}));
SliderRoot.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
/**
* @ignore
*/
ownerState: PropTypes.shape({
'aria-label': PropTypes.string,
'aria-labelledby': PropTypes.string,
'aria-valuetext': PropTypes.string,
classes: PropTypes.object,
color: PropTypes.oneOf(['primary', 'secondary']),
defaultValue: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
disabled: PropTypes.bool,
getAriaLabel: PropTypes.func,
getAriaValueText: PropTypes.func,
isRtl: PropTypes.bool,
marks: PropTypes.oneOfType([
PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.node,
value: PropTypes.number.isRequired,
}),
),
PropTypes.bool,
]),
max: PropTypes.number,
min: PropTypes.number,
name: PropTypes.string,
onChange: PropTypes.func,
onChangeCommitted: PropTypes.func,
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
scale: PropTypes.func,
step: PropTypes.number,
track: PropTypes.oneOf(['inverted', 'normal', false]),
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
valueLabelDisplay: PropTypes.oneOf(['auto', 'off', 'on']),
valueLabelFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
}),
};
const extendUtilityClasses = (ownerState) => {
const { color, size, classes = {} } = ownerState;
return {
...classes,
root: clsx(
classes.root,
getSliderUtilityClass(`color${capitalize(color)}`),
classes[`color${capitalize(color)}`],
size && getSliderUtilityClass(`size${capitalize(size)}`),
size && classes[`size${capitalize(size)}`],
),
thumb: clsx(
classes.thumb,
getSliderUtilityClass(`thumbColor${capitalize(color)}`),
classes[`thumbColor${capitalize(color)}`],
size && getSliderUtilityClass(`thumbSize${capitalize(size)}`),
size && classes[`thumbSize${capitalize(size)}`],
),
};
};
const shouldSpreadOwnerState = (Component) => {
return !Component || !isHostComponent(Component);
};
const Slider = React.forwardRef(function Slider(inputProps, ref) {
const props = useThemeProps({ props: inputProps, name: 'MuiSlider' });
const theme = useTheme();
const isRtl = theme.direction === 'rtl';
const {
components = {},
componentsProps = {},
color = 'primary',
size = 'medium',
...other
} = props;
const ownerState = { ...props, color, size };
const classes = extendUtilityClasses(ownerState);
return (
<SliderUnstyled
{...other}
isRtl={isRtl}
components={{
Root: SliderRoot,
Rail: SliderRail,
Track: SliderTrack,
Thumb: SliderThumb,
ValueLabel: SliderValueLabel,
Mark: SliderMark,
MarkLabel: SliderMarkLabel,
...components,
}}
componentsProps={{
...componentsProps,
root: {
...componentsProps.root,
...(shouldSpreadOwnerState(components.Root) && {
ownerState: { ...componentsProps.root?.ownerState, color, size },
}),
},
thumb: {
...componentsProps.thumb,
...(shouldSpreadOwnerState(components.Thumb) && {
ownerState: { ...componentsProps.thumb?.ownerState, color, size },
}),
},
track: {
...componentsProps.track,
...(shouldSpreadOwnerState(components.Track) && {
ownerState: { ...componentsProps.track?.ownerState, color, size },
}),
},
valueLabel: {
...componentsProps.valueLabel,
...(shouldSpreadOwnerState(components.ValueLabel) && {
ownerState: { ...componentsProps.valueLabel?.ownerState, color, size },
}),
},
}}
classes={classes}
ref={ref}
/>
);
});
Slider.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The label of the slider.
*/
'aria-label': chainPropTypes(PropTypes.string, (props) => {
const range = Array.isArray(props.value || props.defaultValue);
if (range && props['aria-label'] != null) {
return new Error(
'MUI: You need to use the `getAriaLabel` prop instead of `aria-label` when using a range slider.',
);
}
return null;
}),
/**
* The id of the element containing a label for the slider.
*/
'aria-labelledby': PropTypes.string,
/**
* A string value that provides a user-friendly name for the current value of the slider.
*/
'aria-valuetext': chainPropTypes(PropTypes.string, (props) => {
const range = Array.isArray(props.value || props.defaultValue);
if (range && props['aria-valuetext'] != null) {
return new Error(
'MUI: You need to use the `getAriaValueText` prop instead of `aria-valuetext` when using a range slider.',
);
}
return null;
}),
/**
* @ignore
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['primary', 'secondary']),
PropTypes.string,
]),
/**
* The components used for each slot inside the Slider.
* Either a string to use a HTML element or a component.
* @default {}
*/
components: PropTypes.shape({
Mark: PropTypes.elementType,
MarkLabel: PropTypes.elementType,
Rail: PropTypes.elementType,
Root: PropTypes.elementType,
Thumb: PropTypes.elementType,
Track: PropTypes.elementType,
ValueLabel: PropTypes.elementType,
}),
/**
* The props used for each slot inside the Slider.
* @default {}
*/
componentsProps: PropTypes.object,
/**
* The default value. Use when the component is not controlled.
*/
defaultValue: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, the active thumb doesn't swap when moving pointer over a thumb while dragging another thumb.
* @default false
*/
disableSwap: PropTypes.bool,
/**
* Accepts a function which returns a string value that provides a user-friendly name for the thumb labels of the slider.
* This is important for screen reader users.
* @param {number} index The thumb label's index to format.
* @returns {string}
*/
getAriaLabel: PropTypes.func,
/**
* Accepts a function which returns a string value that provides a user-friendly name for the current value of the slider.
* This is important for screen reader users.
* @param {number} value The thumb label's value to format.
* @param {number} index The thumb label's index to format.
* @returns {string}
*/
getAriaValueText: PropTypes.func,
/**
* Indicates whether the theme context has rtl direction. It is set automatically.
* @default false
*/
isRtl: PropTypes.bool,
/**
* Marks indicate predetermined values to which the user can move the slider.
* If `true` the marks are spaced according the value of the `step` prop.
* If an array, it should contain objects with `value` and an optional `label` keys.
* @default false
*/
marks: PropTypes.oneOfType([
PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.node,
value: PropTypes.number.isRequired,
}),
),
PropTypes.bool,
]),
/**
* The maximum allowed value of the slider.
* Should not be equal to min.
* @default 100
*/
max: PropTypes.number,
/**
* The minimum allowed value of the slider.
* Should not be equal to max.
* @default 0
*/
min: PropTypes.number,
/**
* Name attribute of the hidden `input` element.
*/
name: PropTypes.string,
/**
* Callback function that is fired when the slider's value changed.
*
* @param {Event} event The event source of the callback.
* You can pull out the new value by accessing `event.target.value` (any).
* **Warning**: This is a generic event not a change event.
* @param {number | number[]} value The new value.
* @param {number} activeThumb Index of the currently moved thumb.
*/
onChange: PropTypes.func,
/**
* Callback function that is fired when the `mouseup` is triggered.
*
* @param {React.SyntheticEvent | Event} event The event source of the callback. **Warning**: This is a generic event not a change event.
* @param {number | number[]} value The new value.
*/
onChangeCommitted: PropTypes.func,
/**
* The component orientation.
* @default 'horizontal'
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* A transformation function, to change the scale of the slider.
* @default (x) => x
*/
scale: PropTypes.func,
/**
* The size of the slider.
* @default 'medium'
*/
size: PropTypes.oneOf(['small', 'medium']),
/**
* The granularity with which the slider can step through values. (A "discrete" slider.)
* The `min` prop serves as the origin for the valid values.
* We recommend (max - min) to be evenly divisible by the step.
*
* When step is `null`, the thumb can only be slid onto marks provided with the `marks` prop.
* @default 1
*/
step: PropTypes.number,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* Tab index attribute of the hidden `input` element.
*/
tabIndex: PropTypes.number,
/**
* The track presentation:
*
* - `normal` the track will render a bar representing the slider value.
* - `inverted` the track will render a bar representing the remaining slider value.
* - `false` the track will render without a bar.
* @default 'normal'
*/
track: PropTypes.oneOf(['inverted', 'normal', false]),
/**
* The value of the slider.
* For ranged sliders, provide an array with two values.
*/
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
/**
* Controls when the value label is displayed:
*
* - `auto` the value label will display when the thumb is hovered or focused.
* - `on` will display persistently.
* - `off` will never display.
* @default 'off'
*/
valueLabelDisplay: PropTypes.oneOf(['auto', 'off', 'on']),
/**
* The format function the value label's value.
*
* When a function is provided, it should have the following signature:
*
* - {number} value The value label's value to format
* - {number} index The value label's index to format
* @default (x) => x
*/
valueLabelFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
};
export default Slider;