📎 Webclip
Dark Mode on the Web
The page presents dark mode as an accessibility feature for people who read at night or in dim light. It recommends respecting system preference first, then giving users control to switch between light, dark, and system settings. It also warns against flashes of the default theme and shows how to apply theme changes before rendering.
Reading notes#
- Dark mode is framed less as an aesthetic choice and more as a way to reduce eye strain and improve nighttime reading.
- A site should support dark mode with the
color-schememeta tag so the browser can adjust default styling. - The
prefers-color-schememedia query can be used to set custom colors, andwindow.matchMediacan detect support in JavaScript. - System preference should be respected unless the user explicitly selects a theme.
- A theme selector should offer three options: light, dark, and system preference.
- A Flash of Default Theme can happen when theme detection runs after the page renders.
- To avoid that flash, the theme can be read from
localStoragein blocking JavaScript and written to adata-themeattribute before rendering. - The article replaces React context for theming with CSS variables so most components do not need to re-render when the theme changes.
- Type-safe theme values can be defined in TypeScript and exposed as CSS variables.
- Global styles can combine
:root,data-theme, andprefers-color-schemeso default, user, and system settings all work together. - A hook can share theme state without context by combining persisted state, media-query detection, and a setter.
- Images, GIFs, and videos need separate treatment, including alternate assets, brightness and contrast filters, or inversion for suitable black-and-white images.
- Syntax highlighting can be generated at build time with theme-aware CSS variables.
- Embedded tweets can receive dark styling through
twitter:widgets:theme, though client-side embeds may flicker. - GitHub Gist embeds can use filter-based dark mode as a fallback until native support exists.
