Skip to content

Commit aa37968

Browse files
authored
Merge pull request #20 from nyaomaru/deno-study-14
study: client only side component
2 parents 5d17ed3 + e7446ab commit aa37968

File tree

15 files changed

+108
-23
lines changed

15 files changed

+108
-23
lines changed

src/components/MapComponent.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useContext, useEffect } from "preact/hooks";
2+
import { LeafletContext } from "#src/providers/LeafletProvider.tsx";
3+
4+
export function MapComponent() {
5+
const leaf = useContext(LeafletContext);
6+
if (!leaf) return <div>Component placeholder</div>;
7+
useEffect(() => {
8+
const map = leaf.map("map").setView(leaf.latLng(0, 0), 2);
9+
leaf.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
10+
attribution:
11+
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
12+
}).addTo(map);
13+
});
14+
return <div id="map" class="relative w-[80vw] h-[50vh]" />;
15+
}

src/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env -S deno run -A --watch=static/,routes/
22

33
import dev from "$fresh/dev.ts";
4-
import config from "./fresh.config.ts";
4+
import config from "#src/fresh.config.ts";
55

66
import "$std/dotenv/load.ts";
77

src/fresh.gen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as $countdown from "./routes/countdown.tsx";
1919
import * as $greet_name_ from "./routes/greet/[name].tsx";
2020
import * as $greet_middleware from "./routes/greet/_middleware.ts";
2121
import * as $index from "./routes/index.tsx";
22+
import * as $map from "./routes/map.tsx";
2223
import * as $partials_about_id_ from "./routes/partials/about/[id].tsx";
2324
import * as $projects_id_ from "./routes/projects/[id].tsx";
2425
import * as $reportHandler from "./routes/reportHandler.ts";
@@ -27,6 +28,7 @@ import * as $subscribe from "./routes/subscribe.tsx";
2728
import * as $Chart from "./islands/Chart.tsx";
2829
import * as $Countdown from "./islands/Countdown.tsx";
2930
import * as $Counter from "./islands/Counter.tsx";
31+
import * as $MapIsland from "./islands/MapIsland.tsx";
3032
import * as $NextContentButton from "./islands/NextContentButton.tsx";
3133
import { type Manifest } from "$fresh/server.ts";
3234

@@ -49,6 +51,7 @@ const manifest = {
4951
"./routes/greet/[name].tsx": $greet_name_,
5052
"./routes/greet/_middleware.ts": $greet_middleware,
5153
"./routes/index.tsx": $index,
54+
"./routes/map.tsx": $map,
5255
"./routes/partials/about/[id].tsx": $partials_about_id_,
5356
"./routes/projects/[id].tsx": $projects_id_,
5457
"./routes/reportHandler.ts": $reportHandler,
@@ -59,6 +62,7 @@ const manifest = {
5962
"./islands/Chart.tsx": $Chart,
6063
"./islands/Countdown.tsx": $Countdown,
6164
"./islands/Counter.tsx": $Counter,
65+
"./islands/MapIsland.tsx": $MapIsland,
6266
"./islands/NextContentButton.tsx": $NextContentButton,
6367
},
6468
baseUrl: import.meta.url,

src/islands/Counter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Signal } from "@preact/signals";
22
import { ComponentChildren } from "preact";
3-
import { Button } from "../components/Button.tsx";
4-
import { Card } from "../components/Card.tsx";
3+
import { Button } from "#src/components/Button.tsx";
4+
import { Card } from "#src/components/Card.tsx";
55

66
interface Props {
77
count: Signal<number>;

src/islands/MapIsland.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { MapComponent } from "#src/components/MapComponent.tsx";
2+
import { LeafletProvider } from "#src/providers/LeafletProvider.tsx";
3+
4+
export function MapIsland() {
5+
return (
6+
<LeafletProvider>
7+
<MapComponent />
8+
</LeafletProvider>
9+
);
10+
}

src/providers/LeafletProvider.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as Leaflet from "https://esm.sh/v135/@types/leaflet@1.9.4/index.d.ts";
2+
import { IS_BROWSER } from "$fresh/runtime.ts";
3+
import { useState } from "preact/hooks";
4+
import { ComponentChildren, createContext } from "preact";
5+
6+
// Create a context to hold Leaflet data/functions
7+
export const LeafletContext = createContext<typeof Leaflet | null>(null);
8+
9+
// LeafletProvider component manages Leaflet loading and context
10+
export function LeafletProvider(props: { children: ComponentChildren }) {
11+
if (!IS_BROWSER) {
12+
return <p>Leaflet must be loaded on the client. No children will render</p>;
13+
}
14+
const [value, setValue] = useState<typeof Leaflet | null>(null);
15+
return (
16+
<>
17+
<link
18+
rel="stylesheet"
19+
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
20+
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
21+
crossorigin=""
22+
/>
23+
<script
24+
onLoad={() => setValue(globalThis.L)}
25+
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
26+
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
27+
crossorigin=""
28+
/>
29+
<LeafletContext.Provider value={value}>
30+
{props.children}
31+
</LeafletContext.Provider>
32+
</>
33+
);
34+
}

src/routes/about.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Handlers, PageProps, RouteContext } from "$fresh/server.ts";
22
import { Partial } from "$fresh/runtime.ts";
3-
import NextContentButton from "../islands/NextContentButton.tsx";
4-
import { Link } from "../components/Link.tsx";
3+
import NextContentButton from "#src/islands/NextContentButton.tsx";
4+
import { Link } from "#src/components/Link.tsx";
55

