Skip to content

Commit d09a115

Browse files
HotariTobuclaude
andcommitted
Add dark mode with three-state toggle (light/system/dark)
Support class-based dark mode via Tailwind CSS 4's @custom-variant, CodeMirror theme switching with oneDark via Compartment, localStorage persistence, system preference tracking, and FOUC prevention. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3347507 commit d09a115

13 files changed

Lines changed: 217 additions & 20 deletions

‎biome.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"quoteStyle": "double"
2525
}
2626
},
27+
"css": {
28+
"parser": {
29+
"tailwindDirectives": true
30+
}
31+
},
2732
"assist": {
2833
"enabled": true,
2934
"actions": {

‎bun.lock‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@codemirror/lang-python": "^6.2.1",
1515
"@codemirror/language": "^6.12.1",
1616
"@codemirror/state": "^6.5.4",
17+
"@codemirror/theme-one-dark": "^6.1.3",
1718
"@codemirror/view": "^6.39.12",
1819
"bun-plugin-tailwind": "^0.1.2",
1920
"codemirror": "^6.0.2",

‎src/App.tsx‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { CodeEditor } from "./components/code-editor";
33
import { InputPanel } from "./components/input-panel";
44
import { OutputPanel } from "./components/output-panel";
55
import { RunButton } from "./components/run-button";
6+
import { ThemeToggle } from "./components/theme-toggle";
67
import { usePython } from "./hooks/use-python";
8+
import { useTheme } from "./hooks/use-theme";
79

810
const DEFAULT_CODE = `# Welcome to Your Python!
911
# Write your Python code here and click Run.
@@ -20,6 +22,7 @@ for i in range(1, 6):
2022
`;
2123

2224
export function App() {
25+
const { theme, preference, setPreference } = useTheme();
2326
const { state, execute } = usePython();
2427
const codeRef = useRef(DEFAULT_CODE);
2528
const [stdin, setStdin] = useState("");
@@ -41,17 +44,29 @@ export function App() {
4144
}, [state, stdin, execute]);
4245

4346
return (
44-
<div class="min-h-screen bg-gray-50 py-8">
47+
<div class="min-h-screen bg-gray-50 py-8 dark:bg-gray-900">
4548
<div class="w-200 mx-auto space-y-4">
46-
<header>
47-
<h1 class="text-3xl font-bold text-gray-900">Your Python</h1>
48-
<p class="text-gray-600 mt-2">
49-
A browser-based Python execution environment
50-
</p>
49+
<header class="flex items-start justify-between">
50+
<div>
51+
<h1 class="text-3xl font-bold text-gray-900 dark:text-gray-100">
52+
Your Python
53+
</h1>
54+
<p class="text-gray-600 mt-2 dark:text-gray-400">
55+
A browser-based Python execution environment
56+
</p>
57+
</div>
58+
<ThemeToggle
59+
preference={preference}
60+
onPreferenceChange={setPreference}
61+
/>
5162
</header>
5263

5364
<section>
54-
<CodeEditor initialValue={DEFAULT_CODE} onChange={handleCodeChange} />
65+
<CodeEditor
66+
initialValue={DEFAULT_CODE}
67+
onChange={handleCodeChange}
68+
theme={theme}
69+
/>
5570
</section>
5671

5772
<section>

‎src/components/code-editor.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { useCodeMirror } from "../hooks/use-code-mirror";
2+
import type { ResolvedTheme } from "../hooks/use-theme";
23

34
export interface CodeEditorProps {
45
initialValue: string;
56
onChange: (value: string) => void;
7+
theme: ResolvedTheme;
68
}
79

8-
export function CodeEditor({ initialValue, onChange }: CodeEditorProps) {
10+
export function CodeEditor({ initialValue, onChange, theme }: CodeEditorProps) {
911
const { containerRef } = useCodeMirror({
1012
initialValue,
1113
onChange,
14+
theme,
1215
});
1316

1417
return (
1518
<div
1619
ref={containerRef as { current: HTMLDivElement | null }}
17-
class="border border-gray-300 rounded-md overflow-hidden"
20+
class="border border-gray-300 rounded-md overflow-hidden dark:border-gray-600"
1821
/>
1922
);
2023
}

‎src/components/input-panel.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ export function InputPanel({ value, onChange }: InputPanelProps) {
1212
<div>
1313
<button
1414
type="button"
15-
class="text-sm font-medium text-gray-700 mb-2 flex items-center gap-1 hover:text-gray-900"
15+
class="text-sm font-medium text-gray-700 mb-2 flex items-center gap-1 hover:text-gray-900 dark:text-gray-300 dark:hover:text-gray-100"
1616
onClick={() => setIsOpen(!isOpen)}
1717
>
1818
<span>Input</span>
19-
<span class="text-xs text-gray-400">{isOpen ? "▼" : "◀︎"}</span>
19+
<span class="text-xs text-gray-400 dark:text-gray-500">
20+
{isOpen ? "▼" : "◀︎"}
21+
</span>
2022
</button>
2123
{isOpen && (
2224
<div>
23-
<div class="bg-gray-100 p-4 rounded-md">
25+
<div class="bg-gray-100 p-4 rounded-md dark:bg-gray-800">
2426
<textarea
25-
class="block w-full min-h-[3lh] bg-transparent font-mono text-sm resize-none border-none outline-none m-0 p-0"
27+
class="block w-full min-h-[3lh] bg-transparent font-mono text-sm resize-none border-none outline-none m-0 p-0 text-gray-900 dark:text-gray-100"
2628
style={{ fieldSizing: "content" }}
2729
placeholder="Enter input values, one per line..."
2830
value={value}
2931
onInput={(e) => onChange((e.target as HTMLTextAreaElement).value)}
3032
/>
3133
</div>
32-
<p class="text-xs text-gray-500 mt-1">
34+
<p class="text-xs text-gray-500 mt-1 dark:text-gray-400">
3335
Each line is used as input for input() calls. Falls back to prompt
3436
if exhausted.
3537
</p>

‎src/components/output-panel.tsx‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ export function OutputPanel({ stdout, error }: OutputPanelProps) {
88
<div class="space-y-4">
99
{error && (
1010
<div>
11-
<h3 class="text-sm font-medium text-red-700 mb-2">Error</h3>
12-
<pre class="bg-red-50 text-red-800 p-4 rounded-md font-mono text-sm whitespace-pre-wrap">
11+
<h3 class="text-sm font-medium text-red-700 mb-2 dark:text-red-400">
12+
Error
13+
</h3>
14+
<pre class="bg-red-50 text-red-800 p-4 rounded-md font-mono text-sm whitespace-pre-wrap dark:bg-red-950 dark:text-red-300">
1315
{error}
1416
</pre>
1517
</div>
1618
)}
1719
{stdout && (
1820
<div>
19-
<h3 class="text-sm font-medium text-gray-700 mb-2">Output</h3>
20-
<pre class="bg-gray-100 p-4 rounded-md font-mono text-sm whitespace-pre-wrap">
21+
<h3 class="text-sm font-medium text-gray-700 mb-2 dark:text-gray-300">
22+
Output
23+
</h3>
24+
<pre class="bg-gray-100 p-4 rounded-md font-mono text-sm whitespace-pre-wrap dark:bg-gray-800 dark:text-gray-100">
2125
{stdout}
2226
</pre>
2327
</div>
2428
)}
2529
{!stdout && !error && (
26-
<div class="text-gray-500 text-sm italic">
30+
<div class="text-gray-500 text-sm italic dark:text-gray-400">
2731
Run your code to see output here.
2832
</div>
2933
)}

‎src/components/theme-toggle.tsx‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { ThemePreference } from "../hooks/use-theme";
2+
3+
export interface ThemeToggleProps {
4+
preference: ThemePreference;
5+
onPreferenceChange: (preference: ThemePreference) => void;
6+
}
7+
8+
const options: { value: ThemePreference; label: string; icon: string }[] = [
9+
{
10+
value: "light",
11+
label: "Light",
12+
icon: "M12 3v1m0 16v1m-8-9H3m18 0h-1m-2.636-5.364l-.707.707m-9.314 9.314l-.707.707m0-10.728l.707.707m9.314 9.314l.707.707M12 8a4 4 0 100 8 4 4 0 000-8z",
13+
},
14+
{
15+
value: "system",
16+
label: "System",
17+
icon: "M9 17H5a2 2 0 01-2-2V5a2 2 0 012-2h14a2 2 0 012 2v10a2 2 0 01-2 2h-4m-6 0v4m0-4h6m-6 4h6",
18+
},
19+
{
20+
value: "dark",
21+
label: "Dark",
22+
icon: "M21 12.79A9 9 0 1111.21 3a7 7 0 009.79 9.79z",
23+
},
24+
];
25+
26+
export function ThemeToggle({
27+
preference,
28+
onPreferenceChange,
29+
}: ThemeToggleProps) {
30+
return (
31+
<fieldset
32+
aria-label="Theme preference"
33+
class="flex gap-0.5 rounded-lg bg-gray-200 p-1 dark:bg-gray-700 border-none m-0"
34+
>
35+
{options.map(({ value, label, icon }) => (
36+
<button
37+
key={value}
38+
type="button"
39+
aria-label={label}
40+
onClick={() => onPreferenceChange(value)}
41+
class={`rounded-md p-1.5 transition-colors ${
42+
preference === value
43+
? "bg-white text-gray-900 shadow-sm dark:bg-gray-600 dark:text-gray-100"
44+
: "text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100"
45+
}`}
46+
>
47+
<svg
48+
xmlns="http://www.w3.org/2000/svg"
49+
width="16"
50+
height="16"
51+
viewBox="0 0 24 24"
52+
fill="none"
53+
stroke="currentColor"
54+
stroke-width="2"
55+
stroke-linecap="round"
56+
stroke-linejoin="round"
57+
aria-hidden="true"
58+
>
59+
<title>{label}</title>
60+
<path d={icon} />
61+
</svg>
62+
</button>
63+
))}
64+
</fieldset>
65+
);
66+
}

‎src/hooks/use-code-mirror.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { indentWithTab } from "@codemirror/commands";
22
import { python } from "@codemirror/lang-python";
33
import { indentUnit } from "@codemirror/language";
4-
import { EditorState } from "@codemirror/state";
4+
import { Compartment, EditorState } from "@codemirror/state";
5+
import { oneDark } from "@codemirror/theme-one-dark";
56
import { keymap } from "@codemirror/view";
67
import { basicSetup, EditorView } from "codemirror";
78
import { useEffect, useRef } from "preact/hooks";
9+
import type { ResolvedTheme } from "./use-theme";
810

911
export interface UseCodeMirrorOptions {
1012
initialValue: string;
1113
onChange: (value: string) => void;
14+
theme: ResolvedTheme;
1215
}
1316

1417
export interface UseCodeMirrorResult {
@@ -20,12 +23,17 @@ const autoHeightTheme = EditorView.theme({
2023
".cm-scroller": { overflow: "visible" },
2124
});
2225

26+
function editorTheme(theme: ResolvedTheme) {
27+
return theme === "dark" ? oneDark : [];
28+
}
29+
2330
export function useCodeMirror(
2431
options: UseCodeMirrorOptions,
2532
): UseCodeMirrorResult {
2633
const valueRef = useRef(options.initialValue);
2734
const containerRef = useRef<HTMLElement | null>(null);
2835
const viewRef = useRef<EditorView | null>(null);
36+
const themeCompartment = useRef(new Compartment());
2937

3038
useEffect(() => {
3139
if (!containerRef.current || viewRef.current) return;
@@ -36,6 +44,7 @@ export function useCodeMirror(
3644
indentUnit.of(" "),
3745
autoHeightTheme,
3846
keymap.of([indentWithTab]),
47+
themeCompartment.current.of(editorTheme(options.theme)),
3948
EditorView.updateListener.of((update) => {
4049
if (update.docChanged) {
4150
valueRef.current = update.state.doc.toString();
@@ -60,5 +69,11 @@ export function useCodeMirror(
6069
};
6170
}, [options.onChange]);
6271

72+
useEffect(() => {
73+
viewRef.current?.dispatch({
74+
effects: themeCompartment.current.reconfigure(editorTheme(options.theme)),
75+
});
76+
}, [options.theme]);
77+
6378
return { containerRef };
6479
}

‎src/hooks/use-theme.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useCallback, useEffect, useState } from "preact/hooks";
2+
import {
3+
applyTheme,
4+
type ResolvedTheme,
5+
readStoredPreference,
6+
resolveTheme,
7+
STORAGE_KEY,
8+
type ThemePreference,
9+
} from "../theme";
10+
11+
export type { ResolvedTheme, ThemePreference };
12+
13+
export interface UseThemeResult {
14+
theme: ResolvedTheme;
15+
preference: ThemePreference;
16+
setPreference: (preference: ThemePreference) => void;
17+
}
18+
19+
export function useTheme(): UseThemeResult {
20+
const [preference, setPreferenceState] =
21+
useState<ThemePreference>(readStoredPreference);
22+
const [theme, setTheme] = useState<ResolvedTheme>(() =>
23+
resolveTheme(preference),
24+
);
25+
26+
const setPreference = useCallback((newPref: ThemePreference) => {
27+
setPreferenceState(newPref);
28+
localStorage.setItem(STORAGE_KEY, newPref);
29+
const resolved = resolveTheme(newPref);
30+
setTheme(resolved);
31+
applyTheme(resolved);
32+
}, []);
33+
34+
useEffect(() => {
35+
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
36+
const handler = () => {
37+
if (preference !== "system") return;
38+
const resolved = resolveTheme("system");
39+
setTheme(resolved);
40+
applyTheme(resolved);
41+
};
42+
mediaQuery.addEventListener("change", handler);
43+
applyTheme(theme);
44+
return () => mediaQuery.removeEventListener("change", handler);
45+
}, [preference, theme]);
46+
47+
return { theme, preference, setPreference };
48+
}

0 commit comments

Comments
 (0)