Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: add 'system' theme option (#556)

Myles Scolnickdc822f4944c2
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index 35b0fa921a..181cce25ef 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -21,6 +21,7 @@ import { KEYMAP_PRESETS } from "@/core/codemirror/keymaps/keymaps";
import { CopilotConfig } from "@/core/codemirror/copilot/copilot-config";
import { Switch } from "../ui/switch";
import { SettingTitle, SettingDescription, SettingSubtitle } from "./common";
+import { THEMES } from "@/theme/useTheme";
export const UserConfigForm: React.FC = () => {
const [config, setConfig] = useUserConfig();
@@ -201,22 +202,22 @@ export const UserConfigForm: React.FC = () => {
control={form.control}
name="display.theme"
render={({ field }) => (
- <FormItem className="flex flex-row items-start space-x-2 space-y-0">
+ <FormItem>
+ <FormLabel>Theme</FormLabel>
<FormControl>
- <Checkbox
- checked={field.value === "dark"}
- onCheckedChange={(checked) => {
- return field.onChange(
- checked === true ? "dark" : "light"
- );
- }}
- />
+ <NativeSelect
+ onChange={(e) => field.onChange(e.target.value)}
+ value={field.value}
+ className="inline-flex mx-2"
+ >
+ {THEMES.map((option) => (
+ <option value={option} key={option}>
+ {option}
+ </option>
+ ))}
+ </NativeSelect>
</FormControl>
- <FormLabel className="font-normal">
- {/* Spooky mode in October */}
- {new Date().getMonth() === 9 ? "👻 " : undefined}
- Dark mode
- </FormLabel>
+ <FormMessage />
</FormItem>
)}
/>
diff --git a/frontend/src/core/config/config-schema.ts b/frontend/src/core/config/config-schema.ts
index 366370db44..2c3e695357 100644
--- a/frontend/src/core/config/config-schema.ts
+++ b/frontend/src/core/config/config-schema.ts
@@ -41,7 +41,7 @@ export const UserConfigSchema = z
.default({ auto_instantiate: true }),
display: z
.object({
- theme: z.enum(["light", "dark"]).default("light"),
+ theme: z.enum(["light", "dark", "system"]).default("light"),
code_editor_font_size: z.number().nonnegative().default(14),
})
.default({
diff --git a/frontend/src/plugins/impl/CodeEditorPlugin.tsx b/frontend/src/plugins/impl/CodeEditorPlugin.tsx
index 4fcd7b2d9d..c3d5dca046 100644
--- a/frontend/src/plugins/impl/CodeEditorPlugin.tsx
+++ b/frontend/src/plugins/impl/CodeEditorPlugin.tsx
@@ -3,7 +3,7 @@ import { z } from "zod";
import { IPlugin, IPluginProps, Setter } from "../types";
import { Labeled } from "./common/labeled";
-import { Theme, getTheme } from "@/theme/useTheme";
+import { Theme, useThemeForPlugin } from "@/theme/useTheme";
import { lazy } from "react";
type T = string;
@@ -11,7 +11,7 @@ type T = string;
interface Data {
language: string;
placeholder: string;
- theme: Theme;
+ theme?: Theme;
label: string | null;
disabled?: boolean;
minHeight?: number;
@@ -24,7 +24,7 @@ export class CodeEditorPlugin implements IPlugin<T, Data> {
initialValue: z.string(),
language: z.string().default("python"),
placeholder: z.string(),
- theme: z.enum(["light", "dark"]).default("light"),
+ theme: z.enum(["light", "dark"]).optional(),
label: z.string().nullable(),
disabled: z.boolean().optional(),
minHeight: z.number().optional(),
@@ -51,14 +51,15 @@ interface CodeEditorComponentProps extends Data {
}
const CodeEditorComponent = (props: CodeEditorComponentProps) => {
- const theme = props.theme || getTheme();
+ const { theme } = useThemeForPlugin();
+ const finalTheme = props.theme || theme;
const minHeight = props.minHeight ? `${props.minHeight}px` : "70px";
return (
<Labeled label={props.label} align="top">
<LazyAnyLanguageCodeMirror
- className={`cm [&>*]:outline-none border rounded overflow-hidden ${theme}`}
- theme={theme === "dark" ? "dark" : "light"}
+ className={`cm [&>*]:outline-none border rounded overflow-hidden ${finalTheme}`}
+ theme={finalTheme === "dark" ? "dark" : "light"}
minHeight={minHeight}
placeholder={props.placeholder}
editable={!props.disabled}
diff --git a/frontend/src/theme/useTheme.ts b/frontend/src/theme/useTheme.ts
index bee8f344b3..7e05f388e1 100644
--- a/frontend/src/theme/useTheme.ts
+++ b/frontend/src/theme/useTheme.ts
@@ -1,22 +1,50 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { store } from "@/core/state/jotai";
-import { useAtomValue } from "jotai";
+import { atom, useAtomValue } from "jotai";
import { useEffect, useState } from "react";
import { userConfigAtom } from "@/core/config/config";
-export type Theme = "light" | "dark";
+export type Theme = "light" | "dark" | "system";
+export type ResolvedTheme = "light" | "dark";
+
+export const THEMES: Theme[] = ["light", "dark", "system"];
+
+const themeAtom = atom((get) => {
+ return get(userConfigAtom).display.theme;
+});
+
+const prefersDarkModeAtom = atom(false);
+
+function setupThemeListener(): void {
+ if (typeof window === "undefined") {
+ return;
+ }
+
+ const media = window.matchMedia("(prefers-color-scheme: dark)");
+ store.set(prefersDarkModeAtom, media.matches);
+ media.addEventListener("change", (e) => {
+ store.set(prefersDarkModeAtom, e.matches);
+ });
+}
+setupThemeListener();
+
+const resolvedThemeAtom = atom((get) => {
+ const theme = get(themeAtom);
+ const prefersDarkMode = get(prefersDarkModeAtom);
+ return theme === "system" ? (prefersDarkMode ? "dark" : "light") : theme;
+});
/**
* React hook to get the theme of the app.
* This is stored in the user config.
*/
-export function useTheme() {
- const userConfig = useAtomValue(userConfigAtom);
- return { theme: userConfig.display.theme };
+export function useTheme(): { theme: ResolvedTheme } {
+ const theme = useAtomValue(resolvedThemeAtom);
+ return { theme };
}
-export function getTheme(): Theme {
- return store.get(userConfigAtom).display.theme;
+function getTheme(): ResolvedTheme {
+ return store.get(resolvedThemeAtom);
}
/**
@@ -24,10 +52,10 @@ export function getTheme(): Theme {
* for an existing Provider in the react tree.
* Instead we need to subscribe to the atom directly.
*/
-export function useThemeForPlugin() {
+export function useThemeForPlugin(): { theme: ResolvedTheme } {
const [theme, setTheme] = useState(getTheme());
useEffect(() => {
- return store.sub(userConfigAtom, () => {
+ return store.sub(resolvedThemeAtom, () => {
setTheme(getTheme());
});
}, []);
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index 926a9e258c..6a202b130f 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -78,11 +78,11 @@ class DisplayConfig(TypedDict, total=False):
**Keys.**
- - `theme`: `"light"` or `"dark"`
+ - `theme`: `"light"`, `"dark"`, or `"system"`
- `code_editor_font_size`: font size for the code editor
"""
- theme: Literal["light", "dark"]
+ theme: Literal["light", "dark", "system"]
code_editor_font_size: int
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 1befaf87c0..9d1e0d8abc 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -451,7 +451,7 @@ class code_editor(UIElement[str, str]):
major languages are supported, including "sql", "javascript",
"typescript", "html", "css", "c", "cpp", "rust", and more
- `placeholder`: placeholder text to display when the code editor is empty
- - `theme`: theme of the code editor, defaults to `"light"`
+ - `theme`: theme of the code editor, defaults to the editor's default
- `disabled`: whether the input is disabled
- `min_height`: minimum height of the code editor in pixels
- `label`: text label for the element
@@ -465,7 +465,7 @@ class code_editor(UIElement[str, str]):
value: str = "",
language: str = "python",
placeholder: str = "",
- theme: Literal["light", "dark"] = "light",
+ theme: Optional[Literal["light", "dark"]] = None,
disabled: bool = False,
min_height: Optional[int] = None,
*,
diff --git a/marimo/_smoke_tests/code_editor.py b/marimo/_smoke_tests/code_editor.py
index 3a054f9282..4997b4aa82 100644
--- a/marimo/_smoke_tests/code_editor.py
+++ b/marimo/_smoke_tests/code_editor.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.1.73"
+__generated_with = "0.1.76"
app = marimo.App()
@@ -12,7 +12,7 @@ def __(mo):
@app.cell
def __(mo):
- mo.ui.code_editor("SELECT * FROM table;", language="sql")
+ mo.ui.code_editor("SELECT * FROM table;", language="sql", theme="light")
return