Skip to content

feat: add dark mode toggle to nav bar with localStorage persistence - #760

Merged
Chucks1093 merged 1 commit into
accesslayerorg:devfrom
stableprogrammer:feat/dark-mode-toggle-750
Aug 24, 2026
Merged

feat: add dark mode toggle to nav bar with localStorage persistence#760
Chucks1093 merged 1 commit into
accesslayerorg:devfrom
stableprogrammer:feat/dark-mode-toggle-750

Conversation

@stableprogrammer

Copy link
Copy Markdown
Contributor

Closes #750

Summary

  • Adds a useTheme hook that reads the initial theme from localStorage, falls back to the OS prefers-color-scheme when no stored value exists, applies the dark class to <html> on every change, and persists the chosen value under the theme key in localStorage.
  • Adds a Sun/Moon icon toggle button to the right-side actions in the nav bar that switches between light and dark themes on click.
  • Adds a flash-prevention inline script to <head> in index.html that runs before the first paint and applies the stored or OS-derived theme class to <html> immediately, preventing a white flash on dark-mode page load.
  • Adds 15 unit tests covering: initial theme resolution from localStorage and OS preference, invalid stored value fallback, dark class addition and removal on <html>, localStorage persistence on mount and after toggling, and toggle state transitions.

Test plan

  • Toggle button shows Moon icon in light mode and Sun icon in dark mode
  • Clicking the toggle switches the dark class on <html> and updates the UI
  • Preference is written to localStorage['theme'] after each toggle
  • Reloading the page applies the stored theme without a flash
  • First visit with no stored preference applies the OS prefers-color-scheme setting
  • All 15 unit tests for useTheme pass

@Chucks1093

Copy link
Copy Markdown
Member

The inline script in the head for flash prevention is the right approach here. Doing it at the hook level would always be too late to avoid that brief white flash on dark mode page load. One thing worth checking is whether the invalid stored value fallback test covers what happens when a user has a stale value from an older version of the app that used a different storage key or format.

@Chucks1093
Chucks1093 merged commit 532fa6a into accesslayerorg:dev Aug 24, 2026
1 check passed
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.

Add a dark mode toggle to the nav bar persisting the user's preference in localStorage

2 participants