Fix header theme flash when toggling theme in footer#496
Open
vimzh wants to merge 1 commit intopayloadcms:mainfrom
Open
Fix header theme flash when toggling theme in footer#496vimzh wants to merge 1 commit intopayloadcms:mainfrom
vimzh wants to merge 1 commit intopayloadcms:mainfrom
Conversation
The footer's theme toggle was directly calling setHeaderTheme(), which immediately set the header to the user's chosen theme. This caused the navbar to briefly flash the wrong color before the IntersectionObserver corrected it based on the currently visible section's actual theme. The fix removes direct setHeaderTheme() calls from both Footer and CloudFooter components. The global setTheme() already triggers the observer recreation chain (via its dependency array), which determines the correct header theme based on visible sections. Also fixes a missing `theme` dependency in HeaderIntersectionObserver's pathname-reset effect, ensuring the header updates when the global theme preference changes.
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.
Why
When a user scrolls to the bottom of the landing page and toggles the theme selector (e.g. from Auto to Light), the top navbar briefly flashes the selected theme color before reverting on scroll. This happens because the footer's
onThemeChangehandler directly callssetHeaderTheme(), which immediately overrides the header's theme — but theIntersectionObserverthen corrects it back based on the currently visible section'sdata-themeattribute.Steps to reproduce:
Approach
The header theme should be determined solely by the
IntersectionObserverbased on the currently visible section, not directly manipulated by the footer. The globalsetTheme()call already triggers the observer recreation chain (via its dependency array), which re-evaluates the correct header theme. The directsetHeaderTheme()calls create a transient inconsistent state and are unnecessary.How it works
Remove
setHeaderTheme()calls from Footer and CloudFooter — Both components were importinguseHeaderObserversolely to callsetHeaderTheme()alongsidesetTheme(). ThesetTheme()call is sufficient: it updates the global theme preference, which theHeaderIntersectionObserveralready depends on to recreate its observer and re-evaluate header theme.Fix missing
themedependency inHeaderIntersectionObserver— The pathname-reset effect (setHeaderTheme(theme)) only had[pathname]in its dependency array, meaning it could use a stalethemevalue. Addingthemeensures the header also updates when the global theme preference changes (covering edge cases where noIntersectionObserverelement is in view).