@@ -63,8 +63,7 @@ const StyledTestimonials = styled.div`
63
63
border-radius: 50%;
64
64
transition: all .2s;
65
65
66
- &:hover,
67
- &:focus {
66
+ &:hover {
68
67
background: ${ colors . lightGrey } ;
69
68
}
70
69
@@ -75,19 +74,34 @@ const StyledTestimonials = styled.div`
75
74
`
76
75
77
76
const Testimonials : React . SFC < { } > = ( ) => {
78
- const [ currentCycle , setCurrentCycle ] = useState < number > ( 0 )
79
77
const tweetsRef = useRef < HTMLDivElement > ( null )
80
78
const tweetsContainerRef = useRef < HTMLDivElement > ( null )
81
79
const cycles = Math . ceil ( testimonials . length / 3 )
82
80
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
+
83
93
useEffect ( ( ) => {
84
94
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 )
87
100
88
101
tweetsContainer . addEventListener ( 'scroll' , ( e ) => {
89
102
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 )
91
105
} )
92
106
} )
93
107
@@ -97,6 +111,7 @@ const Testimonials: React.SFC<{}> = () => {
97
111
const firstTweet = tweets ?. firstChild
98
112
const spacing = getComputedStyle ( firstTweet ) [ 'margin-right' ]
99
113
const scrollBy = firstTweet . offsetWidth * 3 + parseFloat ( spacing . substring ( 0 , spacing . length - 2 ) ) * 2.8
114
+
100
115
tweetsContainer . scroll ( { left : parseFloat ( scrollBy * to ) , behavior : 'smooth' } )
101
116
}
102
117
@@ -121,10 +136,8 @@ const Testimonials: React.SFC<{}> = () => {
121
136
className = "dot"
122
137
key = { d }
123
138
onClick = { ( ) => {
124
- setCurrentCycle ( d )
125
139
switchTweets ( d )
126
140
} }
127
- style = { currentCycle === d ? { background : colors . lightGrey } : { } }
128
141
title = { `Switch to ${ d + 1 } th set of tweets.` }
129
142
>
130
143
0 commit comments