I recently created a new page on this website. From an early time in my front-end journey, I have been amazed by the websites that are featured on awwwards and I wanted to create something fancy like them.
Here's how I did it.
What we want to build
Let's take a look again at what we're trying to build here:
Hello there!
Changing colors
We can animate the color changing of the text by using transition:
And since I'm using styled-components, we can put this in a re-usable CSS string:
But this doesn't do anything at the moment — to actually change the color, we will need to update the color of the <span> that we are targeting after it has been rendered to the screen.
Let's start writing our little wrapper component for this:
And now we change the color once the component has mounted:
Hello there!
Huh, that didn't seem to work out 🤔 but that's because the transition already ran since the above live example was already "mounted" by the time you scrolled down to it.
A wild custom hook appeared!
To keep changing the color after a pre-defined interval, we'll be defining our very own useInterval custom hook:
Let's now use this hook to change the color of our text just as it completes its transition.
Hello there
Voila! ✨
There we go. What's great is that you can also use this to highlight link, like so:
I'm really glad with how the effect turned out, and it was also easier to accomplish because I was using CSS-in-JS. It's great to see how powerful tools like styled-components can be!
You can check the effect out in action and see it in all its glory 😄