Skip to content

Instantly share code, notes, and snippets.

@mattgperry
Created October 29, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattgperry/6e4315f402bd65356e29054cf32c22a4 to your computer and use it in GitHub Desktop.
Save mattgperry/6e4315f402bd65356e29054cf32c22a4 to your computer and use it in GitHub Desktop.
TextSplit
'The animator’s JavaScript toolbox.'.split('').map((character, i) => (
<TaglineCharacter index={i} key={i} character={character} />
))
function TaglineCharacter({ character, index }) {
const ref = useRef(null);
useEffect(() => {
const controls = animate({
from: 0,
to: 0,
velocity: -500,
stiffness: 120,
elapsed: -index * 20,
onUpdate: (y) =>
(ref.current.style.transform = `translateY(${y}px) translateZ(0)`),
});
return () => controls.stop();
}, []);
return (
<span
ref={ref}
className="hl"
style={{
display: 'inline-block',
width: character === ' ' ? 8 : 'auto',
}}
>
{character}
</span>
);
}
@mattgperry
Copy link
Author

This is with Popmotion but you can imagine a similar approach ditching the useEffect and using motion.span instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment