Skip to content
This repository was archived by the owner on Apr 19, 2021. It is now read-only.

Commit 6801e97

Browse files
committed
fix the dots color switching.
1 parent 95e3020 commit 6801e97

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/components/index/Testimonials.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ const StyledTestimonials = styled.div`
6363
border-radius: 50%;
6464
transition: all .2s;
6565
66-
&:hover,
67-
&:focus {
66+
&:hover {
6867
background: ${colors.lightGrey};
6968
}
7069
@@ -75,19 +74,34 @@ const StyledTestimonials = styled.div`
7574
`
7675

7776
const Testimonials: React.SFC<{}> = () => {
78-
const [currentCycle, setCurrentCycle] = useState<number>(0)
7977
const tweetsRef = useRef<HTMLDivElement>(null)
8078
const tweetsContainerRef = useRef<HTMLDivElement>(null)
8179
const cycles = Math.ceil(testimonials.length / 3)
8280

81+
const setDotsColor = (cycle) => {
82+
const dots = document.querySelectorAll('.dot')
83+
84+
dots.forEach((d, i) => {
85+
if (cycle === i) {
86+
d.style.background = colors.lightGrey
87+
} else {
88+
d.style.background = colors.offWhite
89+
}
90+
})
91+
}
92+
8393
useEffect(() => {
8494
const tweetsContainer = tweetsContainerRef.current
85-
const dots = document.querySelectorAll('.dot')
86-
const cycleWidth = tweetsContainer?.scrollWidth / cycles
95+
const tweets = tweetsRef.current
96+
const firstTweet = tweets?.firstChild
97+
const spacing = getComputedStyle(firstTweet)['margin-right']
98+
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
99+
setDotsColor(0)
87100

88101
tweetsContainer.addEventListener('scroll', (e) => {
89102
const currentScrollPosition = tweetsContainer?.scrollLeft
90-
setCurrentCycle(cycles - Math.floor((tweetsContainer?.scrollWidth - currentScrollPosition) / cycleWidth))
103+
const cycle = cycles - Math.ceil((tweetsContainer?.scrollWidth - currentScrollPosition) / scrollBy)
104+
setDotsColor(cycle)
91105
})
92106
})
93107

@@ -97,6 +111,7 @@ const Testimonials: React.SFC<{}> = () => {
97111
const firstTweet = tweets?.firstChild
98112
const spacing = getComputedStyle(firstTweet)['margin-right']
99113
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
114+
100115
tweetsContainer.scroll({ left: parseFloat(scrollBy * to), behavior: 'smooth' })
101116
}
102117

@@ -121,10 +136,8 @@ const Testimonials: React.SFC<{}> = () => {
121136
className="dot"
122137
key={d}
123138
onClick={() => {
124-
setCurrentCycle(d)
125139
switchTweets(d)
126140
}}
127-
style={currentCycle === d ? { background: colors.lightGrey } : {}}
128141
title={`Switch to ${d + 1}th set of tweets.`}
129142
>
130143
&nbsp;

0 commit comments

Comments
 (0)