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
onChangeevent of thepreact-routerand set theoutElto a div that wrapspreviousEland has a class that will animate the exit of the route, then add aonAnimationEndlistener so that you can set theoutEltonullone the out animation has finished, last thing you need to set thepreviousEltoe.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.sassexample below.here is the full example:
app.tsx
app.sass