Skip to content

Commit

Permalink
React style NavBar
Browse files Browse the repository at this point in the history
  • Loading branch information
rxqd committed Apr 1, 2024
1 parent d4eae91 commit eb15d86
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 78 deletions.
25 changes: 14 additions & 11 deletions src/node/react/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
<html lang="en" data-theme="mine">

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React App</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>

</html>
1 change: 1 addition & 0 deletions src/node/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/react-dom": "^18.2.15",
"@vitejs/plugin-react": "^4.2.0",
"autoprefixer": "^10.4.19",
"daisyui": "^4.9.0",
"postcss": "^8.4.38",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
17 changes: 17 additions & 0 deletions src/node/react/src/components/NavLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Link } from "@tanstack/react-router";

type NavLinkProps = {
children: React.ReactNode,
to: string
}

export default function NavLink({ children, to }: NavLinkProps) {
return (
<Link
activeProps={{
className: "bg-accent"
}}
className="btn btn-ghost text-xl"
to={to}>{children}</Link>
)
}
77 changes: 38 additions & 39 deletions src/node/react/src/components/TicTacToe/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import Board from "./Board";
import {useState} from "react";
import {type SquareValue} from "./types";
import { useState } from "react";
import { type SquareValue } from "./types";

export default function Game() {
const size = 3;
const initSquares: SquareValue[] = Array(size*size).fill(null);

const [history, setHistory] = useState([initSquares]);
const [currentMove, setCurrentMove] = useState(0);
const size = 3;
const initSquares: SquareValue[] = Array(size * size).fill(null);

const currentSquares = history[currentMove];
const xnext = currentMove % 2 === 0;
const [history, setHistory] = useState([initSquares]);
const [currentMove, setCurrentMove] = useState(0);

const handlePlay = (squares: SquareValue[]) => {
const nextHistory = [...history.slice(0, currentMove + 1), squares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
};
const currentSquares = history[currentMove];
const xnext = currentMove % 2 === 0;

const jumpTo = (move: number) => {
setCurrentMove(move);
const handlePlay = (squares: SquareValue[]) => {
const nextHistory = [...history.slice(0, currentMove + 1), squares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
};

const jumpTo = (move: number) => {
setCurrentMove(move);
}

const moves = history.map((_, move) => {
let description;
if (move > 0) {
description = 'Go to move #' + move;
} else {
description = 'Go to game start';
}
return (
<li>
<button onClick={() => jumpTo(move)}>{description}</button>
</li>
);
});

const moves = history.map((_, move) => {
let description;
if (move > 0) {
description = 'Go to move #' + move;
} else {
description = 'Go to game start';
}
return (
<li>
<button onClick={() => jumpTo(move)}>{description}</button>
</li>
);
});

return (
<>
<h2>Tic Tac Toe game</h2>
<div className="game">
<div className="game-board">
<Board size={size} squares={currentSquares} xnext={xnext} onPlay={handlePlay} />
</div>
<div className="game-info">
<ol>{moves}</ol>
<div className="game">
<div className="game-board">
<Board size={size} squares={currentSquares} xnext={xnext} onPlay={handlePlay} />
</div>
<div className="game-info">
<ol>{moves}</ol>
</div>
</div>
</div>
</>
);
}
}
9 changes: 6 additions & 3 deletions src/node/react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import { RouterProvider, createRouter } from '@tanstack/react-router';

import { routeTree } from '@/routeTree.gen';

const router = createRouter({ routeTree });
const router = createRouter({
routeTree,
defaultPreload: 'intent',
})

declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
};

ReactDOM.createRoot(document.getElementById('root')).render(
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
<RouterProvider router={router} />
</React.StrictMode>
)
11 changes: 5 additions & 6 deletions src/node/react/src/pages/SudokuGamePage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export default function SudokuGamePage() {
return (
<div className="container">
<h1>Sudoku game</h1>
</div>
)
}
return (
<div className="container">
</div>
)
}
36 changes: 26 additions & 10 deletions src/node/react/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { createRootRoute, Link, Outlet } from "@tanstack/react-router";
import { createRootRoute, Outlet } from "@tanstack/react-router";
import { TanStackRouterDevtools } from "@tanstack/router-devtools";

import NavLink from "@/components/NavLink";

export const Route = createRootRoute({
component: () => (
<>
<Link to="/">TicTacToe</Link>
<Link to="/sudoku">Sudoku</Link>
<hr />
<Outlet />
<TanStackRouterDevtools />
</>
),
component: Root,
notFoundComponent: NotFound
});

function NotFound() {
return (
<p>Not Found (on root route)</p>
)
}

function Root() {
return (
<>
<div className="navbar bg-neutral text-blue-200">
<NavLink to="/">TicTacToe</NavLink>
<NavLink to="/sudoku">Sudoku</NavLink>
</div >

<hr />
<Outlet />
<TanStackRouterDevtools />
</>
)
}
42 changes: 35 additions & 7 deletions src/node/react/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{ts,tsx}",
],
theme: {
extend: {},
content: [
"./index.html",
"./src/**/*.{ts,tsx}",
],
plugins: [require("daisyui")],
daisyui: {
themes: [
{
mine: {
"primary": "#155e75",
"secondary": "#0891b2",
"accent": "#0069a6",
"neutral": "#164e63",
"base-100": "#e7e5e4",
"info": "#0d9488",
"success": "#155e75",
"warning": "#fde047",
"error": "#b91c1c",
},
mine_dark: {
"primary": "#155e75",
"secondary": "#0891b2",
"accent": "#0069a6",
"neutral": "#164e63",
"base-100": "#20161f",
"info": "#0d9488",
"success": "#155e75",
"warning": "#fde047",
"error": "#b91c1c",
},

},
"coffee",
"dark"
],
},
plugins: [],
}

41 changes: 39 additions & 2 deletions src/node/react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,16 @@ __metadata:
languageName: node
linkType: hard

"css-selector-tokenizer@npm:^0.8":
version: 0.8.0
resolution: "css-selector-tokenizer@npm:0.8.0"
dependencies:
cssesc: "npm:^3.0.0"
fastparse: "npm:^1.1.2"
checksum: 10c0/86f68cc666d41f9d153351677694002a9d00e2609e6abc66fcfd7f580be3d6ecc0929e46a06c621ab28da5febbb54567db9709b819414edae4a36d9ff9133e16
languageName: node
linkType: hard

"cssesc@npm:^3.0.0":
version: 3.0.0
resolution: "cssesc@npm:3.0.0"
Expand All @@ -1283,6 +1293,25 @@ __metadata:
languageName: node
linkType: hard

"culori@npm:^3":
version: 3.3.0
resolution: "culori@npm:3.3.0"
checksum: 10c0/acd330a8f940dc22f36650ba722c9302a4745fee3c8fb40a27ac27228767e2cba4221c2dc414fce652b82cb264d28f65d8b76d505df402ecf94dbb9e440319e1
languageName: node
linkType: hard

"daisyui@npm:^4.9.0":
version: 4.9.0
resolution: "daisyui@npm:4.9.0"
dependencies:
css-selector-tokenizer: "npm:^0.8"
culori: "npm:^3"
picocolors: "npm:^1"
postcss-js: "npm:^4"
checksum: 10c0/4845309413d934174ffb55163b6045f10cc0d381f1528d532a70f7de72a7a0c7e69eb119a93e800927012b4062a0acc83006b76c89512e86aae7bbd43b7346f5
languageName: node
linkType: hard

"date-fns@npm:^2.29.1":
version: 2.30.0
resolution: "date-fns@npm:2.30.0"
Expand Down Expand Up @@ -1483,6 +1512,13 @@ __metadata:
languageName: node
linkType: hard

"fastparse@npm:^1.1.2":
version: 1.1.2
resolution: "fastparse@npm:1.1.2"
checksum: 10c0/c08d6e7ef10c0928426c1963dd4593e2baaf44d223ab1e5ba5d7b30470144b3a4ecb3605958b73754cea3f857ecef00b67c885f07ca2c312b38b67d9d88b84b5
languageName: node
linkType: hard

"fastq@npm:^1.6.0":
version: 1.17.1
resolution: "fastq@npm:1.17.1"
Expand Down Expand Up @@ -2152,7 +2188,7 @@ __metadata:
languageName: node
linkType: hard

"picocolors@npm:^1.0.0":
"picocolors@npm:^1, picocolors@npm:^1.0.0":
version: 1.0.0
resolution: "picocolors@npm:1.0.0"
checksum: 10c0/20a5b249e331c14479d94ec6817a182fd7a5680debae82705747b2db7ec50009a5f6648d0621c561b0572703f84dbef0858abcbd5856d3c5511426afcb1961f7
Expand Down Expand Up @@ -2193,7 +2229,7 @@ __metadata:
languageName: node
linkType: hard

"postcss-js@npm:^4.0.1":
"postcss-js@npm:^4, postcss-js@npm:^4.0.1":
version: 4.0.1
resolution: "postcss-js@npm:4.0.1"
dependencies:
Expand Down Expand Up @@ -2334,6 +2370,7 @@ __metadata:
"@types/react-dom": "npm:^18.2.15"
"@vitejs/plugin-react": "npm:^4.2.0"
autoprefixer: "npm:^10.4.19"
daisyui: "npm:^4.9.0"
postcss: "npm:^8.4.38"
react: "npm:^18.2.0"
react-dom: "npm:^18.2.0"
Expand Down

0 comments on commit eb15d86

Please sign in to comment.