Dark mode change detection

Javascript

Respond to appearance changes in system preference live in the browser in macOS 10.14+ and iOS 13+.

javascript

window.matchMedia('(prefers-color-scheme: dark)').addListener(({ matches }) => { if (matches) { console.log("dark theme"); } else { console.log("light theme"); } });

Hint: try toggling dark mode on and off and observe this website 😉