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