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.

feat: enable dark-mode, move to UserConfig (#266)

Myles Scolnick3992f2fd2889
diff --git a/frontend/src/components/app-config/theme-toggle.tsx b/frontend/src/components/app-config/theme-toggle.tsx
deleted file mode 100644
index 9c15467cbf..0000000000
--- a/frontend/src/components/app-config/theme-toggle.tsx
+++ /dev/null
@@ -1,27 +0,0 @@
-/* Copyright 2023 Marimo. All rights reserved. */
-import { getFeatureFlag } from "@/core/config/feature-flag";
-import { useTheme } from "../../theme/useTheme";
-import { Checkbox } from "../ui/checkbox";
-import { FormItem, FormLabel, FormControl } from "../ui/form";
-
-export const ThemeToggle: React.FC = () => {
- const { theme, setTheme } = useTheme();
-
- if (!getFeatureFlag("theming")) {
- return null;
- }
-
- return (
- <FormItem className="flex flex-row items-start space-x-2 space-y-0">
- <FormControl>
- <Checkbox
- checked={theme === "dark"}
- onCheckedChange={(checked) => {
- return setTheme(checked === true ? "dark" : "light");
- }}
- />
- </FormControl>
- <FormLabel className="font-normal">Dark mode</FormLabel>
- </FormItem>
- );
-};
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index 06c3867641..07930bb3f6 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -16,7 +16,6 @@ import { Input } from "../ui/input";
import { toast } from "../ui/use-toast";
import { saveUserConfig } from "../../core/network/requests";
import { useUserConfig } from "../../core/state/config";
-import { ThemeToggle } from "./theme-toggle";
import { NativeSelect } from "../ui/native-select";
import { KEYMAP_PRESETS } from "@/core/codemirror/keymaps/keymaps";
import { CopilotConfig } from "@/core/codemirror/copilot/copilot-config";
@@ -170,7 +169,32 @@ export const UserConfigForm: React.FC = () => {
)}
/>
</div>
- <ThemeToggle />
+ <div className="flex flex-col gap-3">
+ <SettingSubtitle>Display</SettingSubtitle>
+ <FormField
+ control={form.control}
+ name="display.theme"
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-start space-x-2 space-y-0">
+ <FormControl>
+ <Checkbox
+ checked={field.value === "dark"}
+ onCheckedChange={(checked) => {
+ return field.onChange(
+ checked === true ? "dark" : "light"
+ );
+ }}
+ />
+ </FormControl>
+ <FormLabel className="font-normal">
+ {/* Spooky mode in October */}
+ {new Date().getMonth() === 9 ? "👻 " : undefined}
+ Dark mode
+ </FormLabel>
+ </FormItem>
+ )}
+ />
+ </div>
<div className="flex flex-col gap-3">
<SettingSubtitle>Runtime</SettingSubtitle>
<FormField
diff --git a/frontend/src/core/config/config.ts b/frontend/src/core/config/config.ts
index a285026031..e103b20e04 100644
--- a/frontend/src/core/config/config.ts
+++ b/frontend/src/core/config/config.ts
@@ -27,9 +27,13 @@ export const UserConfigSchema = z
auto_instantiate: z.boolean(),
})
.default({ auto_instantiate: true }),
+ display: z
+ .object({
+ theme: z.enum(["light", "dark"]).default("light"),
+ })
+ .default({ theme: "light" }),
experimental: z
.object({
- theming: z.boolean().optional(),
layouts: z.boolean().optional(),
})
// Pass through so that we don't remove any extra keys that the user has added.
diff --git a/frontend/src/core/config/feature-flag.ts b/frontend/src/core/config/feature-flag.ts
index 4ac8c5be30..ab0662dfcc 100644
--- a/frontend/src/core/config/feature-flag.ts
+++ b/frontend/src/core/config/feature-flag.ts
@@ -3,10 +3,6 @@
import { getUserConfig } from "../state/config";
export interface ExperimentalFeatures {
- /**
- * Allows the user to switch between light and dark themes.
- */
- theming: boolean;
/**
* Allows the user to switch between different layouts.
*/
@@ -14,7 +10,6 @@ export interface ExperimentalFeatures {
}
const defaultValues: ExperimentalFeatures = {
- theming: false,
layouts: false,
};
diff --git a/frontend/src/css/globals.css b/frontend/src/css/globals.css
index 73261e10c9..0f5166036b 100644
--- a/frontend/src/css/globals.css
+++ b/frontend/src/css/globals.css
@@ -115,7 +115,6 @@
--destructive-foreground: 210 40% 98%; /* red-1 */
--accent: 192 56.6% 26.5%; /* cyan-7 */
--accent-foreground: 190 80% 84%; /* cyan-12 */
-
--link: 211 90% 62% /* blue-11 */;
--link-visited: 272 51% 74% /* purple-9 */;
diff --git a/frontend/src/stories/cell.stories.tsx b/frontend/src/stories/cell.stories.tsx
index b51f70c178..19f9dd8200 100644
--- a/frontend/src/stories/cell.stories.tsx
+++ b/frontend/src/stories/cell.stories.tsx
@@ -51,6 +51,9 @@ const props: CellProps = {
autosave_delay: 1000,
format_on_save: false,
},
+ display: {
+ theme: "light",
+ },
runtime: {
auto_instantiate: true,
},
diff --git a/frontend/src/theme/useTheme.ts b/frontend/src/theme/useTheme.ts
index 3cb769c1b2..7634adb549 100644
--- a/frontend/src/theme/useTheme.ts
+++ b/frontend/src/theme/useTheme.ts
@@ -1,70 +1,33 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { store } from "@/core/state/jotai";
-import { getFeatureFlag } from "@/core/config/feature-flag";
-import { useAtom } from "jotai";
-import Cookies from "js-cookie";
-import { atomWithStorage } from "jotai/utils";
-import { SyncStorage } from "jotai/vanilla/utils/atomWithStorage";
+import { useAtomValue } from "jotai";
import { useEffect, useState } from "react";
+import { userConfigAtom } from "@/core/state/config";
export type Theme = "light" | "dark";
-const createCookieStorage = <T>(): SyncStorage<T> => {
- const typed = Cookies.withConverter<T>({});
- return {
- getItem: (key: string, initialValue: T) => {
- return (typed.get(key) as T) ?? initialValue;
- },
- setItem: (key: string, value: T) => {
- typed.set(key, value);
- },
- removeItem: (key: string) => {
- typed.remove(key);
- },
- };
-};
-
-const themeAtom = atomWithStorage<Theme>(
- "marimo:theme",
- "light",
- // We use cookies instead of localStorage, so that the theme persists
- // between different ports on localhost.
- createCookieStorage<Theme>()
-);
-
/**
- * React hook to manage the theme of the app.
- * This is stored in local storage, so it persists between page loads.
+ * React hook to get the theme of the app.
+ * This is stored in the user config.
*/
export function useTheme() {
- const [theme, setTheme] = useAtom(themeAtom);
-
- if (getFeatureFlag("theming")) {
- return {
- theme,
- setTheme,
- };
- }
-
- // Dark theme is not ready so return light theme.
- return { theme: "light" as const, setTheme };
+ const userConfig = useAtomValue(userConfigAtom);
+ return { theme: userConfig.display.theme };
}
export function getTheme(): Theme {
- if (getFeatureFlag("theming")) {
- return store.get(themeAtom);
- }
- return "light";
+ return store.get(userConfigAtom).display.theme;
}
/**
* Plugins are in a different react tree, so we cannot use useAtom as it looks
- * for an existing Provider. Instead we need to subscribe to the atom directly.
+ * for an existing Provider in the react tree.
+ * Instead we need to subscribe to the atom directly.
*/
export function useThemeForPlugin() {
const [theme, setTheme] = useState(getTheme());
useEffect(() => {
- return store.sub(themeAtom, () => {
+ return store.sub(userConfigAtom, () => {
setTheme(getTheme());
});
}, []);
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index 45f4c15248..2ebb995470 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -77,6 +77,18 @@ class RuntimeConfig(TypedDict, total=False):
auto_instantiate: bool
+@mddoc
+class DisplayConfig(TypedDict, total=False):
+ """Configuration for display.
+
+ **Keys.**
+
+ - `theme`: one of `"light"` or `"dark"`
+ """
+
+ theme: Literal["light", "dark"]
+
+
@mddoc
class MarimoConfig(TypedDict, total=False):
"""Configuration for the marimo editor.
@@ -108,6 +120,7 @@ class MarimoConfig(TypedDict, total=False):
save: SaveConfig
keymap: KeymapConfig
runtime: RuntimeConfig
+ display: DisplayConfig
experimental: Dict[str, Any]
@@ -120,6 +133,7 @@ DEFAULT_CONFIG: MarimoConfig = {
},
"keymap": {"preset": "default"},
"runtime": {"auto_instantiate": True},
+ "display": {"theme": "light"},
}
_USER_CONFIG: Optional[MarimoConfig] = None