UNPKG

33.9 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
14var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
16var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
17
18var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
19
20var React = _interopRequireWildcard(require("react"));
21
22var _propTypes = _interopRequireDefault(require("prop-types"));
23
24var _clsx = _interopRequireDefault(require("clsx"));
25
26var _utils = require("@material-ui/utils");
27
28var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
29
30var _useTheme = _interopRequireDefault(require("../styles/useTheme"));
31
32var _colorManipulator = require("../styles/colorManipulator");
33
34var _useIsFocusVisible2 = _interopRequireDefault(require("../utils/useIsFocusVisible"));
35
36var _ownerDocument = _interopRequireDefault(require("../utils/ownerDocument"));
37
38var _useEventCallback = _interopRequireDefault(require("../utils/useEventCallback"));
39
40var _useForkRef = _interopRequireDefault(require("../utils/useForkRef"));
41
42var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
43
44var _useControlled3 = _interopRequireDefault(require("../utils/useControlled"));
45
46var _ValueLabel = _interopRequireDefault(require("./ValueLabel"));
47
48function asc(a, b) {
49 return a - b;
50}
51
52function clamp(value, min, max) {
53 return Math.min(Math.max(min, value), max);
54}
55
56function findClosest(values, currentValue) {
57 var _values$reduce = values.reduce(function (acc, value, index) {
58 var distance = Math.abs(currentValue - value);
59
60 if (acc === null || distance < acc.distance || distance === acc.distance) {
61 return {
62 distance: distance,
63 index: index
64 };
65 }
66
67 return acc;
68 }, null),
69 closestIndex = _values$reduce.index;
70
71 return closestIndex;
72}
73
74function trackFinger(event, touchId) {
75 if (touchId.current !== undefined && event.changedTouches) {
76 for (var i = 0; i < event.changedTouches.length; i += 1) {
77 var touch = event.changedTouches[i];
78
79 if (touch.identifier === touchId.current) {
80 return {
81 x: touch.clientX,
82 y: touch.clientY
83 };
84 }
85 }
86
87 return false;
88 }
89
90 return {
91 x: event.clientX,
92 y: event.clientY
93 };
94}
95
96function valueToPercent(value, min, max) {
97 return (value - min) * 100 / (max - min);
98}
99
100function percentToValue(percent, min, max) {
101 return (max - min) * percent + min;
102}
103
104function getDecimalPrecision(num) {
105 // This handles the case when num is very small (0.00000001), js will turn this into 1e-8.
106 // When num is bigger than 1 or less than -1 it won't get converted to this notation so it's fine.
107 if (Math.abs(num) < 1) {
108 var parts = num.toExponential().split('e-');
109 var matissaDecimalPart = parts[0].split('.')[1];
110 return (matissaDecimalPart ? matissaDecimalPart.length : 0) + parseInt(parts[1], 10);
111 }
112
113 var decimalPart = num.toString().split('.')[1];
114 return decimalPart ? decimalPart.length : 0;
115}
116
117function roundValueToStep(value, step, min) {
118 var nearest = Math.round((value - min) / step) * step + min;
119 return Number(nearest.toFixed(getDecimalPrecision(step)));
120}
121
122function setValueIndex(_ref) {
123 var values = _ref.values,
124 source = _ref.source,
125 newValue = _ref.newValue,
126 index = _ref.index;
127
128 // Performance shortcut
129 if (values[index] === newValue) {
130 return source;
131 }
132
133 var output = values.slice();
134 output[index] = newValue;
135 return output;
136}
137
138function focusThumb(_ref2) {
139 var sliderRef = _ref2.sliderRef,
140 activeIndex = _ref2.activeIndex,
141 setActive = _ref2.setActive;
142
143 if (!sliderRef.current.contains(document.activeElement) || Number(document.activeElement.getAttribute('data-index')) !== activeIndex) {
144 sliderRef.current.querySelector("[role=\"slider\"][data-index=\"".concat(activeIndex, "\"]")).focus();
145 }
146
147 if (setActive) {
148 setActive(activeIndex);
149 }
150}
151
152var axisProps = {
153 horizontal: {
154 offset: function offset(percent) {
155 return {
156 left: "".concat(percent, "%")
157 };
158 },
159 leap: function leap(percent) {
160 return {
161 width: "".concat(percent, "%")
162 };
163 }
164 },
165 'horizontal-reverse': {
166 offset: function offset(percent) {
167 return {
168 right: "".concat(percent, "%")
169 };
170 },
171 leap: function leap(percent) {
172 return {
173 width: "".concat(percent, "%")
174 };
175 }
176 },
177 vertical: {
178 offset: function offset(percent) {
179 return {
180 bottom: "".concat(percent, "%")
181 };
182 },
183 leap: function leap(percent) {
184 return {
185 height: "".concat(percent, "%")
186 };
187 }
188 }
189};
190
191var Identity = function Identity(x) {
192 return x;
193};
194
195var styles = function styles(theme) {
196 return {
197 /* Styles applied to the root element. */
198 root: {
199 height: 2,
200 width: '100%',
201 boxSizing: 'content-box',
202 padding: '13px 0',
203 display: 'inline-block',
204 position: 'relative',
205 cursor: 'pointer',
206 touchAction: 'none',
207 color: theme.palette.primary.main,
208 WebkitTapHighlightColor: 'transparent',
209 '&$disabled': {
210 pointerEvents: 'none',
211 cursor: 'default',
212 color: theme.palette.grey[400]
213 },
214 '&$vertical': {
215 width: 2,
216 height: '100%',
217 padding: '0 13px'
218 },
219 // The primary input mechanism of the device includes a pointing device of limited accuracy.
220 '@media (pointer: coarse)': {
221 // Reach 42px touch target, about ~8mm on screen.
222 padding: '20px 0',
223 '&$vertical': {
224 padding: '0 20px'
225 }
226 },
227 '@media print': {
228 colorAdjust: 'exact'
229 }
230 },
231
232 /* Styles applied to the root element if `color="primary"`. */
233 colorPrimary: {// TODO v5: move the style here
234 },
235
236 /* Styles applied to the root element if `color="secondary"`. */
237 colorSecondary: {
238 color: theme.palette.secondary.main
239 },
240
241 /* Styles applied to the root element if `marks` is provided with at least one label. */
242 marked: {
243 marginBottom: 20,
244 '&$vertical': {
245 marginBottom: 'auto',
246 marginRight: 20
247 }
248 },
249
250 /* Pseudo-class applied to the root element if `orientation="vertical"`. */
251 vertical: {},
252
253 /* Pseudo-class applied to the root and thumb element if `disabled={true}`. */
254 disabled: {},
255
256 /* Styles applied to the rail element. */
257 rail: {
258 display: 'block',
259 position: 'absolute',
260 width: '100%',
261 height: 2,
262 borderRadius: 1,
263 backgroundColor: 'currentColor',
264 opacity: 0.38,
265 '$vertical &': {
266 height: '100%',
267 width: 2
268 }
269 },
270
271 /* Styles applied to the track element. */
272 track: {
273 display: 'block',
274 position: 'absolute',
275 height: 2,
276 borderRadius: 1,
277 backgroundColor: 'currentColor',
278 '$vertical &': {
279 width: 2
280 }
281 },
282
283 /* Styles applied to the track element if `track={false}`. */
284 trackFalse: {
285 '& $track': {
286 display: 'none'
287 }
288 },
289
290 /* Styles applied to the track element if `track="inverted"`. */
291 trackInverted: {
292 '& $track': {
293 backgroundColor: // Same logic as the LinearProgress track color
294 theme.palette.type === 'light' ? (0, _colorManipulator.lighten)(theme.palette.primary.main, 0.62) : (0, _colorManipulator.darken)(theme.palette.primary.main, 0.5)
295 },
296 '& $rail': {
297 opacity: 1
298 }
299 },
300
301 /* Styles applied to the thumb element. */
302 thumb: {
303 position: 'absolute',
304 width: 12,
305 height: 12,
306 marginLeft: -6,
307 marginTop: -5,
308 boxSizing: 'border-box',
309 borderRadius: '50%',
310 outline: 0,
311 backgroundColor: 'currentColor',
312 display: 'flex',
313 alignItems: 'center',
314 justifyContent: 'center',
315 transition: theme.transitions.create(['box-shadow'], {
316 duration: theme.transitions.duration.shortest
317 }),
318 '&::after': {
319 position: 'absolute',
320 content: '""',
321 borderRadius: '50%',
322 // reach 42px hit target (2 * 15 + thumb diameter)
323 left: -15,
324 top: -15,
325 right: -15,
326 bottom: -15
327 },
328 '&$focusVisible,&:hover': {
329 boxShadow: "0px 0px 0px 8px ".concat((0, _colorManipulator.alpha)(theme.palette.primary.main, 0.16)),
330 '@media (hover: none)': {
331 boxShadow: 'none'
332 }
333 },
334 '&$active': {
335 boxShadow: "0px 0px 0px 14px ".concat((0, _colorManipulator.alpha)(theme.palette.primary.main, 0.16))
336 },
337 '&$disabled': {
338 width: 8,
339 height: 8,
340 marginLeft: -4,
341 marginTop: -3,
342 '&:hover': {
343 boxShadow: 'none'
344 }
345 },
346 '$vertical &': {
347 marginLeft: -5,
348 marginBottom: -6
349 },
350 '$vertical &$disabled': {
351 marginLeft: -3,
352 marginBottom: -4
353 }
354 },
355
356 /* Styles applied to the thumb element if `color="primary"`. */
357 thumbColorPrimary: {// TODO v5: move the style here
358 },
359
360 /* Styles applied to the thumb element if `color="secondary"`. */
361 thumbColorSecondary: {
362 '&$focusVisible,&:hover': {
363 boxShadow: "0px 0px 0px 8px ".concat((0, _colorManipulator.alpha)(theme.palette.secondary.main, 0.16))
364 },
365 '&$active': {
366 boxShadow: "0px 0px 0px 14px ".concat((0, _colorManipulator.alpha)(theme.palette.secondary.main, 0.16))
367 }
368 },
369
370 /* Pseudo-class applied to the thumb element if it's active. */
371 active: {},
372
373 /* Pseudo-class applied to the thumb element if keyboard focused. */
374 focusVisible: {},
375
376 /* Styles applied to the thumb label element. */
377 valueLabel: {
378 // IE 11 centering bug, to remove from the customization demos once no longer supported
379 left: 'calc(-50% - 4px)'
380 },
381
382 /* Styles applied to the mark element. */
383 mark: {
384 position: 'absolute',
385 width: 2,
386 height: 2,
387 borderRadius: 1,
388 backgroundColor: 'currentColor'
389 },
390
391 /* Styles applied to the mark element if active (depending on the value). */
392 markActive: {
393 backgroundColor: theme.palette.background.paper,
394 opacity: 0.8
395 },
396
397 /* Styles applied to the mark label element. */
398 markLabel: (0, _extends2.default)({}, theme.typography.body2, {
399 color: theme.palette.text.secondary,
400 position: 'absolute',
401 top: 26,
402 transform: 'translateX(-50%)',
403 whiteSpace: 'nowrap',
404 '$vertical &': {
405 top: 'auto',
406 left: 26,
407 transform: 'translateY(50%)'
408 },
409 '@media (pointer: coarse)': {
410 top: 40,
411 '$vertical &': {
412 left: 31
413 }
414 }
415 }),
416
417 /* Styles applied to the mark label element if active (depending on the value). */
418 markLabelActive: {
419 color: theme.palette.text.primary
420 }
421 };
422};
423
424exports.styles = styles;
425var Slider = /*#__PURE__*/React.forwardRef(function Slider(props, ref) {
426 var ariaLabel = props['aria-label'],
427 ariaLabelledby = props['aria-labelledby'],
428 ariaValuetext = props['aria-valuetext'],
429 classes = props.classes,
430 className = props.className,
431 _props$color = props.color,
432 color = _props$color === void 0 ? 'primary' : _props$color,
433 _props$component = props.component,
434 Component = _props$component === void 0 ? 'span' : _props$component,
435 defaultValue = props.defaultValue,
436 _props$disabled = props.disabled,
437 disabled = _props$disabled === void 0 ? false : _props$disabled,
438 getAriaLabel = props.getAriaLabel,
439 getAriaValueText = props.getAriaValueText,
440 _props$marks = props.marks,
441 marksProp = _props$marks === void 0 ? false : _props$marks,
442 _props$max = props.max,
443 max = _props$max === void 0 ? 100 : _props$max,
444 _props$min = props.min,
445 min = _props$min === void 0 ? 0 : _props$min,
446 name = props.name,
447 onChange = props.onChange,
448 onChangeCommitted = props.onChangeCommitted,
449 onMouseDown = props.onMouseDown,
450 _props$orientation = props.orientation,
451 orientation = _props$orientation === void 0 ? 'horizontal' : _props$orientation,
452 _props$scale = props.scale,
453 scale = _props$scale === void 0 ? Identity : _props$scale,
454 _props$step = props.step,
455 step = _props$step === void 0 ? 1 : _props$step,
456 _props$ThumbComponent = props.ThumbComponent,
457 ThumbComponent = _props$ThumbComponent === void 0 ? 'span' : _props$ThumbComponent,
458 _props$track = props.track,
459 track = _props$track === void 0 ? 'normal' : _props$track,
460 valueProp = props.value,
461 _props$ValueLabelComp = props.ValueLabelComponent,
462 ValueLabelComponent = _props$ValueLabelComp === void 0 ? _ValueLabel.default : _props$ValueLabelComp,
463 _props$valueLabelDisp = props.valueLabelDisplay,
464 valueLabelDisplay = _props$valueLabelDisp === void 0 ? 'off' : _props$valueLabelDisp,
465 _props$valueLabelForm = props.valueLabelFormat,
466 valueLabelFormat = _props$valueLabelForm === void 0 ? Identity : _props$valueLabelForm,
467 other = (0, _objectWithoutProperties2.default)(props, ["aria-label", "aria-labelledby", "aria-valuetext", "classes", "className", "color", "component", "defaultValue", "disabled", "getAriaLabel", "getAriaValueText", "marks", "max", "min", "name", "onChange", "onChangeCommitted", "onMouseDown", "orientation", "scale", "step", "ThumbComponent", "track", "value", "ValueLabelComponent", "valueLabelDisplay", "valueLabelFormat"]);
468 var theme = (0, _useTheme.default)();
469 var touchId = React.useRef(); // We can't use the :active browser pseudo-classes.
470 // - The active state isn't triggered when clicking on the rail.
471 // - The active state isn't transfered when inversing a range slider.
472
473 var _React$useState = React.useState(-1),
474 active = _React$useState[0],
475 setActive = _React$useState[1];
476
477 var _React$useState2 = React.useState(-1),
478 open = _React$useState2[0],
479 setOpen = _React$useState2[1];
480
481 var _useControlled = (0, _useControlled3.default)({
482 controlled: valueProp,
483 default: defaultValue,
484 name: 'Slider'
485 }),
486 _useControlled2 = (0, _slicedToArray2.default)(_useControlled, 2),
487 valueDerived = _useControlled2[0],
488 setValueState = _useControlled2[1];
489
490 var range = Array.isArray(valueDerived);
491 var values = range ? valueDerived.slice().sort(asc) : [valueDerived];
492 values = values.map(function (value) {
493 return clamp(value, min, max);
494 });
495 var marks = marksProp === true && step !== null ? (0, _toConsumableArray2.default)(Array(Math.floor((max - min) / step) + 1)).map(function (_, index) {
496 return {
497 value: min + step * index
498 };
499 }) : marksProp || [];
500
501 var _useIsFocusVisible = (0, _useIsFocusVisible2.default)(),
502 isFocusVisible = _useIsFocusVisible.isFocusVisible,
503 onBlurVisible = _useIsFocusVisible.onBlurVisible,
504 focusVisibleRef = _useIsFocusVisible.ref;
505
506 var _React$useState3 = React.useState(-1),
507 focusVisible = _React$useState3[0],
508 setFocusVisible = _React$useState3[1];
509
510 var sliderRef = React.useRef();
511 var handleFocusRef = (0, _useForkRef.default)(focusVisibleRef, sliderRef);
512 var handleRef = (0, _useForkRef.default)(ref, handleFocusRef);
513 var handleFocus = (0, _useEventCallback.default)(function (event) {
514 var index = Number(event.currentTarget.getAttribute('data-index'));
515
516 if (isFocusVisible(event)) {
517 setFocusVisible(index);
518 }
519
520 setOpen(index);
521 });
522 var handleBlur = (0, _useEventCallback.default)(function () {
523 if (focusVisible !== -1) {
524 setFocusVisible(-1);
525 onBlurVisible();
526 }
527
528 setOpen(-1);
529 });
530 var handleMouseOver = (0, _useEventCallback.default)(function (event) {
531 var index = Number(event.currentTarget.getAttribute('data-index'));
532 setOpen(index);
533 });
534 var handleMouseLeave = (0, _useEventCallback.default)(function () {
535 setOpen(-1);
536 });
537 var isRtl = theme.direction === 'rtl';
538 var handleKeyDown = (0, _useEventCallback.default)(function (event) {
539 var index = Number(event.currentTarget.getAttribute('data-index'));
540 var value = values[index];
541 var tenPercents = (max - min) / 10;
542 var marksValues = marks.map(function (mark) {
543 return mark.value;
544 });
545 var marksIndex = marksValues.indexOf(value);
546 var newValue;
547 var increaseKey = isRtl ? 'ArrowLeft' : 'ArrowRight';
548 var decreaseKey = isRtl ? 'ArrowRight' : 'ArrowLeft';
549
550 switch (event.key) {
551 case 'Home':
552 newValue = min;
553 break;
554
555 case 'End':
556 newValue = max;
557 break;
558
559 case 'PageUp':
560 if (step) {
561 newValue = value + tenPercents;
562 }
563
564 break;
565
566 case 'PageDown':
567 if (step) {
568 newValue = value - tenPercents;
569 }
570
571 break;
572
573 case increaseKey:
574 case 'ArrowUp':
575 if (step) {
576 newValue = value + step;
577 } else {
578 newValue = marksValues[marksIndex + 1] || marksValues[marksValues.length - 1];
579 }
580
581 break;
582
583 case decreaseKey:
584 case 'ArrowDown':
585 if (step) {
586 newValue = value - step;
587 } else {
588 newValue = marksValues[marksIndex - 1] || marksValues[0];
589 }
590
591 break;
592
593 default:
594 return;
595 } // Prevent scroll of the page
596
597
598 event.preventDefault();
599
600 if (step) {
601 newValue = roundValueToStep(newValue, step, min);
602 }
603
604 newValue = clamp(newValue, min, max);
605
606 if (range) {
607 var previousValue = newValue;
608 newValue = setValueIndex({
609 values: values,
610 source: valueDerived,
611 newValue: newValue,
612 index: index
613 }).sort(asc);
614 focusThumb({
615 sliderRef: sliderRef,
616 activeIndex: newValue.indexOf(previousValue)
617 });
618 }
619
620 setValueState(newValue);
621 setFocusVisible(index);
622
623 if (onChange) {
624 onChange(event, newValue);
625 }
626
627 if (onChangeCommitted) {
628 onChangeCommitted(event, newValue);
629 }
630 });
631 var previousIndex = React.useRef();
632 var axis = orientation;
633
634 if (isRtl && orientation !== "vertical") {
635 axis += '-reverse';
636 }
637
638 var getFingerNewValue = function getFingerNewValue(_ref3) {
639 var finger = _ref3.finger,
640 _ref3$move = _ref3.move,
641 move = _ref3$move === void 0 ? false : _ref3$move,
642 values2 = _ref3.values,
643 source = _ref3.source;
644 var slider = sliderRef.current;
645
646 var _slider$getBoundingCl = slider.getBoundingClientRect(),
647 width = _slider$getBoundingCl.width,
648 height = _slider$getBoundingCl.height,
649 bottom = _slider$getBoundingCl.bottom,
650 left = _slider$getBoundingCl.left;
651
652 var percent;
653
654 if (axis.indexOf('vertical') === 0) {
655 percent = (bottom - finger.y) / height;
656 } else {
657 percent = (finger.x - left) / width;
658 }
659
660 if (axis.indexOf('-reverse') !== -1) {
661 percent = 1 - percent;
662 }
663
664 var newValue;
665 newValue = percentToValue(percent, min, max);
666
667 if (step) {
668 newValue = roundValueToStep(newValue, step, min);
669 } else {
670 var marksValues = marks.map(function (mark) {
671 return mark.value;
672 });
673 var closestIndex = findClosest(marksValues, newValue);
674 newValue = marksValues[closestIndex];
675 }
676
677 newValue = clamp(newValue, min, max);
678 var activeIndex = 0;
679
680 if (range) {
681 if (!move) {
682 activeIndex = findClosest(values2, newValue);
683 } else {
684 activeIndex = previousIndex.current;
685 }
686
687 var previousValue = newValue;
688 newValue = setValueIndex({
689 values: values2,
690 source: source,
691 newValue: newValue,
692 index: activeIndex
693 }).sort(asc);
694 activeIndex = newValue.indexOf(previousValue);
695 previousIndex.current = activeIndex;
696 }
697
698 return {
699 newValue: newValue,
700 activeIndex: activeIndex
701 };
702 };
703
704 var handleTouchMove = (0, _useEventCallback.default)(function (event) {
705 var finger = trackFinger(event, touchId);
706
707 if (!finger) {
708 return;
709 }
710
711 var _getFingerNewValue = getFingerNewValue({
712 finger: finger,
713 move: true,
714 values: values,
715 source: valueDerived
716 }),
717 newValue = _getFingerNewValue.newValue,
718 activeIndex = _getFingerNewValue.activeIndex;
719
720 focusThumb({
721 sliderRef: sliderRef,
722 activeIndex: activeIndex,
723 setActive: setActive
724 });
725 setValueState(newValue);
726
727 if (onChange) {
728 onChange(event, newValue);
729 }
730 });
731 var handleTouchEnd = (0, _useEventCallback.default)(function (event) {
732 var finger = trackFinger(event, touchId);
733
734 if (!finger) {
735 return;
736 }
737
738 var _getFingerNewValue2 = getFingerNewValue({
739 finger: finger,
740 values: values,
741 source: valueDerived
742 }),
743 newValue = _getFingerNewValue2.newValue;
744
745 setActive(-1);
746
747 if (event.type === 'touchend') {
748 setOpen(-1);
749 }
750
751 if (onChangeCommitted) {
752 onChangeCommitted(event, newValue);
753 }
754
755 touchId.current = undefined;
756 var doc = (0, _ownerDocument.default)(sliderRef.current);
757 doc.removeEventListener('mousemove', handleTouchMove);
758 doc.removeEventListener('mouseup', handleTouchEnd);
759 doc.removeEventListener('touchmove', handleTouchMove);
760 doc.removeEventListener('touchend', handleTouchEnd);
761 });
762 var handleTouchStart = (0, _useEventCallback.default)(function (event) {
763 // Workaround as Safari has partial support for touchAction: 'none'.
764 event.preventDefault();
765 var touch = event.changedTouches[0];
766
767 if (touch != null) {
768 // A number that uniquely identifies the current finger in the touch session.
769 touchId.current = touch.identifier;
770 }
771
772 var finger = trackFinger(event, touchId);
773
774 var _getFingerNewValue3 = getFingerNewValue({
775 finger: finger,
776 values: values,
777 source: valueDerived
778 }),
779 newValue = _getFingerNewValue3.newValue,
780 activeIndex = _getFingerNewValue3.activeIndex;
781
782 focusThumb({
783 sliderRef: sliderRef,
784 activeIndex: activeIndex,
785 setActive: setActive
786 });
787 setValueState(newValue);
788
789 if (onChange) {
790 onChange(event, newValue);
791 }
792
793 var doc = (0, _ownerDocument.default)(sliderRef.current);
794 doc.addEventListener('touchmove', handleTouchMove);
795 doc.addEventListener('touchend', handleTouchEnd);
796 });
797 React.useEffect(function () {
798 var slider = sliderRef.current;
799 slider.addEventListener('touchstart', handleTouchStart);
800 var doc = (0, _ownerDocument.default)(slider);
801 return function () {
802 slider.removeEventListener('touchstart', handleTouchStart);
803 doc.removeEventListener('mousemove', handleTouchMove);
804 doc.removeEventListener('mouseup', handleTouchEnd);
805 doc.removeEventListener('touchmove', handleTouchMove);
806 doc.removeEventListener('touchend', handleTouchEnd);
807 };
808 }, [handleTouchEnd, handleTouchMove, handleTouchStart]);
809 var handleMouseDown = (0, _useEventCallback.default)(function (event) {
810 if (onMouseDown) {
811 onMouseDown(event);
812 }
813
814 event.preventDefault();
815 var finger = trackFinger(event, touchId);
816
817 var _getFingerNewValue4 = getFingerNewValue({
818 finger: finger,
819 values: values,
820 source: valueDerived
821 }),
822 newValue = _getFingerNewValue4.newValue,
823 activeIndex = _getFingerNewValue4.activeIndex;
824
825 focusThumb({
826 sliderRef: sliderRef,
827 activeIndex: activeIndex,
828 setActive: setActive
829 });
830 setValueState(newValue);
831
832 if (onChange) {
833 onChange(event, newValue);
834 }
835
836 var doc = (0, _ownerDocument.default)(sliderRef.current);
837 doc.addEventListener('mousemove', handleTouchMove);
838 doc.addEventListener('mouseup', handleTouchEnd);
839 });
840 var trackOffset = valueToPercent(range ? values[0] : min, min, max);
841 var trackLeap = valueToPercent(values[values.length - 1], min, max) - trackOffset;
842 var trackStyle = (0, _extends2.default)({}, axisProps[axis].offset(trackOffset), axisProps[axis].leap(trackLeap));
843 return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
844 ref: handleRef,
845 className: (0, _clsx.default)(classes.root, classes["color".concat((0, _capitalize.default)(color))], className, disabled && classes.disabled, marks.length > 0 && marks.some(function (mark) {
846 return mark.label;
847 }) && classes.marked, track === false && classes.trackFalse, orientation === 'vertical' && classes.vertical, track === 'inverted' && classes.trackInverted),
848 onMouseDown: handleMouseDown
849 }, other), /*#__PURE__*/React.createElement("span", {
850 className: classes.rail
851 }), /*#__PURE__*/React.createElement("span", {
852 className: classes.track,
853 style: trackStyle
854 }), /*#__PURE__*/React.createElement("input", {
855 value: values.join(','),
856 name: name,
857 type: "hidden"
858 }), marks.map(function (mark, index) {
859 var percent = valueToPercent(mark.value, min, max);
860 var style = axisProps[axis].offset(percent);
861 var markActive;
862
863 if (track === false) {
864 markActive = values.indexOf(mark.value) !== -1;
865 } else {
866 markActive = track === 'normal' && (range ? mark.value >= values[0] && mark.value <= values[values.length - 1] : mark.value <= values[0]) || track === 'inverted' && (range ? mark.value <= values[0] || mark.value >= values[values.length - 1] : mark.value >= values[0]);
867 }
868
869 return /*#__PURE__*/React.createElement(React.Fragment, {
870 key: mark.value
871 }, /*#__PURE__*/React.createElement("span", {
872 style: style,
873 "data-index": index,
874 className: (0, _clsx.default)(classes.mark, markActive && classes.markActive)
875 }), mark.label != null ? /*#__PURE__*/React.createElement("span", {
876 "aria-hidden": true,
877 "data-index": index,
878 style: style,
879 className: (0, _clsx.default)(classes.markLabel, markActive && classes.markLabelActive)
880 }, mark.label) : null);
881 }), values.map(function (value, index) {
882 var percent = valueToPercent(value, min, max);
883 var style = axisProps[axis].offset(percent);
884 return /*#__PURE__*/React.createElement(ValueLabelComponent, {
885 key: index,
886 valueLabelFormat: valueLabelFormat,
887 valueLabelDisplay: valueLabelDisplay,
888 className: classes.valueLabel,
889 value: typeof valueLabelFormat === 'function' ? valueLabelFormat(scale(value), index) : valueLabelFormat,
890 index: index,
891 open: open === index || active === index || valueLabelDisplay === 'on',
892 disabled: disabled
893 }, /*#__PURE__*/React.createElement(ThumbComponent, {
894 className: (0, _clsx.default)(classes.thumb, classes["thumbColor".concat((0, _capitalize.default)(color))], active === index && classes.active, disabled && classes.disabled, focusVisible === index && classes.focusVisible),
895 tabIndex: disabled ? null : 0,
896 role: "slider",
897 style: style,
898 "data-index": index,
899 "aria-label": getAriaLabel ? getAriaLabel(index) : ariaLabel,
900 "aria-labelledby": ariaLabelledby,
901 "aria-orientation": orientation,
902 "aria-valuemax": scale(max),
903 "aria-valuemin": scale(min),
904 "aria-valuenow": scale(value),
905 "aria-valuetext": getAriaValueText ? getAriaValueText(scale(value), index) : ariaValuetext,
906 onKeyDown: handleKeyDown,
907 onFocus: handleFocus,
908 onBlur: handleBlur,
909 onMouseOver: handleMouseOver,
910 onMouseLeave: handleMouseLeave
911 }));
912 }));
913});
914process.env.NODE_ENV !== "production" ? Slider.propTypes = {
915 /**
916 * The label of the slider.
917 */
918 'aria-label': (0, _utils.chainPropTypes)(_propTypes.default.string, function (props) {
919 var range = Array.isArray(props.value || props.defaultValue);
920
921 if (range && props['aria-label'] != null) {
922 return new Error('Material-UI: You need to use the `getAriaLabel` prop instead of `aria-label` when using a range slider.');
923 }
924
925 return null;
926 }),
927
928 /**
929 * The id of the element containing a label for the slider.
930 */
931 'aria-labelledby': _propTypes.default.string,
932
933 /**
934 * A string value that provides a user-friendly name for the current value of the slider.
935 */
936 'aria-valuetext': (0, _utils.chainPropTypes)(_propTypes.default.string, function (props) {
937 var range = Array.isArray(props.value || props.defaultValue);
938
939 if (range && props['aria-valuetext'] != null) {
940 return new Error('Material-UI: You need to use the `getAriaValueText` prop instead of `aria-valuetext` when using a range slider.');
941 }
942
943 return null;
944 }),
945
946 /**
947 * Override or extend the styles applied to the component.
948 * See [CSS API](#css) below for more details.
949 */
950 classes: _propTypes.default.object.isRequired,
951
952 /**
953 * @ignore
954 */
955 className: _propTypes.default.string,
956
957 /**
958 * The color of the component. It supports those theme colors that make sense for this component.
959 */
960 color: _propTypes.default.oneOf(['primary', 'secondary']),
961
962 /**
963 * The component used for the root node.
964 * Either a string to use a HTML element or a component.
965 */
966 component: _propTypes.default
967 /* @typescript-to-proptypes-ignore */
968 .elementType,
969
970 /**
971 * The default element value. Use when the component is not controlled.
972 */
973 defaultValue: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.arrayOf(_propTypes.default.number)]),
974
975 /**
976 * If `true`, the slider will be disabled.
977 */
978 disabled: _propTypes.default.bool,
979
980 /**
981 * Accepts a function which returns a string value that provides a user-friendly name for the thumb labels of the slider.
982 *
983 * @param {number} index The thumb label's index to format.
984 * @returns {string}
985 */
986 getAriaLabel: _propTypes.default.func,
987
988 /**
989 * Accepts a function which returns a string value that provides a user-friendly name for the current value of the slider.
990 *
991 * @param {number} value The thumb label's value to format.
992 * @param {number} index The thumb label's index to format.
993 * @returns {string}
994 */
995 getAriaValueText: _propTypes.default.func,
996
997 /**
998 * Marks indicate predetermined values to which the user can move the slider.
999 * If `true` the marks will be spaced according the value of the `step` prop.
1000 * If an array, it should contain objects with `value` and an optional `label` keys.
1001 */
1002 marks: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.array]),
1003
1004 /**
1005 * The maximum allowed value of the slider.
1006 * Should not be equal to min.
1007 */
1008 max: _propTypes.default.number,
1009
1010 /**
1011 * The minimum allowed value of the slider.
1012 * Should not be equal to max.
1013 */
1014 min: _propTypes.default.number,
1015
1016 /**
1017 * Name attribute of the hidden `input` element.
1018 */
1019 name: _propTypes.default.string,
1020
1021 /**
1022 * Callback function that is fired when the slider's value changed.
1023 *
1024 * @param {object} event The event source of the callback.
1025 * @param {number | number[]} value The new value.
1026 */
1027 onChange: _propTypes.default.func,
1028
1029 /**
1030 * Callback function that is fired when the `mouseup` is triggered.
1031 *
1032 * @param {object} event The event source of the callback.
1033 * @param {number | number[]} value The new value.
1034 */
1035 onChangeCommitted: _propTypes.default.func,
1036
1037 /**
1038 * @ignore
1039 */
1040 onMouseDown: _propTypes.default.func,
1041
1042 /**
1043 * The slider orientation.
1044 */
1045 orientation: _propTypes.default.oneOf(['horizontal', 'vertical']),
1046
1047 /**
1048 * A transformation function, to change the scale of the slider.
1049 */
1050 scale: _propTypes.default.func,
1051
1052 /**
1053 * The granularity with which the slider can step through values. (A "discrete" slider.)
1054 * The `min` prop serves as the origin for the valid values.
1055 * We recommend (max - min) to be evenly divisible by the step.
1056 *
1057 * When step is `null`, the thumb can only be slid onto marks provided with the `marks` prop.
1058 */
1059 step: _propTypes.default.number,
1060
1061 /**
1062 * The component used to display the value label.
1063 */
1064 ThumbComponent: _propTypes.default.elementType,
1065
1066 /**
1067 * The track presentation:
1068 *
1069 * - `normal` the track will render a bar representing the slider value.
1070 * - `inverted` the track will render a bar representing the remaining slider value.
1071 * - `false` the track will render without a bar.
1072 */
1073 track: _propTypes.default.oneOf(['normal', false, 'inverted']),
1074
1075 /**
1076 * The value of the slider.
1077 * For ranged sliders, provide an array with two values.
1078 */
1079 value: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.arrayOf(_propTypes.default.number)]),
1080
1081 /**
1082 * The value label component.
1083 */
1084 ValueLabelComponent: _propTypes.default.elementType,
1085
1086 /**
1087 * Controls when the value label is displayed:
1088 *
1089 * - `auto` the value label will display when the thumb is hovered or focused.
1090 * - `on` will display persistently.
1091 * - `off` will never display.
1092 */
1093 valueLabelDisplay: _propTypes.default.oneOf(['on', 'auto', 'off']),
1094
1095 /**
1096 * The format function the value label's value.
1097 *
1098 * When a function is provided, it should have the following signature:
1099 *
1100 * - {number} value The value label's value to format
1101 * - {number} index The value label's index to format
1102 */
1103 valueLabelFormat: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func])
1104} : void 0;
1105
1106var _default = (0, _withStyles.default)(styles, {
1107 name: 'MuiSlider'
1108})(Slider);
1109
1110exports.default = _default;
\No newline at end of file