[...]
const SlideAnimation: Component<{
component: Component,
direction: "left" | "right"
}> = (props) => (
<Motion
initial={{transform: props.direction === "left" ? "translateX(-100%)" : "translateX(100%)"}}
animate={{transform: "translateX(0%)"}}
exit={{transform: props.direction === "left" ? "translateX(100%)" : "translateX(-100%)"}}
transition={{duration: 0.3}}
style="position: absolute; width: 100%; height: 100%;"
>
<props.component/>
</Motion>
)
const Layout: Component = () => {
const [previousPath, setPreviousPath] = createSignal<string>("/");
const [slideDirection, setSlideDirection] = createSignal<"left" | "right">("right");
const currentPath = () => useLocation().pathname;
function calcDirection() {
const normalizedCurrentPath = currentPath().replace(/\/$/, "");
const normalizedPreviousPath = previousPath().replace(/\/$/, "");
const depth = normalizedCurrentPath.split("/").length - normalizedPreviousPath.split("/").length;
setSlideDirection(depth < 0 ? "right" : "left");
}
createEffect(() => {
if (currentPath() !== previousPath()) {
calcDirection();
setPreviousPath(currentPath());
}
})
return (
<div>
<Presence initial={false}>
<Switch>
<Match when={currentPath() === "/"}>
<SlideAnimation component={Home} direction={slideDirection()}/>
</Match>
<Match when={currentPath() === "/about"}>
<SlideAnimation component={About} direction={slideDirection()}/>
</Match>
</Switch>
</Presence>
</div>
);
}
const App = () => (
<HashRouter root={Layout}/>
);
render(() => <App/>, document.getElementById("root")!);