You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,6 +80,7 @@ projects that use wouter: **[Ultra](https://ultrajs.dev/)**,
80
80
-[Are relative routes and links supported?](#are-relative-routes-and-links-supported)
81
81
-[Can I initiate navigation from outside a component?](#can-i-initiate-navigation-from-outside-a-component)
82
82
-[Can I use _wouter_ in my TypeScript project?](#can-i-use-wouter-in-my-typescript-project)
83
+
-[How can add animated route transitions?](#how-can-add-animated-route-transitions)
83
84
-[Preact support?](#preact-support)
84
85
-[Server-side Rendering support (SSR)?](#server-side-rendering-support-ssr)
85
86
-[How do I configure the router to render a specific route in tests?](#how-do-i-configure-the-router-to-render-a-specific-route-in-tests)
@@ -704,6 +705,52 @@ It's the same function that is used internally.
704
705
Yes! Although the project isn't written in TypeScript, the type definition files are bundled with
705
706
the package.
706
707
708
+
### How can add animated route transitions?
709
+
710
+
Let's take look at how wouter routes can be animated with [`framer-motion`](framer.com/motion).
711
+
Animating enter transitions is easy, but exit transitions require a bit more work. We'll use the `AnimatePresence` component that will keep the page in the DOM until the exit animation is complete.
712
+
713
+
Unfortunately, `AnimatePresence` only animates its **direct children**, so this won't work:
{/* This will not work! `motion.div` is not a direct child */}
721
+
<Route path="/">
722
+
<motion.div
723
+
initial={{ opacity:0 }}
724
+
animate={{ opacity:1 }}
725
+
exit={{ opacity:0 }}
726
+
/>
727
+
</Route>
728
+
</AnimatePresence>
729
+
);
730
+
```
731
+
732
+
The workaround is to match this route manually with `useRoute`:
733
+
734
+
```jsx
735
+
exportconstMyComponent= ({ isVisible }) => {
736
+
const [isMatch] =useRoute("/");
737
+
738
+
return (
739
+
<AnimatePresence>
740
+
{isMatch && (
741
+
<motion.div
742
+
initial={{ opacity:0 }}
743
+
animate={{ opacity:1 }}
744
+
exit={{ opacity:0 }}
745
+
/>
746
+
)}
747
+
</AnimatePresence>
748
+
);
749
+
};
750
+
```
751
+
752
+
More complex examples involve using `useRoutes` hook (similar to how React Router does it), but wouter does not ship it out-of-the-box. Please refer to [this issue](https://github.com/molefrog/wouter/issues/414#issuecomment-1954192679) for the workaround.
753
+
707
754
### Preact support?
708
755
709
756
Preact exports are available through a separate package named `wouter-preact` (or within the
0 commit comments