-
Notifications
You must be signed in to change notification settings - Fork 638
Set embedded app theme to match the site theme #1387
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
853bbc7
Load site with apps to match the current theme
sfc-gh-dmatthews 994f6e2
Get current theme from DOM
sfc-gh-dmatthews acca8a5
Reload Cloud components on theme change
sfc-gh-dmatthews 2ed49ae
Get embed string directly
sfc-gh-dmatthews a2a8a40
Add theme context
sfc-gh-dmatthews a7a7477
Reuse code
sfc-gh-tteixeira 263b697
Add comments
sfc-gh-tteixeira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { | ||
| createContext, | ||
| useContext, | ||
| useState, | ||
| useEffect, | ||
| useCallback, | ||
| } from "react"; | ||
|
|
||
| const ThemeContext = createContext(); | ||
|
|
||
| /** | ||
| * Updates all Streamlit Cloud iframes on the page with the new theme. | ||
| * This is extracted as a shared function so it can be called from multiple places. | ||
| */ | ||
| export function updateIframeThemes(theme) { | ||
| if (typeof document === "undefined") return; | ||
|
|
||
| const iframes = document.querySelectorAll('iframe[src*="streamlit.app"]'); | ||
| iframes.forEach((iframe) => { | ||
| iframe.src = getThemedUrl(iframe.src, theme); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the user's theme preference from localStorage or system preference. | ||
| * Returns "light" as default for SSR. | ||
| */ | ||
| function getUserPreference() { | ||
| if (typeof window === "undefined") { | ||
| return "light"; | ||
| } | ||
| if (window.localStorage.getItem("theme")) { | ||
| return window.localStorage.getItem("theme"); | ||
| } | ||
| return window.matchMedia("(prefers-color-scheme: dark)").matches | ||
| ? "dark" | ||
| : "light"; | ||
| } | ||
|
|
||
| export function ThemeContextProvider({ children }) { | ||
| // Initialize with "light" for SSR, will be updated on mount | ||
| const [theme, setThemeState] = useState("light"); | ||
| const [isInitialized, setIsInitialized] = useState(false); | ||
|
|
||
| // Apply theme to DOM and localStorage | ||
| const applyTheme = useCallback((newTheme) => { | ||
| if (typeof document === "undefined") return; | ||
|
|
||
| const inactiveTheme = newTheme === "light" ? "dark" : "light"; | ||
| document.documentElement.classList.add(newTheme); | ||
| document.documentElement.classList.remove(inactiveTheme); | ||
| localStorage.setItem("theme", newTheme); | ||
| }, []); | ||
|
|
||
| // Set theme and update everything | ||
| const setTheme = useCallback( | ||
| (newTheme) => { | ||
| setThemeState(newTheme); | ||
| applyTheme(newTheme); | ||
| updateIframeThemes(newTheme); | ||
| }, | ||
| [applyTheme], | ||
| ); | ||
|
|
||
| // Initialize theme on mount (client-side only) | ||
| useEffect(() => { | ||
| const preferredTheme = getUserPreference(); | ||
| setThemeState(preferredTheme); | ||
| applyTheme(preferredTheme); | ||
| setIsInitialized(true); | ||
| }, [applyTheme]); | ||
|
|
||
| return ( | ||
| <ThemeContext.Provider value={{ theme, setTheme, isInitialized }}> | ||
| {children} | ||
| </ThemeContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useThemeContext() { | ||
| const context = useContext(ThemeContext); | ||
| if (context === undefined) { | ||
| throw new Error( | ||
| "useThemeContext must be used within a ThemeContextProvider", | ||
| ); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| /** | ||
| * Safe version of useThemeContext that returns null if not within a provider. | ||
| * Useful for components that may be rendered outside the provider (e.g., SSR). | ||
| */ | ||
| export function useThemeContextSafe() { | ||
| return useContext(ThemeContext); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current theme from DOM (for use in non-React contexts or SSR fallback). | ||
| * Returns "light" as default if document is not available. | ||
| */ | ||
| export function getThemeFromDOM() { | ||
| if (typeof document !== "undefined") { | ||
| return document.documentElement.classList.contains("dark") | ||
| ? "dark" | ||
| : "light"; | ||
| } | ||
| return "light"; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a "light" or "dark" theme to a given Streamlit Cloud URL. | ||
| */ | ||
| export function getThemedUrl(url, theme) { | ||
| const themedUrl = new URL(url); | ||
| addThemeToSearchParams(themedUrl.searchParams, theme); | ||
| return themedUrl.toString(); | ||
| } | ||
|
|
||
| export function addThemeToSearchParams(searchParams, theme) { | ||
| const existingEmbedOptions = searchParams.getAll("embed_options"); | ||
|
|
||
| const nonThemeOptions = existingEmbedOptions.filter( | ||
| (option) => option !== "light_theme" && option !== "dark_theme", | ||
| ); | ||
|
|
||
| // Clear all embed_options and re-add the non-theme ones | ||
| searchParams.delete("embed_options"); | ||
| nonThemeOptions.forEach((option) => | ||
| searchParams.append("embed_options", option), | ||
| ); | ||
|
|
||
| searchParams.append("embed_options", `${theme}_theme`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.