-
-
Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathTimeLine.js
147 lines (134 loc) · 4.31 KB
/
TimeLine.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
import React, { useState, useRef, useEffect } from "react";
import {
CarouselButton,
CarouselButtonDot,
CarouselButtons,
CarouselContainer,
CarouselItem,
CarouselItemImg,
CarouselItemText,
CarouselItemTitle,
CarouselMobileScrollNode,
} from "./TimeLineStyles";
import {
Section,
SectionDivider,
SectionText,
SectionTitle,
} from "../../styles/GlobalComponents";
import { TimeLineData } from "../../constants/constants";
const TOTAL_CAROUSEL_COUNT = TimeLineData.length;
const Timeline = () => {
const [activeItem, setActiveItem] = useState(0);
const carouselRef = useRef();
const scroll = (node, left) => {
return node.scrollTo({ left, behavior: "smooth" });
};
const handleClick = (e, i) => {
e.preventDefault();
if (carouselRef.current) {
const scrollLeft = Math.floor(
carouselRef.current.scrollWidth * 0.7 * (i / TimeLineData.length)
);
scroll(carouselRef.current, scrollLeft);
}
};
const handleScroll = () => {
if (carouselRef.current) {
const index = Math.round(
(carouselRef.current.scrollLeft /
(carouselRef.current.scrollWidth * 0.7)) *
TimeLineData.length
);
setActiveItem(index);
}
};
// snap back to beginning of scroll when window is resized
// avoids a bug where content is covered up if coming from smaller screen
useEffect(() => {
const handleResize = () => {
scroll(carouselRef.current, 0);
};
window.addEventListener("resize", handleResize);
}, []);
return (
<Section id="about">
<SectionTitle>About Me</SectionTitle>
<SectionText>
The purpose of HolderDesigns is to bring valuable design and development solutions
to assist with modern business problems.
</SectionText>
<CarouselContainer ref={carouselRef} onScroll={handleScroll}>
<>
{TimeLineData.map((item, index) => (
<CarouselMobileScrollNode
key={index}
final={index === TOTAL_CAROUSEL_COUNT - 1}
>
<CarouselItem
index={index}
id={`carousel__item-${index}`}
active={activeItem}
onClick={(e) => handleClick(e, index)}
>
<CarouselItemTitle>
{`${item.year}`}
<CarouselItemImg
width="208"
height="6"
viewBox="0 0 208 6"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M2.5 5.5C3.88071 5.5 5 4.38071 5 3V3.5L208 3.50002V2.50002L5 2.5V3C5 1.61929 3.88071 0.5 2.5 0.5C1.11929 0.5 0 1.61929 0 3C0 4.38071 1.11929 5.5 2.5 5.5Z"
fill="url(#paint0_linear)"
fill-opacity="0.33"
/>
<defs>
<linearGradient
id="paint0_linear"
x1="-4.30412e-10"
y1="0.5"
x2="208"
y2="0.500295"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="white" />
<stop
offset="0.79478"
stop-color="white"
stop-opacity="0"
/>
</linearGradient>
</defs>
</CarouselItemImg>
</CarouselItemTitle>
<CarouselItemText>{item.text}</CarouselItemText>
</CarouselItem>
</CarouselMobileScrollNode>
))}
</>
</CarouselContainer>
<CarouselButtons>
{TimeLineData.map((item, index) => {
return (
<CarouselButton
key={index}
index={index}
active={activeItem}
onClick={(e) => handleClick(e, index)}
type="button"
>
<CarouselButtonDot active={activeItem} />
</CarouselButton>
);
})}
</CarouselButtons>
<SectionDivider />
</Section>
);
};
export default Timeline;