source: imaps-frontend/node_modules/react-use/esm/useSlider.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.4 KB
Line 
1import { useEffect, useRef } from 'react';
2import { isBrowser, noop, off, on } from './misc/util';
3import useMountedState from './useMountedState';
4import useSetState from './useSetState';
5var useSlider = function (ref, options) {
6 if (options === void 0) { options = {}; }
7 var isMounted = useMountedState();
8 var isSliding = useRef(false);
9 var valueRef = useRef(0);
10 var frame = useRef(0);
11 var _a = useSetState({
12 isSliding: false,
13 value: 0,
14 }), state = _a[0], setState = _a[1];
15 valueRef.current = state.value;
16 useEffect(function () {
17 if (isBrowser) {
18 var styles = options.styles === undefined ? true : options.styles;
19 var reverse_1 = options.reverse === undefined ? false : options.reverse;
20 if (ref.current && styles) {
21 ref.current.style.userSelect = 'none';
22 }
23 var startScrubbing_1 = function () {
24 if (!isSliding.current && isMounted()) {
25 (options.onScrubStart || noop)();
26 isSliding.current = true;
27 setState({ isSliding: true });
28 bindEvents_1();
29 }
30 };
31 var stopScrubbing_1 = function () {
32 if (isSliding.current && isMounted()) {
33 (options.onScrubStop || noop)(valueRef.current);
34 isSliding.current = false;
35 setState({ isSliding: false });
36 unbindEvents_1();
37 }
38 };
39 var onMouseDown_1 = function (event) {
40 startScrubbing_1();
41 onMouseMove_1(event);
42 };
43 var onMouseMove_1 = options.vertical
44 ? function (event) { return onScrub_1(event.clientY); }
45 : function (event) { return onScrub_1(event.clientX); };
46 var onTouchStart_1 = function (event) {
47 startScrubbing_1();
48 onTouchMove_1(event);
49 };
50 var onTouchMove_1 = options.vertical
51 ? function (event) { return onScrub_1(event.changedTouches[0].clientY); }
52 : function (event) { return onScrub_1(event.changedTouches[0].clientX); };
53 var bindEvents_1 = function () {
54 on(document, 'mousemove', onMouseMove_1);
55 on(document, 'mouseup', stopScrubbing_1);
56 on(document, 'touchmove', onTouchMove_1);
57 on(document, 'touchend', stopScrubbing_1);
58 };
59 var unbindEvents_1 = function () {
60 off(document, 'mousemove', onMouseMove_1);
61 off(document, 'mouseup', stopScrubbing_1);
62 off(document, 'touchmove', onTouchMove_1);
63 off(document, 'touchend', stopScrubbing_1);
64 };
65 var onScrub_1 = function (clientXY) {
66 cancelAnimationFrame(frame.current);
67 frame.current = requestAnimationFrame(function () {
68 if (isMounted() && ref.current) {
69 var rect = ref.current.getBoundingClientRect();
70 var pos = options.vertical ? rect.top : rect.left;
71 var length_1 = options.vertical ? rect.height : rect.width;
72 // Prevent returning 0 when element is hidden by CSS
73 if (!length_1) {
74 return;
75 }
76 var value = (clientXY - pos) / length_1;
77 if (value > 1) {
78 value = 1;
79 }
80 else if (value < 0) {
81 value = 0;
82 }
83 if (reverse_1) {
84 value = 1 - value;
85 }
86 setState({
87 value: value,
88 });
89 (options.onScrub || noop)(value);
90 }
91 });
92 };
93 on(ref.current, 'mousedown', onMouseDown_1);
94 on(ref.current, 'touchstart', onTouchStart_1);
95 return function () {
96 off(ref.current, 'mousedown', onMouseDown_1);
97 off(ref.current, 'touchstart', onTouchStart_1);
98 };
99 }
100 else {
101 return undefined;
102 }
103 }, [ref, options.vertical]);
104 return state;
105};
106export default useSlider;
Note: See TracBrowser for help on using the repository browser.