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
|
/**
* Image gallery
*/
import React, { useState, useEffect } from "react";
import { mod } from "../utils/index.js";
export default function Gallery({ images, visible, onLoad }) {
const hasItems = !!images?.length;
const oneItem = images?.length === 1;
const [index, setIndex] = useState(0);
const [opacity, setOpacity] = useState(0);
useEffect(() => {
setIndex(0);
setOpacity(0);
setTimeout(() => setOpacity(1), 500);
onLoad();
}, [images]);
function previous() {
setOpacity(0);
onLoad();
setTimeout(() => setIndex(mod(index - 1, images.length)), 200);
// setTimeout(() => setOpacity(1), 500);
}
function next() {
setOpacity(0);
onLoad();
setTimeout(() => setIndex(mod(index + 1, images.length)), 200);
// setTimeout(() => setOpacity(1), 500);
}
function nextOrWrap() {
if (oneItem) return;
setOpacity(0);
onLoad();
setTimeout(() => setIndex(mod(index + 1, images.length)), 200);
// setTimeout(() => setOpacity(1), 500);
}
function appear() {
setOpacity(1);
onLoad();
}
if (!hasItems) {
return <div className="gallery" style={{ opacity: 0 }} />;
}
return (
<div className="gallery" style={{ opacity: visible ? 1 : 0 }}>
<div className="image">
{visible && !!images[index] && (
<img
src={images[index].uri}
onClick={nextOrWrap}
onLoad={appear}
style={{ opacity }}
/>
)}
</div>
<div className="buttons arrows">
{!oneItem && (
<div>
<img
src="/assets/img/arrow-back.svg"
onClick={previous}
style={{ opacity: index > 0 ? 1 : 0 }}
/>
</div>
)}
{!oneItem && (
<div>
<img
src="/assets/img/arrow-forward.svg"
onClick={next}
style={{ opacity: index < images.length - 1 ? 1 : 0 }}
/>
</div>
)}
</div>
</div>
);
}
|