The Perfect Dark Mode
summary
Shows how to eliminate the white flicker by running a blocking inline script in the HTML (Next.js _document) that uses an immediately-invoked function to read localStorage and window.matchMedia, then writes values into CSS variables and a data-theme attribute before paint. The code persists user choice, falls back to system preference and light if absent, defers rendering the toggle until state is known, and removes the flash while preserving correct theme and toggle state.
AI-written summary · about
chapters (15)
- 0:00Introduction and speaker intro
- 1:04What is perfect dark mode
- 3:32Basic dark mode toggle example
- 4:57Storing user preferences
- 6:02System preference detection
- 7:25Preventing the white flash
- 9:09Blocking scripts in HTML
- 10:39Immediately-invoked functions
- 12:05Setting initial color mode
- 13:07CSS variables for themes
- 15:59Passing preference to document
- 16:42Recap of the solution
- 17:26Putting it all together
- 18:50Final implementation and demo
- 19:12Resources and closing
