Control a CSS animation with XState
The Notification Center example takes inspiration from the React Toastify library.
When you trigger a notification with a timeout with React Toastify, it displays a progress bar at the bottom of the notification. When you hover the notification, the progress bar and the timer are paused, as shown below:
React Toastify uses a CSS animation,
defined with a @keyframes
.
The Notification Center does too:
The animation is applied to the progress bar with CSS:
React Toastify’s implementation’s beauty is using a CSS animation as a resumable timer.
A CSS animation can be paused with the animation-play-state
property.
The Notification Center uses the current state of the machine to determine when to stop the animation:
We can know when the animation ends thanks to the animationend
event received by the animated element and close the notification:
The Notification Center example could have used a JavaScript timer, but it would have involved more code and increased complexity. Relying on the browser led to a simple and robust solution. The animation can even be stopped in the Dev Tools without causing any bugs.
Check out the Notification Center example to play with the animation. Hover over it to pause the animation.
Get news from XState by Example
Sign up for the newsletter to be notified when more machines or an interactive tutorial are released. I respect your privacy and will only send emails once in a while.