-
Notifications
You must be signed in to change notification settings - Fork 146
feat: Enhanced theme switcher with system preference detection and tri-state selection #4639
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
rebeccaalpert
merged 8 commits into
patternfly:main
from
srambach:theme-switch-user-system-pref
Jun 16, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1d574c2
feat(theme): implement full-screen theme selector and custom hook forβ¦
srambach a6608c3
fix: Add comprehensive SSR-safe guards for matchMedia API - prevents β¦
srambach 7f435d1
feat(theme):address comments to remove unnecessary case stmts
srambach 4802511
feat(theme):fix aria label to include selection
srambach 3926a55
fix(theme): remove unnecessary export statement
srambach c5e4538
fix(theme): use consistent block style
srambach 95b8efb
fix(theme): change option wording to mode
srambach 68cb771
fix(theme): move theme switcher over
srambach 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { useState, useEffect, useCallback } from 'react'; | ||
|
|
||
| const THEME_MODES = { | ||
| SYSTEM: 'system', | ||
| LIGHT: 'light', | ||
| DARK: 'dark' | ||
| }; | ||
|
|
||
| const THEME_STORAGE_KEY = 'theme-preference'; | ||
| const DARK_MODE_CLASS = 'pf-v6-theme-dark'; | ||
|
|
||
| export const useTheme = () => { | ||
| const getStoredThemeMode = () => { | ||
| if (typeof window === 'undefined' || !window.localStorage) return null; | ||
thatblindgeye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return localStorage.getItem(THEME_STORAGE_KEY); | ||
| }; | ||
|
|
||
| const setStoredThemeMode = (mode) => { | ||
| if (typeof window === 'undefined' || !window.localStorage) return; | ||
| localStorage.setItem(THEME_STORAGE_KEY, mode); | ||
| }; | ||
|
|
||
| const getResolvedTheme = (mode) => { | ||
| if (typeof window === 'undefined') return 'light'; | ||
| if (mode === THEME_MODES.SYSTEM) { | ||
| return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | ||
| } | ||
| return mode; | ||
| }; | ||
|
|
||
| const updateThemeClass = (resolvedTheme) => { | ||
| if (typeof window === 'undefined') return; | ||
| const htmlElement = document.querySelector('html'); | ||
| if (resolvedTheme === 'dark') { | ||
| htmlElement.classList.add(DARK_MODE_CLASS); | ||
| } else { | ||
| htmlElement.classList.remove(DARK_MODE_CLASS); | ||
| } | ||
| }; | ||
|
|
||
| const [themeMode, setThemeModeState] = useState(() => { | ||
| const stored = getStoredThemeMode(); | ||
| return stored && Object.values(THEME_MODES).includes(stored) ? stored : THEME_MODES.SYSTEM; | ||
| }); | ||
|
|
||
| const [resolvedTheme, setResolvedTheme] = useState(() => getResolvedTheme(themeMode)); | ||
|
|
||
| const setThemeMode = useCallback((newMode) => { | ||
| setThemeModeState(newMode); | ||
| setStoredThemeMode(newMode); | ||
|
|
||
| const newResolvedTheme = getResolvedTheme(newMode); | ||
| setResolvedTheme(newResolvedTheme); | ||
| updateThemeClass(newResolvedTheme); | ||
| }, []); | ||
|
|
||
| // Listen for system preference changes | ||
| useEffect(() => { | ||
| if (typeof window === 'undefined') return; | ||
|
|
||
| const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); | ||
|
|
||
| const handleSystemThemeChange = (e) => { | ||
| if (themeMode === THEME_MODES.SYSTEM) { | ||
| const newSystemTheme = e.matches ? 'dark' : 'light'; | ||
| setResolvedTheme(newSystemTheme); | ||
| updateThemeClass(newSystemTheme); | ||
| } | ||
| }; | ||
|
|
||
| mediaQuery.addEventListener('change', handleSystemThemeChange); | ||
|
|
||
| return () => { | ||
| mediaQuery.removeEventListener('change', handleSystemThemeChange); | ||
| }; | ||
| }, [themeMode]); | ||
|
|
||
| // Initial theme application | ||
| useEffect(() => { | ||
| const initialResolvedTheme = getResolvedTheme(themeMode); | ||
| setResolvedTheme(initialResolvedTheme); | ||
| updateThemeClass(initialResolvedTheme); | ||
| }, [themeMode]); | ||
|
|
||
| return { | ||
| themeMode, | ||
| setThemeMode, | ||
| resolvedTheme, | ||
| THEME_MODES | ||
| }; | ||
| }; | ||
|
|
||
| export { THEME_MODES }; | ||
thatblindgeye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
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.