Skip to content

Add dark mode theme persistence script to VitePress config#65

Merged
amavashev merged 1 commit intomainfrom
claude/fix-dark-mode-first-visit-dw2ib
Mar 16, 2026
Merged

Add dark mode theme persistence script to VitePress config#65
amavashev merged 1 commit intomainfrom
claude/fix-dark-mode-first-visit-dw2ib

Conversation

@amavashev
Copy link
Contributor

Summary

Added a script to the VitePress head configuration that persects the user's theme preference (dark/light mode) by reading from localStorage and applying the appropriate CSS class on page load.

Key Changes

  • Injected an inline script into the document head that:
    • Retrieves the stored theme preference from localStorage using the key 'vitepress-theme-appearance'
    • Applies the 'dark' class to the document root element if the stored preference is not 'light'
    • Executes immediately on page load to prevent theme flash before content renders

Implementation Details

The script uses an IIFE (Immediately Invoked Function Expression) to avoid polluting the global scope and runs before the page content is rendered, ensuring the correct theme class is applied before any styles are evaluated. This prevents the common "flash of wrong theme" issue that occurs when theme preference is only applied after the DOM is fully loaded.

https://claude.ai/code/session_014BemMGykQoZYFErMakRahp

Add blocking inline script to <head> that applies the dark class
immediately before VitePress's own appearance script runs. This
prevents the flash-of-light-theme when a visitor's OS prefers light
mode but the site default is dark.

https://claude.ai/code/session_014BemMGykQoZYFErMakRahp
@amavashev amavashev merged commit 0771645 into main Mar 16, 2026
1 check passed
@amavashev amavashev deleted the claude/fix-dark-mode-first-visit-dw2ib branch March 22, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants