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

Commit b0a7377

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

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/components/index/Testimonials.tsx

Lines changed: 20 additions & 15 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,36 +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+
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+
}
8492

8593
useEffect(() => {
8694
const tweetsContainer = tweetsContainerRef.current
87-
const dots = document.querySelectorAll('.dot')
88-
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)
89100

90101
tweetsContainer.addEventListener('scroll', (e) => {
91102
const currentScrollPosition = tweetsContainer?.scrollLeft
92-
setCurrentCycle(cycles - Math.floor((tweetsContainer?.scrollWidth - currentScrollPosition) / cycleWidth))
103+
const cycle = cycles - Math.ceil((tweetsContainer?.scrollWidth - currentScrollPosition) / scrollBy)
104+
setDotsColor(cycle)
93105
})
94106
})
95107

96108
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-
103109
const tweetsContainer = tweetsContainerRef.current
104110
const tweets = tweetsRef.current
105111
const firstTweet = tweets?.firstChild
106112
const spacing = getComputedStyle(firstTweet)['margin-right']
107113
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
114+
108115
tweetsContainer.scroll({ left: parseFloat(scrollBy * to), behavior: 'smooth' })
109116
}
110117

@@ -129,10 +136,8 @@ const Testimonials: React.SFC<{}> = () => {
129136
className="dot"
130137
key={d}
131138
onClick={() => {
132-
setCurrentCycle(d)
133139
switchTweets(d)
134140
}}
135-
style={currentCycle === d ? { background: colors.lightGrey } : {}}
136141
title={`Switch to ${d + 1}th set of tweets.`}
137142
>
138143
&nbsp;

0 commit comments

Comments
 (0)