Define a custom enter and exit transition
The toast relies on react-transition-group
for the enter and exit transition. Any transition built with react-transition-group should work!
I'll use the zoom animation from animate.css to build a custom transition
Using the cssTransition helper
The easiest way to roll your own transition is by using the cssTransition
helper. Doing so you don't need to deal with react-transition-group
. You only need to provide the enter
and the exit
class name, the transition duration
is set
to 750ms
by default but it can be overridden.
The cssTransition
will also take care to collapse the toast when they exited.
Different duration for enter and exit
If you want the transition duration to be different between the enter and exit transition pass an array:
Handle transition based on the toast position
Some transitions are based on the toast position. This is the case for the default one. If you pass appendPosition
to the cssTransition
helper as shown below, the current position will be appended to the enter
and exit
class name:
Important
Don't forget to add the position as well when you write your css animations
Prevent the toast from collapsing after the exit animation
By default, the remaining toast will collapse smoothly
This can be disabled as well:
Tweak collapse duration
The default duration is 300ms. This is also easy to change 💪
Create a transition with the Transition component
You can also use the Transition
component from react-transition-group if you want more control.
When you create your own transition the following props are passed:
- in: this tell if the component is visible or not (same as react-transition-group)
- appear: this tell to animate the component when it's visible (same as react-transition-group)
- nodeRef: the node ref.(same as react-transition-group)
- done: this callback should be called at the end of your transition
- position: the toast position
- preventExitTransition: this will be true if the toast has been closed by a drag event
In the example below we will reimplement the cssTransition
helper.
This can seem intimidating but if you know react-transition-group, this is really straightforward.
Of course, you could also the CSSTransition component if you wish.