From 4d04070446888bacfde2b66053ed0ea452b22392 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 22 May 2021 14:26:02 +0100 Subject: [PATCH] Use tsx syntax in NavBar. --- src/elements/NavBar.ts | 74 ----------------------------------------- src/elements/NavBar.tsx | 62 ++++++++++++++++++++++++++++++++++ src/main.ts | 6 ++-- 3 files changed, 65 insertions(+), 77 deletions(-) delete mode 100644 src/elements/NavBar.ts create mode 100644 src/elements/NavBar.tsx diff --git a/src/elements/NavBar.ts b/src/elements/NavBar.ts deleted file mode 100644 index 032d0e8..0000000 --- a/src/elements/NavBar.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { ListItem } from "./ListItem"; -import { PageRouter } from "z-pagerouter"; -import { makeElement } from "z-makeelement"; -import i18next from "i18next"; - -export function NavBar(router: PageRouter): HTMLElement { - return makeElement({ - id: "navBar", - tag: "nav", - class: ["uk-navbar", "uk-navbar-container"], - children: [ - makeElement({ - tag: "div", - class: "uk-navbar-left", - children: makeElement({ - tag: "ul", - class: "uk-navbar-nav", - children: [ - ListItem( - makeElement({ - tag: "a", - text: i18next.t("home_btn"), - onclick: async () => { - await router.changePage("HOME"); - }, - }), - ), - ListItem( - makeElement({ - tag: "a", - text: i18next.t("back_btn"), - onclick: async () => { - await router.goBack(); - }, - }), - ), - ListItem( - makeElement({ - tag: "a", - text: i18next.t("refresh_btn"), - onclick: async () => { - await router.refresh(); - }, - }), - ), - ], - }), - }), - makeElement({ - tag: "div", - class: "uk-navbar-right", - children: makeElement({ - tag: "ul", - class: "uk-navbar-nav", - children: [ - ListItem( - makeElement({ - tag: "a", - text: i18next.t("me_btn"), - onclick: async () => { - await router.changePage("ME"); - }, - }), - ), - ], - }), - }), - ], - }); -} - -export function reloadNavBar(router: PageRouter): void { - document.querySelector("#navBar").replaceWith(NavBar(router)); -} diff --git a/src/elements/NavBar.tsx b/src/elements/NavBar.tsx new file mode 100644 index 0000000..8fcb043 --- /dev/null +++ b/src/elements/NavBar.tsx @@ -0,0 +1,62 @@ +import { JSX, render } from "preact"; +import { PageRouter } from "z-pagerouter"; +import i18next from "i18next"; + +export type NavBarProps = { + router: PageRouter; +}; + +export function NavBar(props: NavBarProps): JSX.Element { + return ( + + ); +} + +export function reloadNavBar(router: PageRouter): void { + render(, document.querySelector("#navBarBox")); +} diff --git a/src/main.ts b/src/main.ts index 75972d3..0119ca6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,7 +19,6 @@ Prism.highlightAll(); // Actual Imports -import { NavBar } from "./elements/NavBar"; import { PageRouter } from "z-pagerouter"; import { formatDistance } from "./formatDistance"; import { getSealStatus } from "./api/sys/getSealStatus"; @@ -27,6 +26,7 @@ import { makeElement } from "z-makeelement"; import { pageList } from "./allPages"; import { pageState } from "./globalPageState"; import { playground } from "./playground"; +import { reloadNavBar } from "./elements/NavBar"; import i18next from "i18next"; // @ts-ignore @@ -34,7 +34,7 @@ import translations from "./translations/index.mjs"; async function onLoad(): Promise { document.body.innerHTML = ""; - document.body.appendChild(makeElement({ tag: "div", id: "navBarReplace" })); + document.body.appendChild(makeElement({ tag: "div", id: "navBarBox" })); document.body.appendChild( makeElement({ tag: "div", @@ -65,7 +65,7 @@ async function onLoad(): Promise { document.getElementById("pageTitle"), ); - document.querySelector("#navBarReplace").replaceWith(NavBar(pageRouter)); + reloadNavBar(pageRouter); pageRouter.addEventListener("pageChanged", async function (_) { pageState.currentPage = await pageRouter.getCurrentPageID();