-
Notifications
You must be signed in to change notification settings - Fork 37
fix(theming): allow three states in useTheme #656
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
Open
AugustinMauroy
wants to merge
5
commits into
main
Choose a base branch
from
fix-theme-toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−35
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,49 +1,62 @@ | ||
| import { useState, useEffect, useCallback } from 'react'; | ||
|
|
||
| /** @returns {'dark'|'light'} The current OS-level color scheme. */ | ||
| const getSystemTheme = () => | ||
| matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | ||
|
|
||
| /** | ||
| * Applies the given theme to the `<html>` element's `data-theme` attribute | ||
| * and persists the theme preference in `localStorage`. | ||
| * | ||
| * @param {string} theme - The theme to apply ('light' or 'dark'). | ||
| * Applies a theme to the document root. | ||
| * Resolves 'system' to the actual OS preference before applying. | ||
| * @param {'system'|'light'|'dark'} pref - The theme preference. | ||
| */ | ||
| const applyTheme = theme => { | ||
| const applyTheme = pref => { | ||
| const theme = pref === 'system' ? getSystemTheme() : pref; | ||
| document.documentElement.setAttribute('data-theme', theme); | ||
| document.documentElement.style.colorScheme = theme; | ||
| localStorage.setItem('theme', theme); | ||
| }; | ||
|
|
||
| /** | ||
| * A React hook for managing the application's light/dark theme. | ||
| * Applies the system theme to the document root. | ||
| */ | ||
| const applySystemTheme = () => applyTheme('system'); | ||
|
|
||
| /** | ||
| * React hook for managing theme preference. | ||
| * Persists the choice to localStorage and listens for OS theme changes | ||
| * when set to 'system'. | ||
| * @returns {['system'|'light'|'dark', (next: 'system'|'light'|'dark') => void]} | ||
| */ | ||
| export const useTheme = () => { | ||
| const [theme, setTheme] = useState('light'); | ||
| // Read stored preference once on mount; default to 'system'. | ||
| const [pref, setPref] = useState(() => { | ||
| if (typeof window === 'undefined') { | ||
| return 'system'; | ||
| } | ||
|
|
||
| return localStorage.getItem('theme') || 'system'; | ||
| }); | ||
|
|
||
| // Apply theme on every preference change, and if 'system', | ||
| // also listen for OS-level color scheme changes. | ||
| useEffect(() => { | ||
| const initial = | ||
| // Try to get the theme from localStorage first. | ||
| localStorage.getItem('theme') || | ||
| // If not found, check the `data-theme` attribute on the document element | ||
| document.documentElement.getAttribute('data-theme') || | ||
| // As a final fallback, check the user's system preference for dark mode. | ||
| (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); | ||
|
|
||
| applyTheme(initial); | ||
| setTheme(initial); | ||
| }, []); | ||
| applyTheme(pref); | ||
|
|
||
| if (pref !== 'system') { | ||
| return; | ||
| } | ||
|
|
||
| const mql = matchMedia('(prefers-color-scheme: dark)'); | ||
| mql.addEventListener('change', applySystemTheme); | ||
| return () => mql.removeEventListener('change', applySystemTheme); | ||
| }, [pref]); | ||
|
|
||
| /** | ||
| * Callback function to toggle between 'light' and 'dark' themes. | ||
| */ | ||
| const toggleTheme = useCallback(() => { | ||
| setTheme(prev => { | ||
| // Determine the next theme based on the current theme. | ||
| const next = prev === 'light' ? 'dark' : 'light'; | ||
| // Apply the new theme. | ||
| applyTheme(next); | ||
| // Return the new theme to update the state. | ||
| return next; | ||
| }); | ||
| /** Updates the preference in both React state and localStorage. */ | ||
| const setTheme = useCallback(next => { | ||
| setPref(next); | ||
| if (typeof window !== 'undefined') { | ||
| localStorage.setItem('theme', next); | ||
| } | ||
|
Comment on lines
+56
to
+58
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
| }, []); | ||
|
|
||
| return [theme, toggleTheme]; | ||
| return [pref, setTheme]; | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the if?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because node/rolldown was trying to execute the local storage on the building time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably exit the hook early with a
SERVERcodepath.if (SERVER) return ['system', ()=>{}]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be better ways to systematically omit something to run from a server code-path. 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"use client" work ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but any if statement
if (CLIENT)orif (SERVER)will only run the code in the block on the client/server, respectively (with code elimination)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right ! what is your idea to define CLIENT and SERVER bool ?