-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewGallery.js
177 lines (159 loc) · 4.36 KB
/
newGallery.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
import React, { useState, useEffect } from "react";
import imgPath from "../assets/anand.jpg";
import magnus from "../assets/magnus.jpeg";
import ding from "../assets/ding.jpeg";
import fabi from "../assets/fabi.jpg";
import wesley from "../assets/wesley.jpeg";
import hou from "../assets/hou.jpg";
import ju from "../assets/ju.jpeg";
const slideWidth = 30;
const _items = [
{
player: {
title: "Vishy Anand",
desc: 'Known as "The Madras Tiger", Anand is 5 time world champion and legend of Indian Chess.',
image: imgPath,
},
},
{
player: {
title: "Magnus Carlsen",
desc: "World Chess Champion, Rank 1, World Rapid Champion",
image: magnus,
},
},
{
player: {
title: "Ding Liren",
desc: 'The "Chinese Dragon" is currently world No.3 and wins 2019 Chess Olympiad under his captaincy.',
image: ding,
},
},
{
player: {
title: "Fabiano Caruana",
desc: 'Fabi" as many like to call him is an American professional chess player with current No.2 Rank.',
image: fabi,
},
},
{
player: {
title: "Wesley So",
desc: "World No. 6, Fischer Random Champion. Emerges as most successful Online Player during Covid era.",
image: wesley,
},
},
{
player: {
title: "Hou Yifan",
desc: "Best active female player. World No. 1, Former Womens World Champion. ",
image: hou,
},
},
{
player: {
title: "Ju Wenjun",
desc: "Ju Wenjun from China is the current Womens World Champion with 2600+ elo ratings.",
image: ju,
},
},
];
const length = _items.length;
_items.push(..._items);
const sleep = (ms = 0) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const createItem = (position, idx) => {
const item = {
styles: {
transform: `translateX(${position * slideWidth}rem)`,
},
player: _items[idx].player,
};
switch (position) {
case length - 1:
case length + 1:
item.styles = { ...item.styles, filter: "grayscale(1)" };
break;
case length:
break;
default:
item.styles = { ...item.styles, opacity: 0 };
break;
}
return item;
};
const CarouselSlideItem = ({ pos, idx, activeIdx }) => {
const item = createItem(pos, idx, activeIdx);
return (
<li className="carousel__slide-item" style={item.styles}>
<div className="carousel__slide-item-img-link">
<img src={item.player.image} alt={item.player.title} />
</div>
<div className="carousel-slide-item__body">
<h5>{item.player.title}</h5>
<p>{item.player.desc}</p>
</div>
</li>
);
};
const keys = Array.from(Array(_items.length).keys());
const Carousel = () => {
const [items, setItems] = useState(keys);
const [isTicking, setIsTicking] = useState(false);
const [activeIdx, setActiveIdx] = useState(0);
const bigLength = items.length;
const prevClick = (jump = 1) => {
if (!isTicking) {
setIsTicking(true);
setItems((prev) => {
return prev.map((_, i) => prev[(i + jump) % bigLength]);
});
}
};
const nextClick = (jump = 1) => {
if (!isTicking) {
setIsTicking(true);
setItems((prev) => {
return prev.map((_, i) => prev[(i - jump + bigLength) % bigLength]);
});
}
};
useEffect(() => {
if (isTicking) sleep(300).then(() => setIsTicking(false));
}, [isTicking]);
useEffect(() => {
setActiveIdx((length - (items[0] % length)) % length) // prettier-ignore
}, [items]);
return (
<div className="carousel__wrap">
<div className="carousel__inner">
<button
className="carousel__btn carousel__btn--prev"
onClick={() => prevClick()}
>
<i className="carousel__btn-arrow carousel__btn-arrow--left" />
</button>
<div className="carousel__container">
<ul className="carousel__slide-list">
{items.map((pos, i) => (
<CarouselSlideItem
key={i}
idx={i}
pos={pos}
activeIdx={activeIdx}
/>
))}
</ul>
</div>
<button
className="carousel__btn carousel__btn--next"
onClick={() => nextClick()}
>
<i className="carousel__btn-arrow carousel__btn-arrow--right" />
</button>
</div>
</div>
);
};
export default Carousel;