|
| 1 | +import { |
| 2 | + createContext, |
| 3 | + useContext, |
| 4 | + useState, |
| 5 | + useEffect, |
| 6 | + useCallback, |
| 7 | +} from "react"; |
| 8 | + |
| 9 | +const ThemeContext = createContext(); |
| 10 | + |
| 11 | +/** |
| 12 | + * Updates all Streamlit Cloud iframes on the page with the new theme. |
| 13 | + * This is extracted as a shared function so it can be called from multiple places. |
| 14 | + */ |
| 15 | +export function updateIframesTheme(theme) { |
| 16 | + if (typeof document === "undefined") return; |
| 17 | + |
| 18 | + const iframes = document.querySelectorAll('iframe[src*="streamlit.app"]'); |
| 19 | + iframes.forEach((iframe) => { |
| 20 | + const currentSrc = iframe.src; |
| 21 | + const url = new URL(currentSrc); |
| 22 | + |
| 23 | + // Get all existing embed_options |
| 24 | + const existingEmbedOptions = url.searchParams.getAll("embed_options"); |
| 25 | + |
| 26 | + // Remove only theme-related embed_options (light_theme or dark_theme) |
| 27 | + const nonThemeOptions = existingEmbedOptions.filter( |
| 28 | + (option) => option !== "light_theme" && option !== "dark_theme", |
| 29 | + ); |
| 30 | + |
| 31 | + // Clear all embed_options and re-add the non-theme ones |
| 32 | + url.searchParams.delete("embed_options"); |
| 33 | + nonThemeOptions.forEach((option) => |
| 34 | + url.searchParams.append("embed_options", option), |
| 35 | + ); |
| 36 | + |
| 37 | + // Add new theme parameter |
| 38 | + url.searchParams.append("embed_options", `${theme}_theme`); |
| 39 | + |
| 40 | + // Force reload iframe with new theme |
| 41 | + iframe.src = url.toString(); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Gets the user's theme preference from localStorage or system preference. |
| 47 | + * Returns "light" as default for SSR. |
| 48 | + */ |
| 49 | +function getUserPreference() { |
| 50 | + if (typeof window === "undefined") { |
| 51 | + return "light"; |
| 52 | + } |
| 53 | + if (window.localStorage.getItem("theme")) { |
| 54 | + return window.localStorage.getItem("theme"); |
| 55 | + } |
| 56 | + return window.matchMedia("(prefers-color-scheme: dark)").matches |
| 57 | + ? "dark" |
| 58 | + : "light"; |
| 59 | +} |
| 60 | + |
| 61 | +export function ThemeContextProvider({ children }) { |
| 62 | + // Initialize with "light" for SSR, will be updated on mount |
| 63 | + const [theme, setThemeState] = useState("light"); |
| 64 | + const [isInitialized, setIsInitialized] = useState(false); |
| 65 | + |
| 66 | + // Apply theme to DOM and localStorage |
| 67 | + const applyTheme = useCallback((newTheme) => { |
| 68 | + if (typeof document === "undefined") return; |
| 69 | + |
| 70 | + const inactiveTheme = newTheme === "light" ? "dark" : "light"; |
| 71 | + document.documentElement.classList.add(newTheme); |
| 72 | + document.documentElement.classList.remove(inactiveTheme); |
| 73 | + localStorage.setItem("theme", newTheme); |
| 74 | + }, []); |
| 75 | + |
| 76 | + // Set theme and update everything |
| 77 | + const setTheme = useCallback( |
| 78 | + (newTheme) => { |
| 79 | + setThemeState(newTheme); |
| 80 | + applyTheme(newTheme); |
| 81 | + updateIframesTheme(newTheme); |
| 82 | + }, |
| 83 | + [applyTheme], |
| 84 | + ); |
| 85 | + |
| 86 | + // Initialize theme on mount (client-side only) |
| 87 | + useEffect(() => { |
| 88 | + const preferredTheme = getUserPreference(); |
| 89 | + setThemeState(preferredTheme); |
| 90 | + applyTheme(preferredTheme); |
| 91 | + setIsInitialized(true); |
| 92 | + }, [applyTheme]); |
| 93 | + |
| 94 | + return ( |
| 95 | + <ThemeContext.Provider value={{ theme, setTheme, isInitialized }}> |
| 96 | + {children} |
| 97 | + </ThemeContext.Provider> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +export function useThemeContext() { |
| 102 | + const context = useContext(ThemeContext); |
| 103 | + if (context === undefined) { |
| 104 | + throw new Error( |
| 105 | + "useThemeContext must be used within a ThemeContextProvider", |
| 106 | + ); |
| 107 | + } |
| 108 | + return context; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Safe version of useThemeContext that returns null if not within a provider. |
| 113 | + * Useful for components that may be rendered outside the provider (e.g., SSR). |
| 114 | + */ |
| 115 | +export function useThemeContextSafe() { |
| 116 | + return useContext(ThemeContext); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Gets the current theme from DOM (for use in non-React contexts or SSR fallback). |
| 121 | + * Returns "light" as default if document is not available. |
| 122 | + */ |
| 123 | +export function getThemeFromDOM() { |
| 124 | + if (typeof document !== "undefined") { |
| 125 | + return document.documentElement.classList.contains("dark") |
| 126 | + ? "dark" |
| 127 | + : "light"; |
| 128 | + } |
| 129 | + return "light"; |
| 130 | +} |
0 commit comments