fix: resolvedTheme from useTheme() now reflects forcedTheme - #383
Open
i8ramin wants to merge 1 commit into
Open
Conversation
When forcedTheme is set on ThemeProvider, useTheme().resolvedTheme should return the forced theme value. Previously it was computed as: resolvedTheme: theme === 'system' ? resolvedTheme : theme This ignores forcedTheme entirely — consumers reading resolvedTheme get the user's stored preference instead of the active forced theme. Fix: prepend forcedTheme with nullish coalescing: resolvedTheme: forcedTheme ?? (theme === 'system' ? resolvedTheme : theme) Note: the v1 branch attempted this same fix but has an operator precedence bug (missing parentheses around the ternary). This PR includes the correct precedence. Also adds a Playwright test that verifies resolvedTheme from useTheme() matches the forced theme on forced-theme pages. Closes pacocoursey#252
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
useTheme().resolvedThemeignoresforcedTheme— consumers get the user's stored preference instead of the active forced theme.Closes #252
The Bug
When
forcedThemeis set onThemeProvider, the DOM is updated correctly (thedata-themeattribute andcolor-schemestyle reflect the forced value), but the React context value forresolvedThemeis wrong:This forces consumers to read
forcedThemedirectly and fall back manually:Root Cause
In
next-themes/src/index.tsx, the provider value computes:This ignores
forcedThemeentirely. Thethemestate variable holds the user's stored preference, not the forced value.Fix
One-line change — prepend
forcedThemewith nullish coalescing and correct parentheses:Note on the v1 branch
The
v1branch attempted this same fix but has an operator precedence bug — the ternary is not wrapped in parentheses:This PR uses the correct precedence.
Test
Added a Playwright test that verifies
resolvedThemefromuseTheme()matches the forced theme on forced-theme pages (the existing tests only checked DOM attributes, which is why this bug wasn't caught).Impact
forcedThemeis still available as a separate fieldapplyTheme)