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

add julius volz tweet + fix the dots coloring. #852

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/components/index/Testimonials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ const StyledTestimonials = styled.div`
border-radius: 50%;
transition: all .2s;

&:hover,
&:focus {
&:hover {
background: ${colors.lightGrey};
}

Expand All @@ -75,33 +74,45 @@ const StyledTestimonials = styled.div`
`

const Testimonials: React.SFC<{}> = () => {
const [currentCycle, setCurrentCycle] = useState<number>(0)
const tweetsRef = useRef<HTMLDivElement>(null)
const tweetsContainerRef = useRef<HTMLDivElement>(null)
const cycles = Math.ceil(testimonials.length / 3)

const setDotsColor = (cycle) => {
const dots = document.querySelectorAll('.dot')

dots.forEach((d, i) => {
if (cycle === i) {
d.style.background = colors.lightGrey
} else {
d.style.background = colors.offWhite
}
})
}

useEffect(() => {
const tweetsContainer = tweetsContainerRef.current
const dots = document.querySelectorAll('.dot')
const cycleWidth = tweetsContainer?.scrollWidth / cycles
const tweets = tweetsRef.current
const firstTweet = tweets?.firstChild
const spacing = getComputedStyle(firstTweet)['margin-right']
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
setDotsColor(0)

tweetsContainer.addEventListener('scroll', (e) => {
const currentScrollPosition = tweetsContainer?.scrollLeft
setCurrentCycle(cycles - Math.floor((tweetsContainer?.scrollWidth - currentScrollPosition) / cycleWidth))
const cycle = cycles - Math.ceil((tweetsContainer?.scrollWidth - currentScrollPosition) / scrollBy)
setDotsColor(cycle)
})
})
Comment on lines 101 to 106
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel good about this block of code as it gets re-ran every time the scroll happens i.e I feel like it is quite expensive and unwise to call the setDotsColor function everytime scroll happens would be nice if some how we could detect that the scroll position is at a different set of tweets and then and only then call the set dots color funtion ? i.e only 6 times for 6 sets (each set containing 3 tweets). @jankeromnes Do you think this is something that is worth spending the time for its optimization ? I can try to figure out a solution ?

Copy link
Contributor

@jankeromnes jankeromnes Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nisarhassan12! As suggested in our 1:1 discussion, please look into:

  • How to write high-performance scroll event listeners (i.e. they should never block scrolling, and they should never be a performance problem -- I remember seeing various blog posts about how to do this from people in the front-end community, but I don't have a direct link)
  • How to debounce things that happen too frequently (e.g. if this listener happens 1000 times per second, maybe you can debounce it such that it only happens a few times per second)

Additionally, I took a look at why the edge dots are sometimes not selected (last dot on desktop, first on mobile).

The value element.scrollLeft actually goes from 0 to element.scrollWidth - element.clientWidth (taking clientWidth into account is important). So:

  • When scrollLeft is 0, the left-most dot should be highlighted
  • When scrollLeft is scrollWidth - clientWidth, the right-most dot should be highlighted
  • When it's in-between, maybe you can divide the percentage by scrollBy, but first you'll need to verify that scrollBy * cycles is the same as scrollWidth

Hope this helps! Let me know if you still run into problems with this (then maybe we could finish solving it together in a call).


const switchTweets = (to: number) => {
const tweetsContainer = tweetsContainerRef.current
const cycleWidth = tweetsContainer?.scrollWidth / cycles
tweetsContainer.scroll({ left: parseFloat(cycleWidth * to), behavior: 'smooth' })

// Older method which regards the actuals tweets and the space between them and not the width of .tweets
const tweets = tweetsRef.current
const firstTweet = tweets?.firstChild
const spacing = getComputedStyle(firstTweet)['margin-right']
const scrollBy = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8

// const tweets = tweetsRef.current
// const firstTweet = tweets?.firstChild
// const spacing = getComputedStyle(firstTweet)['margin-right']
// const transform = firstTweet.offsetWidth * 3 + parseFloat(spacing.substring(0, spacing.length - 2)) * 2.8
tweetsContainer.scroll({ left: parseFloat(scrollBy * to), behavior: 'smooth' })
}

return (
Expand All @@ -125,10 +136,8 @@ const Testimonials: React.SFC<{}> = () => {
className="dot"
key={d}
onClick={() => {
setCurrentCycle(d)
switchTweets(d)
}}
style={currentCycle === d ? { background: colors.lightGrey } : {}}
title={`Switch to ${d + 1}th set of tweets.`}
>
&nbsp;
Expand Down
16 changes: 16 additions & 0 deletions src/contents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Yo from '../resources/yo.jpg'
import K33g from '../resources/k33g.jpg'
import Michael from '../resources/michael.jpg'
import Roku from '../resources/roku.jpg'
import Julius from '../resources/julius.jpg'
import { TestimonialProps } from '../components/index/Testimonial'

import { Link } from 'gatsby'
Expand Down Expand Up @@ -156,6 +157,21 @@ export const testimonials: TestimonialProps[] = [
twitterHandle: 'dnsmichi',
tweetId: '1311560585717460992'
},
{
name: 'Julius Volz',
avatar: Julius,
org: <a href="https://twitter.com/PrometheusIO" target="_blank">@PrometheusIO</a>,
role: 'Creator',
text: (
<>
<p>
Look at any PR (pull request) in a full coding environment where you can edit, build, and test the PR code, by just prepending "<a href="https://gitpod.io" target="_blank">gitpod.io#</a>" to the PR URL. Super useful for reviewing/testing stuff without having to check it out locally!
</p>
</>
),
twitterHandle: 'juliusvolz',
tweetId: '1298972181708275717'
},
{
name: 'David Ressler',
avatar: DavidRessler,
Expand Down
Binary file added src/resources/julius.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.