Skip to content

Clean up useEffect animations (Gatsby) #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 16 additions & 9 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const IndexPage = () => {
useEffect( () => {
let i = 0
let forward = true
let timer

const showResponse = num => {
document.getElementById( 'r1' ).style.display = num === 1 ? 'block' : 'none'
Expand All @@ -104,33 +105,35 @@ const IndexPage = () => {
if ( i === 20 ) {
forward = false
showResponse( 3 )
setTimeout( type, 1500 )
timer = setTimeout( type, 1500 )
} else if ( i === 11 ) {
showResponse( 2 )
setTimeout( type, 1500 )
timer = setTimeout( type, 1500 )
} else {
setTimeout( type, Math.random() * 180 + 70 )
timer = setTimeout( type, Math.random() * 180 + 70 )
}
} else {
i -= 1
document.getElementById( `ch${i}` ).style.display = 'none'
if ( i === 0 ) {
forward = true
showResponse( 1 )
setTimeout( type, 2000 )
timer = setTimeout( type, 2000 )
} else {
setTimeout( type, 80 )
timer = setTimeout( type, 80 )
}
}
}
setTimeout( type, 2000 )
timer = setTimeout( type, 2000 )
return () => clearTimeout( timer )
}, [] )

// Illustration of a type IDL following a query by line
useEffect( () => {
const typeHighlight = document.getElementById( 'type-highlight' )
const queryHighlight = document.getElementById( 'query-highlight' )
let line = 0
let timer

const typeLines = [ 2, 6, 7, 6, 8, 13, 14, 9, 18, 19, 20, 13 ]
const queryLines = [ 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14 ]
Expand All @@ -139,10 +142,11 @@ const IndexPage = () => {
typeHighlight.style.top = `${17 * typeLines[ line ] - 9}px`
queryHighlight.style.top = `${17 * queryLines[ line ] - 9}px`
line = ( line + 1 ) % typeLines.length
setTimeout( highlightLine, 800 + Math.random() * 200 )
timer = setTimeout( highlightLine, 800 + Math.random() * 200 )
}

highlightLine()
return () => clearTimeout( timer )
}, [] )

// Illustration showing more legs added to a graph? Or a type evolving over time?
Expand All @@ -151,23 +155,26 @@ const IndexPage = () => {
const inView = document.getElementById( 'typeEvolveView' )
inView.className = `step${i}`

setInterval( () => {
const interval = setInterval( () => {
i = ( i + 1 ) % 7
inView.className = `step${i}`
}, 2200 )
return () => clearInterval( interval )
}, [] )

// Illustration of each field becoming a function?
useEffect( () => {
let i = 0
let timer
const inView = document.getElementById( 'leverageCodeView' )
const delayBefore = [ 800, 1800, 1200, 3000, 3000, 3000 ]
const step = () => {
inView.className = `step${i}`
i = ( i + 1 ) % 6
setTimeout( step, delayBefore[ i ] )
timer = setTimeout( step, delayBefore[ i ] )
}
step()
return () => clearTimeout( timer )
}, [] )

return (
Expand Down