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

Commit acd5ad0

Browse files
committed
fix the dots color switching.
1 parent 13ee1de commit acd5ad0

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/components/index/Testimonials.tsx

Lines changed: 18 additions & 14 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,37 +74,44 @@ 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

83-
console.log(cycles, testimonials.length)
81+
const setDotsColor = (cycle) => {
82+
const dots = document.querySelectorAll('.dot')
83+
console.log("Cycle is:", cycle)
84+
85+
dots.forEach((d, i) => {
86+
if (cycle === i) {
87+
d.style.background = colors.lightGrey
88+
} else {
89+
d.style.background = colors.offWhite
90+
}
91+
})
92+
}
8493

8594
useEffect(() => {
8695
const tweetsContainer = tweetsContainerRef.current
87-
const dots = document.querySelectorAll('.dot')
8896
const cycleWidth = tweetsContainer?.scrollWidth / cycles
8997

9098
tweetsContainer.addEventListener('scroll', (e) => {
9199
const currentScrollPosition = tweetsContainer?.scrollLeft
92-
setCurrentCycle(cycles - Math.floor((tweetsContainer?.scrollWidth - currentScrollPosition) / cycleWidth))
100+
const cycle = cycles - Math.floor((tweetsContainer?.scrollWidth - currentScrollPosition) / cycleWidth)
101+
setDotsColor(cycle)
93102
})
94103
})
95104

96105
const switchTweets = (to: number) => {
97-
// const tweetsContainer = tweetsContainerRef.current
98-
// const cycleWidth = tweetsContainer?.scrollWidth / cycles
99-
// tweetsContainer.scroll({ left: parseFloat(cycleWidth * to), behavior: 'smooth' })
100-
101-
// Older method which regards the actuals tweets and the space between them and not the width of .tweets
102-
103106
const tweetsContainer = tweetsContainerRef.current
104107
const tweets = tweetsRef.current
105108
const firstTweet = tweets?.firstChild
106109
const spacing = getComputedStyle(firstTweet)['margin-right']
107110
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
108111
tweetsContainer.scroll({ left: parseFloat(scrollBy * to), behavior: 'smooth' })
112+
setTimeout(() => {
113+
setDotsColor(to)
114+
}, 500)
109115
}
110116

111117
return (
@@ -129,10 +135,8 @@ const Testimonials: React.SFC<{}> = () => {
129135
className="dot"
130136
key={d}
131137
onClick={() => {
132-
setCurrentCycle(d)
133138
switchTweets(d)
134139
}}
135-
style={currentCycle === d ? { background: colors.lightGrey } : {}}
136140
title={`Switch to ${d + 1}th set of tweets.`}
137141
>
138142
&nbsp;

0 commit comments

Comments
 (0)