Skip to content

Commit 59bbf9e

Browse files
committed
cleanup env and move storage to utils
1 parent 993cd4f commit 59bbf9e

File tree

6 files changed

+38
-85
lines changed

6 files changed

+38
-85
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

public/robots.txt

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/app.tsx

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
import { Router } from "@solidjs/router";
22
import { FileRoutes } from "@solidjs/start/router";
33
import { ColorModeProvider, ColorModeScript, createLocalStorageManager } from "@kobalte/core";
4-
import { Suspense, Show } from "solid-js";
5-
6-
import "./app.css";
7-
8-
const ENABLE_COLOR_MODE = import.meta.env.VITE_ENABLE_COLOR_MODE === "true";
4+
import { Suspense } from "solid-js";
95

106
export default function App() {
11-
let storageManager: ReturnType<typeof createLocalStorageManager>;
12-
13-
if (ENABLE_COLOR_MODE) {
14-
storageManager = createLocalStorageManager("theme");
15-
}
7+
const storageManager = createLocalStorageManager("theme");
168

179
return (
18-
<Router
19-
base={import.meta.env.SERVER_BASE_URL}
20-
root={(props) => (
21-
<Show when={ENABLE_COLOR_MODE} fallback={<Suspense>{props.children}</Suspense>}>
22-
<>
23-
<ColorModeScript storageType={storageManager.type} />
24-
<ColorModeProvider storageManager={storageManager}>
25-
<Suspense>{props.children}</Suspense>
26-
</ColorModeProvider>
27-
</>
28-
</Show>
29-
)}>
30-
<FileRoutes />
31-
</Router>
10+
<>
11+
<ColorModeScript storageType={storageManager.type} />
12+
<ColorModeProvider storageManager={storageManager}>
13+
<Suspense>
14+
<Router base={import.meta.env.SERVER_BASE_URL}>
15+
<FileRoutes />
16+
</Router>
17+
</Suspense>
18+
</ColorModeProvider>
19+
</>
3220
);
3321
}

src/routes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MetaProvider, Title } from "@solidjs/meta";
22
import { Show } from "solid-js";
33

4-
import { createStorageSignal } from "~/storage";
4+
import { createStorageSignal } from "~/utils";
55

66
import { ThemeToggle } from "~/components/ui/ThemeToggle";
77
import { Flex } from "~/components/ui/Flex";

src/storage.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { ClassValue } from "clsx";
22
import { clsx } from "clsx";
33
import { twMerge } from "tailwind-merge";
4+
import type { Accessor, Setter } from "solid-js";
5+
import { createSignal, createEffect, onMount, on } from "solid-js";
46

57
export function cn(...inputs: ClassValue[]) {
68
return twMerge(clsx(inputs));
@@ -10,3 +12,26 @@ export function getCSSVar(name: string) {
1012
const css = getComputedStyle(document.documentElement);
1113
return css.getPropertyValue(name).trim();
1214
}
15+
16+
export function createStorageSignal<T>(key: string, defaultValue: T): [Accessor<T>, Setter<T>] {
17+
const [value, setValue] = createSignal<T>(defaultValue);
18+
19+
onMount(() => {
20+
const item = localStorage.getItem(key);
21+
if (item != null) {
22+
try {
23+
setValue(JSON.parse(item));
24+
} catch {
25+
/* ignore malformed */
26+
}
27+
}
28+
});
29+
30+
createEffect(
31+
on(value, (v) => {
32+
localStorage.setItem(key, JSON.stringify(v));
33+
})
34+
);
35+
36+
return [value, setValue];
37+
}

0 commit comments

Comments
 (0)