+
);
@@ -36,7 +37,7 @@ function MenuButton(props : { active: boolean, setActive: (_:boolean) => void })
function Title() {
return (
-
+
Tymur Arsentiev
);
diff --git a/src/nav/nav.scss b/src/nav/nav.scss
index 321726d..c9e39f5 100644
--- a/src/nav/nav.scss
+++ b/src/nav/nav.scss
@@ -1,11 +1,11 @@
$padding: 15px;
$menu: #282c34;
-.click:hover {
+.menu-btn:hover {
opacity: 90%;
}
-.click:active {
+.menu-btn:active {
opacity: 80%;
}
@@ -25,7 +25,6 @@ $menu: #282c34;
left: 50%;
transform: translateX(-50%);
- color: white;
}
.header {
@@ -40,7 +39,6 @@ $menu: #282c34;
.name {
align-self: center;
flex-grow: 1;
- text-decoration: underline;
}
.wave {
position: absolute;
diff --git a/src/router/Link.tsx b/src/router/Link.tsx
index 6bcbc57..33d1897 100644
--- a/src/router/Link.tsx
+++ b/src/router/Link.tsx
@@ -1,7 +1,10 @@
+import "./link.scss";
+
import React, {PropsWithChildren} from "react";
import {useRouter} from "./RouterContext";
+import {Path} from "./router";
-export function Link(props: {className: string, href: string, replace?: boolean } & PropsWithChildren) {
+export function Link(props: {className: string, href: Path, replace?: boolean } & PropsWithChildren) {
const {children, className, href, replace} = props;
const router = useRouter();
@@ -14,5 +17,5 @@ export function Link(props: {className: string, href: string, replace?: boolean
}
}
- return
{children}
+ return
{children}
}
\ No newline at end of file
diff --git a/src/router/Route.tsx b/src/router/Route.tsx
index e537e74..70a93de 100644
--- a/src/router/Route.tsx
+++ b/src/router/Route.tsx
@@ -6,7 +6,5 @@ export function Route(props: PropsWithChildren & {path: Path}) {
const {path, children} = props;
const router = useRouter();
- return <>
- { router.current?.currentPage === path && children}
- >
+ return router.current === path ? <>{children}> : <>>
}
\ No newline at end of file
diff --git a/src/router/RouterContext.tsx b/src/router/RouterContext.tsx
index 6e70fbf..0c03ea9 100644
--- a/src/router/RouterContext.tsx
+++ b/src/router/RouterContext.tsx
@@ -1,62 +1,36 @@
-import {createContext, PropsWithChildren, useContext, useEffect, useSyncExternalStore} from "react";
-import {HistoryState, Path, Router} from "./router";
-import * as repl from "node:repl";
-
-const DEFAULT : Router = {
- current: {
- currentPage: window.location.pathname as Path
- },
- pushPage: function(){},
- replacePage: function(){}
-};
+import {createContext, PropsWithChildren, useContext, useEffect, useState, useSyncExternalStore} from "react";
+import {DEFAULT, Path, Router} from "./router";
const RouterContext = createContext
(DEFAULT);
-
export function useRouter() {
return useContext(RouterContext);
}
-function subscribe(onChange: () => void) {
- window.addEventListener("popstate", onChange);
-
- // return unsubscribe function
- return function() {
- window.removeEventListener("popstate", onChange);
- }
-}
-
-function getSnapshot(): HistoryState {
- return window.history.state ?? DEFAULT;
-}
-
-function pushState(path: string) {
- window.history.pushState({
- currentPage: path as Path
- },
- "",
- path);
-}
-
-function replaceState(path: string) {
- window.history.replaceState({
- currentPage: path as Path
- },
- "",
- path);
-}
export function RouterProvider(props : PropsWithChildren) {
- const state = useSyncExternalStore(subscribe, getSnapshot);
+ // path is replaced on every state push/replace/pop
+ const [path, setPath] = useState(window.location.pathname as Path);
- // start history with initial page
+ // handle the back/forward button
useEffect(() => {
- replaceState(window.location.pathname);
+ function subscribe(ev: PopStateEvent) {
+ setPath(ev.state as Path);
+ }
+
+ window.addEventListener("popstate", subscribe);
+ return () => window.removeEventListener("popstate", subscribe);
}, []);
return
{props.children}
diff --git a/src/router/link.scss b/src/router/link.scss
new file mode 100644
index 0000000..8d43b38
--- /dev/null
+++ b/src/router/link.scss
@@ -0,0 +1,12 @@
+.link {
+ color: white;
+ text-decoration: underline;
+}
+
+.link:hover {
+ opacity: 90%;
+}
+
+.link:active {
+ opacity: 80%;
+}
\ No newline at end of file
diff --git a/src/router/router.ts b/src/router/router.ts
index 24ae0d8..8c6b48f 100644
--- a/src/router/router.ts
+++ b/src/router/router.ts
@@ -1,10 +1,13 @@
export type Router = {
- replacePage: (path: string) => void;
- pushPage: (path: string) => void;
- current: HistoryState
+ replacePage: (path: Path) => void;
+ pushPage: (path: Path) => void;
+ current: Path
}
export type Path = `/${string}`;
-export type HistoryState = {
- currentPage: Path
-};
+
+export const DEFAULT : Router = {
+ current: window.location.pathname as Path,
+ pushPage: function(){},
+ replacePage: function(){}
+};
\ No newline at end of file