Skip to content

feat: add a theme picker #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<link rel="stylesheet" href="src/index.css" />
</head>

<body class="dark">
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react-simple-code-editor": "^0.14.1",
"tailwind-merge": "^3.3.0",
"tailwindcss-animate": "^1.0.7",
"valibot": "^1.1.0",
"zustand": "^5.0.5"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 55 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Editor } from "@/Editor";
import { Preview } from "@/Preview";
import { Logo } from "@/components/Logo";
import { ResizableHandle, ResizablePanelGroup } from "@/components/Resizable";
import { Editor } from "./Editor";
import { Logo } from "./components/Logo";
import { Preview } from "./Preview";
import { useStore } from "@/store";
import { useEffect } from "react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuTrigger,
} from "@/components/DropdownMenu";
import { type FC, useEffect, useMemo } from "react";

// Glue code required to be able to run wasm compiled Go code.
import "@/utils/wasm_exec.js";
import { useTheme } from "@/contexts/theme";
import { MoonIcon, SunIcon, SunMoonIcon } from "lucide-react";
import { Button } from "./components/Button";

type GoPreviewDef = (v: unknown) => Promise<string>;

Expand Down Expand Up @@ -93,13 +103,11 @@ export const App = () => {
>
Support
</a>
<ThemeSelector />
</div>
</nav>

<ResizablePanelGroup
aria-hidden={!$wasmState}
direction={"horizontal"}
>
<ResizablePanelGroup aria-hidden={!$wasmState} direction={"horizontal"}>
{/* EDITOR */}
<Editor />

Expand All @@ -111,3 +119,42 @@ export const App = () => {
</main>
);
};

const ThemeSelector: FC = () => {
const { theme, setTheme } = useTheme();

const Icon = useMemo(() => {
if (theme === "system") {
return SunMoonIcon;
}

if (theme === "dark") {
return MoonIcon;
}

return SunIcon;
}, [theme]);

return (
<DropdownMenu>
<DropdownMenuTrigger asChild={true}>
<Button variant="subtle" size="icon-lg">
<Icon height={24} width={24} />
</Button>
</DropdownMenuTrigger>
<DropdownMenuPortal>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setTheme("dark")}>
<MoonIcon width={24} height={24} /> Dark
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("light")}>
<SunIcon width={24} height={24} /> Light
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("system")}>
<SunMoonIcon width={24} height={24} /> System
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
);
};
74 changes: 74 additions & 0 deletions src/contexts/theme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
createContext,
useContext,
useEffect,
useState,
type FC,
type PropsWithChildren,
} from "react";
import * as v from "valibot";

const STORAGE_KEY = "theme";

const ThemeSchema = v.union([
v.literal("dark"),
v.literal("light"),
v.literal("system"),
]);
type Theme = v.InferInput<typeof ThemeSchema>;

type ThemeContext = {
theme: Theme;
setTheme: (theme: Theme) => void;
};

const ThemeContext = createContext<ThemeContext>({
theme: "system",
setTheme: () => null,
});

export const ThemeProvider: FC<PropsWithChildren> = ({ children }) => {
const [theme, setTheme] = useState<Theme>(() => {
const parsedTheme = v.safeParse(
ThemeSchema,
localStorage.getItem(STORAGE_KEY),
);

if (!parsedTheme.success) {
return "system";
}

return parsedTheme.output;
});

useEffect(() => {
const force =
theme === "dark" ||
(theme === "system" &&
window.matchMedia("(prefers-color-scheme: dark)").matches);

document.documentElement.classList.toggle("dark", force);

if (theme === "system") {
localStorage.removeItem(STORAGE_KEY);
} else {
localStorage.setItem(STORAGE_KEY, theme);
}
}, [theme]);

return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
};

export const useTheme = () => {
const themeContext = useContext(ThemeContext);

if (!themeContext) {
throw new Error("useTheme must be used within a ThemeProvider");
}

return themeContext;
};
9 changes: 6 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TooltipProvider } from "@/components/Tooltip";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { App } from "./App.tsx";
import { ThemeProvider } from "@/contexts/theme.tsx";

const root = document.getElementById("root");

Expand All @@ -12,9 +13,11 @@ if (!root) {
} else {
createRoot(root).render(
<StrictMode>
<TooltipProvider>
<App />
</TooltipProvider>
<ThemeProvider>
<TooltipProvider>
<App />
</TooltipProvider>
</ThemeProvider>
</StrictMode>,
);
}