Tailwind CSS darkMode strategy lacks built-in persistence for user theme selection
0 reputation · 21 Apr 2022, 11:14 UTC
0 reputation · 21 Apr 2022, 11:14 UTC
Tailwind CSS provides two darkMode strategies: media follows the operating system prefers-color-scheme query, while class requires a dark class on the root element. Neither strategy includes a native mechanism to store and restore a user's explicit theme choice across sessions.
The configuration reference documents the strategy values but does not define a persistence layer. A media strategy cannot honor a manual override without custom JavaScript that reads a stored preference and forces the dark class. The class strategy reverts to the default on every reload because the class is not reapplied automatically.
Server-rendered applications may send HTML with the wrong theme if persistence logic executes after the initial paint, causing a flash of incorrect styling. The core library offers no callback or event to synchronize client-side state before hydration.
darkMode function in tailwind.config.js safely read from localStorage or a cookie before the first paint?33275 reputation · 21 Apr 2022, 19:42 UTC
Tailwind’s darkMode can be set to media or class. Neither of those knobs stores a user’s choice. With media the OS preference is respected, but a manual toggle must be re‑applied on every page load. With class the dark class is only present when you add it yourself; the class disappears on a hard refresh because the state is lost.
class strategy. In tailwind.config.js set:
module.exports = {
darkMode: 'class',
// …other config
}
localStorage (client‑only). Using a cookie guarantees the server can render the correct class on the first request.<head> that runs as early as possible:
<script>
(function () {
const theme = document.cookie.match(/theme=([^;]+)/) ? RegExp.$1 : localStorage.getItem('theme');
if (theme === 'dark') {
document.documentElement.classList.add('dark');
}
})();
</script>
Because this script executes before the Tailwind CSS file is parsed, the browser will apply the correct styles immediately, eliminating the flash of the wrong theme.
theme cookie from the request and render <html class="dark"> when appropriate. If the cookie is missing, render the default <html>.localStorage and toggle the dark class on document.documentElement. Optionally trigger a full page reload to ensure server‑side rendering stays in sync.darkMode function read localStorage or a cookie?No. A darkMode function in tailwind.config.js runs at build time, not at runtime. It has no access to the browser’s localStorage or to cookies that arrive with the HTTP request. Therefore it cannot be used to decide whether the dark class should be applied on the first paint.
class mode and apply the dark class via an early inline script or server‑side cookie logic.darkMode functions cannot help with runtime persistence.Do you currently store the theme in a cookie that is sent with the initial request, or only in localStorage?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.