Skip to content

Instantly share code, notes, and snippets.

@JamesGelok
Created January 16, 2020 09:38
Show Gist options
  • Save JamesGelok/4fd7c63131344f8f2b9697801b70b7cb to your computer and use it in GitHub Desktop.
Save JamesGelok/4fd7c63131344f8f2b9697801b70b7cb to your computer and use it in GitHub Desktop.
React Spring - Messages w/ Next
import React, { useState, useEffect } from 'react'
import { render } from 'react-dom'
import { useTrail, animated } from 'react-spring'
import './styles.css'
const config = { mass: 5, tension: 2000, friction: 200 }
function Message({ items, next: Next }) {
const [toggle, set] = useState(false);
const [showNext, setShowNext] = useState(false);
const trail = useTrail(items.length, {
config,
opacity: toggle ? 1 : 0,
x: toggle ? 0 : 20,
height: toggle ? 80 : 0,
from: { opacity: 0, x: 20, height: 0 },
})
useEffect(() => {
setTimeout(() => set(true), 1000);
setTimeout(() => set(false), 2000);
setTimeout(() => Next && setShowNext(true), 3000);
}, []);
if (showNext) return <Next />;
return (
<div className="trails-main" onClick={() => set(state => !state)}>
<div>
{trail.map(({ x, height, ...rest }, index) => (
<animated.div
key={items[index]}
className="trails-text"
style={{ ...rest, transform: x.interpolate(x => `translate3d(0,${x}px,0)`) }}>
<animated.div style={{ height }}>{items[index]}</animated.div>
</animated.div>
))}
</div>
</div>
)
}
render(<Message items={['Nice', 'Job!']} next={() => <Message items={['See ya', 'next time!', ':)']} />} />, document.getElementById('root'))
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, helvetica neue, helvetica, ubuntu, roboto, noto,
segoe ui, arial, sans-serif;
background: transparent;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
background-color: #f0f0f0;
}
#root {
width: 100%;
height: 100%;
}
.trails-main {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.trails-text {
position: relative;
width: 100%;
height: 80px;
line-height: 80px;
color: palevioletred;
font-size: 5em;
font-weight: 800;
text-transform: uppercase;
will-change: transform, opacity;
overflow: hidden;
}
.trails-text > div {
overflow: hidden;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment