June 28th, 2024

Notes on implementing dark mode

The article explores refining dark mode implementation on websites using Tailwind for styling. It emphasizes a tri-state setting (dark, light, system default), local storage usage, and technical considerations for a seamless experience.

Read original articleLink Icon
Notes on implementing dark mode

The article discusses the implementation of dark mode on a website, specifically focusing on the refinements needed for a great dark mode experience. It mentions using Tailwind for styling and covers key concepts like the prefers-color-scheme CSS media selector and local storage for storing user preferences. The author emphasizes the importance of a tri-state setting for dark mode (dark, light, or system default) and provides code snippets for achieving this functionality. Additionally, it addresses common issues like avoiding theme flicker on page load by checking the theme initially with inline JavaScript. The article highlights the benefits of using local storage for persistent user settings and explains the technical considerations for a smooth dark mode implementation.

Related

Exposition of Front End Build Systems

Exposition of Front End Build Systems

Frontend build systems are crucial in web development, involving transpilation, bundling, and minification steps. Tools like Babel and Webpack optimize code for performance and developer experience. Various bundlers like Webpack, Rollup, Parcel, esbuild, and Turbopack are compared for features and performance.

Microfeatures I love in blogs and personal websites

Microfeatures I love in blogs and personal websites

The article explores microfeatures for blogs and websites inspired by programming concepts. It highlights sidenotes, navigation tools, progress indicators, and interactive elements to improve user experience subtly. Examples demonstrate practical implementations.

Local First, Forever

Local First, Forever

Local-first software emphasizes storing data on the user's device, occasionally syncing with the internet. Benefits include data control, but challenges like syncing between devices and company reliability exist. Using cloud services like Dropbox for syncing, including CRDT for conflict handling, is recommended for simplicity and reliability.

Google no longer developing Material Web Components

Google no longer developing Material Web Components

Material Web is a library of web components based on Material 3 for creating attractive and accessible web apps. Components are in maintenance mode, seeking new maintainers. Visit the Material Web GitHub for details.

HSL: An Intuitive Way to Represent Color in CSS

HSL: An Intuitive Way to Represent Color in CSS

HSL in CSS simplifies color representation by breaking it into hue, saturation, and lightness components. It enables easy color manipulation and transparency adjustments, aiding dynamic effects without external tools or JavaScript.

Link Icon 9 comments
By @jayflux - 5 months
We are working on a W3C proposal to hopefully improve both the DX and UX of setting dark/light so that developers can avoid hacks around FOUC, doubling up all CSS and needing JS on every page load to check the state.

You can read more about it here: https://github.com/WICG/web-preferences-api

Feedback is welcome

By @lawn - 5 months
The first thing I noticed is that the site didn't respect my dark mode preference because I have JavaScript disabled.

So there's still some work left here until I'd call the implementation "great".

(A simple css check for prefers-color-scheme handles this.)

By @dave4420 - 5 months
Something I don’t understand is: why do we need to allow the user to explicitly set dark mode or light mode? Why can’t we just check prefers-color-scheme and leave it at that?
By @codetrotter - 5 months
Overall pretty neat. Not a huge fan of the three-way toggle though. Two-way toggles are plenty confusing already.
By @globular-toast - 5 months
Yep, this is the way. You can test it works in Firefox dev tools by forcing the dark/light browser settings (you can set it to print too to test your print stylesheet).

It's a shame there isn't a standard high contrast setting, though. That seems more important than dark mode. Oh well.

By @meribold - 5 months
There's one more detail along the lines of theme changes in other tabs and theme preference changes from the OS/browser: activating the correct theme following history navigation. When I navigate to another page of the author's site, change the theme from light to dark, and then use my browser's back button, I get the light theme again. On my own site I use the `pageshow` event to fix this (https://github.com/meribold/meribold.org/blob/afe6885/_inclu...).

The extra effort is only needed on Firefox and Safari, though. Chromium already handles this somehow.

By @arcanemachiner - 5 months
Nicely done. Very thorough.

Now I have to check if my implementation supports cross-tab changes...

By @a1o - 5 months
Are you the author? I am curious about how people write TextMate grammars. I can't really find (good) resources on it and I find it hard to reverse engineer existing grammars to understand them.