MCPcopy Create free account
hub / github.com/QuarkGluonPlasma/react-course-code / Viewpager

Function Viewpager

use-gesture-test/src/App.tsx:15–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13]
14
15function Viewpager() {
16 const index = useRef(0);
17 const width = window.innerWidth;
18
19 const [props, api] = useSprings(pages.length, i => ({
20 x: i * width,
21 scale: 1
22 }));
23
24 const bind = useDrag(({ active, movement: [mx], direction: [xDir], cancel }) => {
25 if (active && Math.abs(mx) > width / 2) {
26 let newIndex = index.current + (xDir > 0 ? -1 : 1);
27
28 if(newIndex < 0) {
29 newIndex = 0;
30 }
31
32 if(newIndex > pages.length - 1) {
33 newIndex = pages.length - 1;
34 }
35
36 index.current = newIndex;
37
38 cancel()
39 }
40 api.start(i => {
41 const x = (i - index.current) * width + (active ? mx : 0)
42 const scale = active ? 1 - Math.abs(mx) / width / 2 : 1
43 return { x, scale }
44 })
45 });
46
47 return (
48 <div className='wrapper'>
49 {props.map(({ x, scale }, i) => (
50 <animated.div className='page' {...bind()} key={i} style={{ x }}>
51 <animated.div style={{ scale, backgroundImage: `url(${pages[i]})` }} />
52 </animated.div>
53 ))}
54 </div>
55 )
56}
57
58export default Viewpager;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected