1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* @jsxImportSource https://esm.sh/hono/jsx */
import { Hono } from "npm:hono";
import { html } from "npm:hono/html";
import type { Child, FC } from "npm:hono/jsx";
import { jsxRenderer, useRequestContext } from "npm:hono/jsx-renderer";
const app = new Hono();
// Components
/**
* Nav component that uses `hx-boost` toplevel links targeting `main`.
*
* @remarks
* hx-boost links targeting `main` are handled automatically by the
* layout middleware.
*/
const Nav: FC = () => {
return (
<div id="menu" hx-boost="true" hx-target="#main">
<nav>
<a href="/">Home</a>
<a href="/test_page">Test Page</a>
</nav>
</div>
);
};
// Layout
/**
* Layout component that wraps children in a `main` tag.
*/
const Main: FC<{ children: Child }> = ({ children }) => {
return (
<main id="main" class="h-full">
{children}
</main>
);
};
/**
* Layout component that wraps children in a full html page.
*/
const Page: FC<{ children: Child }> = ({ children }) => {
return (
<html>
<head>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
</meta>
<script type="module" src="https://cdn.skypack.dev/twind/shim"></script>
<script src="https://unpkg.com/htmx.org@1.9.9"></script>
<script type="module" src="https://esm.town/v/saolsen/test_game_browser" defer></script>
<script type="module" src="https://esm.town/v/saolsen/test_game_server" defer></script>
</head>
<body class="h-screen">
<Main children={children}></Main>
</body>
</html>
);
};
/**
* Conditional layout component based on the hx-target header.
*
* @remarks
* Check the `hx-target` header to see if htmx made the request and is
* expecting a partial to swap in.
* This app uses boosted links that target `main`. That way we can keep all
* the javascript and the nav bar loaded. The `main` target is handled here.
* If the `hx-target` is anything else we return the inner content.
* If there is no `hx-target` header then this is a fresh page load and we
* return the whole layout.
*/
const Layout: FC<{ children: Child }> = ({ children }) => {
const c = useRequestContext();
const hx_target = c.req.header("hx-target");
switch (hx_target) {
case undefined: {
return <Page children={children}></Page>;
}
case "main": {
return <Main children={children}></Main>;
}
default: {
return <>{children}</>;
}
}
};
app.use("*", jsxRenderer(({ children }) => <Layout children={children}></Layout>));
app.get("/", (c) => {
return c.render(
<div class="flex flex-col h-full">
<div class="flex-grow flex items-center justify-center">
<div id="wrapper" class="w-full h-full overflow-hidden flex items-center justify-center">