66
const loadFooValue = async () => {
77
return "nyaomaru";

src/routes/chart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ChartIsland from "../islands/Chart.tsx";
1+
import ChartIsland from "#src/islands/Chart.tsx";
22
import { ChartColors } from "$fresh_charts/utils.ts";
3-
import { Link } from "../components/Link.tsx";
3+
import { Link } from "#src/components/Link.tsx";
44

55
const createRandomNumber = () => {
66
return Math.floor(Math.random() * 100);

src/routes/countdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineRoute } from "$fresh/server.ts";
2-
import Countdown from "../islands/Countdown.tsx";
3-
import { Link } from "../components/Link.tsx";
2+
import Countdown from "#src/islands/Countdown.tsx";
3+
import { Link } from "#src/components/Link.tsx";
44

55
export default defineRoute(async (_req, _ctx) => {
66
const date = new Date();

src/routes/greet/[name].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Head } from "$fresh/runtime.ts";
22
import { PageProps, RouteConfig } from "$fresh/server.ts";
3-
import { Link } from "../../components/Link.tsx";
3+
import { Link } from "#src/components/Link.tsx";
44

55
export const config: RouteConfig = {
66
skipInheritedLayouts: true,

src/routes/index.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Handlers, PageProps, RouteConfig } from "$fresh/server.ts";
22
import { useCSP } from "$fresh/runtime.ts";
33
import { useSignal } from "@preact/signals";
4-
import { Link } from "../components/Link.tsx";
5-
import Counter from "../islands/Counter.tsx";
4+
import { Link } from "#src/components/Link.tsx";
5+
import Counter from "#src/islands/Counter.tsx";
66

77
const nameList = ["Alice", "Bob", "Charlie", "David", "Eve"];
88

@@ -59,19 +59,25 @@ export default function Home({ data }: PageProps<string>) {
5959
<div class="my-4 flex flex-col items-center justify-center">
6060
<h2 class="text-2xl font-bold">Page Buttons</h2>
6161
<div class="my-4 flex gap-4">
62-
<Link text="Go About Page" href="about" />
63-
<Link text="Go Greet Page" href={`greet/${name}`} />
64-
<Link text="Go Search Page" href="search" />
65-
<Link text="Go Countdown Page" href="countdown" />
62+
<Link text="About" href="about" />
63+
<Link text="Greet" href={`greet/${name}`} />
64+
<Link text="Search" href="search" />
65+
<Link text="Countdown" href="countdown" />
6666
</div>
6767
<div class="my-4 flex gap-4">
6868
<Link
69-
text="Go Projects Page"
69+
text="Projects"
7070
href={`projects/${Math.round(Math.random()) + 1}`}
7171
/>
72-
<Link text="Go Chart Page" href="chart" />
73-
<Link text="Go Markdown Page" href="string" />
74-
<Link text="Go CSP Page" href="correctCSPwithReport" />
72+
<Link text="Chart" href="chart" />
73+
<Link text="Markdown" href="string" />
74+
<Link text="CSP" href="correctCSPwithReport" />
75+
</div>
76+
<div class="my-4 flex gap-4">
77+
<Link
78+
text="Map"
79+
href="map"
80+
/>
7581
</div>
7682
</div>
7783

src/routes/map.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineRoute } from "$fresh/server.ts";
2+
import { Link } from "#src/components/Link.tsx";
3+
import { MapIsland } from "#src/islands/MapIsland.tsx";
4+
5+
export default defineRoute(async (_req, _ctx) => {
6+
return (
7+
<>
8+
<div>
9+
<MapIsland />
10+
</div>
11+
<div class="mt-4">
12+
<Link text="Back" href="/" color="secondary" />
13+
</div>
14+
</>
15+
);
16+
});

src/routes/projects/[id].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Handlers, PageProps } from "$fresh/server.ts";
2-
import { Link } from "../../components/Link.tsx";
2+
import { Link } from "#src/components/Link.tsx";
33

44
const projects = [{ id: 1, name: "Project 1", stars: 10 }, {
55
id: 2,

src/routes/search.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Handlers, PageProps } from "$fresh/server.ts";
2-
import { Link } from "../components/Link.tsx";
2+
import { Link } from "#src/components/Link.tsx";
33

44
const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
55

src/routes/subscribe.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Handlers, type PageProps } from "$fresh/server.ts";
2-
import { Link } from "../components/Link.tsx";
2+
import { Link } from "#src/components/Link.tsx";
33

44
interface Props {
55
message: string | null;

0 commit comments

Comments
 (0)