Skip to content

Commit 5fbaecb

Browse files
authored
Merge pull request #1 from Rmnlly/csb-1569116728618
started hooks update
2 parents f8804b5 + a43fd3b commit 5fbaecb

File tree

1 file changed

+131
-3
lines changed

1 file changed

+131
-3
lines changed

src/carousel.jsx

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,133 @@
1-
import React from "react";
1+
import React, { useState, useRef, useEffect, useReducer } from "react";
22
import "./styles.css";
33

4-
export default class Carousel extends React.Component {
4+
//A hook for getting the previous state/a previous value, if needed
5+
const usePrevious = value => {
6+
const ref = useRef();
7+
useEffect(() => {
8+
ref.current = value;
9+
});
10+
return ref.current;
11+
};
12+
13+
const useWidth = ref => {
14+
console.log("called useWidth");
15+
const [width, setWidth] = useState(0);
16+
useEffect(() => {
17+
setWidth(ref.current.getBoundingClientRect().width);
18+
}, [ref]);
19+
};
20+
21+
const telemetryReducer = (state, action) => {
22+
switch (action.type) {
23+
case "user_move":
24+
return {
25+
angle: action.angle,
26+
change: action.change,
27+
velocity: action.velocity
28+
};
29+
case "decay_move":
30+
return {
31+
...state,
32+
angle: action.angle,
33+
velocity: action.velocity
34+
};
35+
case "reset_change":
36+
return {
37+
...state,
38+
change: 0
39+
};
40+
case "reset_velocity":
41+
return { ...state, velocity: 0 };
42+
43+
default:
44+
return state;
45+
}
46+
};
47+
48+
const Carousel2 = () => {
49+
const [dragState, setDragState] = useState({
50+
dragging: false,
51+
dragStartX: 0,
52+
dragStartTime: 0
53+
});
54+
const [carouselTelemetry, dispatch] = useReducer(telemetryReducer, {
55+
angle: 0,
56+
change: 0,
57+
velocity: 0
58+
});
59+
const [decayInterval, setDecayInterval] = useState(null);
60+
const draggableRef = useRef();
61+
const width = useWidth(draggableRef);
62+
63+
useEffect(() => {
64+
window.addEventListener("mouseup", onDragEndMouse);
65+
window.addEventListener("touchend", onDragEndTouch);
66+
67+
return () => {
68+
window.removeEventListener("mouseup", onDragEndMouse);
69+
window.removeEventListener("touchend", onDragEndTouch);
70+
};
71+
}, []);
72+
73+
const onDragEndMouse = e => {
74+
window.removeEventListener("mousemove", this.onMouseMove);
75+
onDragEnd();
76+
};
77+
78+
const onDragEndTouch = e => {
79+
window.removeEventListener("touchmove", this.onTouchMove);
80+
onDragEnd();
81+
};
82+
83+
const onDragStartMouse = e => {
84+
onDragStart(e.clientX);
85+
window.addEventListener("mousemove", this.onMouseMove);
86+
};
87+
88+
const onDragStartTouch = e => {
89+
const touch = e.targetTouches[0];
90+
onDragStart(touch.clientX);
91+
window.addEventListener("touchmove", this.onTouchMove);
92+
};
93+
94+
const onDragStart = clientX => {
95+
if (decayInterval !== null) {
96+
clearInterval(decayInterval);
97+
}
98+
setDragState({ dragStartX: clientX, dragging: true, velocity: 0 });
99+
requestAnimationFrame(updatePosition);
100+
};
101+
102+
const onDragEnd = () => {
103+
setDragState(state => ({ ...state, change: 0, dragging: false }));
104+
setDecayInterval(state =>
105+
state === null ? setInterval(animateSliding, 66) : null
106+
);
107+
};
108+
109+
return (
110+
<div>
111+
<h1>Spin the text below!</h1>
112+
<div
113+
ref={this.draggableRef}
114+
onMouseDown={onDragStartMouse}
115+
onTouchStart={onDragStartTouch}
116+
className="spinText"
117+
>
118+
touch move me wherever necessary
119+
</div>
120+
<br />
121+
<br />
122+
<br />
123+
<span>
124+
{`pos: ${carouselTelemetry.angle} velo:${carouselTelemetry.velocity}`}
125+
</span>
126+
</div>
127+
);
128+
};
129+
130+
class Carousel extends React.Component {
5131
constructor(props) {
6132
super(props);
7133

@@ -13,8 +139,8 @@ export default class Carousel extends React.Component {
13139
dragStartTime: 0,
14140
carouselPosition: 0, //This can be a value between 0 - 360,
15141
change: 0,
16-
width: 0,
17142
velocity: 0,
143+
width: 0,
18144
intervalId: null
19145
};
20146

@@ -201,3 +327,5 @@ export default class Carousel extends React.Component {
201327
);
202328
}
203329
}
330+
331+
export default Carousel;

0 commit comments

Comments
 (0)