@@ -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,37 +74,44 @@ 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
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
+ }
84
93
85
94
useEffect ( ( ) => {
86
95
const tweetsContainer = tweetsContainerRef . current
87
- const dots = document . querySelectorAll ( '.dot' )
88
96
const cycleWidth = tweetsContainer ?. scrollWidth / cycles
89
97
90
98
tweetsContainer . addEventListener ( 'scroll' , ( e ) => {
91
99
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 )
93
102
} )
94
103
} )
95
104
96
105
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
-
103
106
const tweetsContainer = tweetsContainerRef . current
104
107
const tweets = tweetsRef . current
105
108
const firstTweet = tweets ?. firstChild
106
109
const spacing = getComputedStyle ( firstTweet ) [ 'margin-right' ]
107
110
const scrollBy = firstTweet . offsetWidth * 3 + parseFloat ( spacing . substring ( 0 , spacing . length - 2 ) ) * 2.8
108
111
tweetsContainer . scroll ( { left : parseFloat ( scrollBy * to ) , behavior : 'smooth' } )
112
+ setTimeout ( ( ) => {
113
+ setDotsColor ( to )
114
+ } , 500 )
109
115
}
110
116
111
117
return (
@@ -129,10 +135,8 @@ const Testimonials: React.SFC<{}> = () => {
129
135
className = "dot"
130
136
key = { d }
131
137
onClick = { ( ) => {
132
- setCurrentCycle ( d )
133
138
switchTweets ( d )
134
139
} }
135
- style = { currentCycle === d ? { background : colors . lightGrey } : { } }
136
140
title = { `Switch to ${ d + 1 } th set of tweets.` }
137
141
>
138
142
0 commit comments