1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-19 08:28:09 +00:00
quot/app/src/app.tsx

39 lines
977 B
TypeScript
Raw Normal View History

2022-08-23 09:01:47 +09:00
import { createSignal } from "solid-js";
import Index from "./pages/index";
import Page from "./pages/page";
const routes = {
"/": Index,
};
export default () => {
const [pathname, setPathname] = createSignal(
document.location.pathname as keyof typeof routes
);
document.body.addEventListener("click", (e) => {
if (
e.target instanceof HTMLAnchorElement &&
e.target.origin === document.location.origin &&
e.target.pathname in routes // TODO: support params ... solid router を入れよう
) {
e.preventDefault();
window.history.pushState({}, "", e.target.href);
setPathname(e.target.pathname as keyof typeof routes);
}
});
window.addEventListener("popstate", () => {
setPathname(document.location.pathname as keyof typeof routes);
});
return () => (
<>
<header>
<h1>Quot</h1>
</header>
{routes[pathname()] ?? <Page id={Number(pathname().slice(1))} />}
</>
);
};