I'm trying to get a page transition using preact-router
, i already tried the preact-transition-group
package with the preact-css-transition-group
package, that gives me an error, anyways here is my basic setup:
import { h, FunctionComponent } from 'preact';
import Router from 'preact-router';
const App: FunctionComponent = () => {
return (
<Router>
<div path="/1" style="padding: 50px">
page 1 <a href="/2">page 2</a>
</div>
<div path="/2" style="padding: 50px">
page 2 <a href="/1">page 1</a>
</div>
</Router>
);
};
a typescript and/or preact-router
solution is preferable but not necessary.
With this approach you need to wrap every route in a div with a class that will animate the route and set the key and path attribute to the path of the route like this:
then you need to add two reactive variables like this:
after that you can listen to the
onChange
event of thepreact-router
and set theoutEl
to a div that wrapspreviousEl
and has a class that will animate the exit of the route, then add aonAnimationEnd
listener so that you can set theoutEl
tonull
one the out animation has finished, last thing you need to set thepreviousEl
toe.current.props.children
, your router component listener should look like this:last thing you need to do is to create the animations, look at the
app.sass
example below.here is the full example:
app.tsx
app.sass