Vanilla JavaScript Color Scheme Switcher

Introduction

This page is a prototype for making a dark/light mode color scheme switcher that prevents flashes of unstyled content (FOUC) and loading blink as much as possible. I'm doing a full write up as well that I'll published when everything is finalized.

Goals

  1. Use Vanilla JavaScript that can be used with static site generators
  2. Save the user's preferred scheme between visits
  3. Avoid Flash Of Unstyled Content (FOUC) as much as possible
  4. Avoid browser flash when loading a new page as much as possible
  5. Prevent the Firefox console message "Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content." as much as possible
  6. Only show color scheme picker if JavaScript is enabled
  7. Only show 'auto' button if there is data for it
  8. Default to light mode color scheme if JavaScript is not enabled
  9. Provide for multiple instances of the switcher that stay in sync
  10. Update the color-scheme CSS property to style form controls, etc...

Files

The source code for this site is on GitHub at:

https://github.com/alanwsmith/color-scheme-switcher.alanwsmith.com

The individual files are:

Converter

VS Code Theme Converter