Performance: Class-based dark mode strategy
This affects page load speed and rendering performance by controlling when and how dark mode styles are applied.
/* Using class-based dark mode */ @tailwind base; @tailwind components; @tailwind utilities; /* Tailwind config: darkMode: 'class' */ <body class="dark"> <div class="bg-white dark:bg-gray-900 text-black dark:text-white"> Hello World </div> </body>
/* Using media query dark mode */ @tailwind base; @tailwind components; @tailwind utilities; /* Tailwind config: darkMode: 'media' */ <body> <div class="bg-white dark:bg-gray-900 text-black dark:text-white"> Hello World </div> </body>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Media query dark mode | No extra DOM nodes | Triggers reflow on system theme change | Medium paint cost due to style recalculation | [X] Bad |
| Class-based dark mode | Adds one class to <body> | Single reflow on class toggle | Lower paint cost, stable layout | [OK] Good |
dark: prefix in Tailwind CSS do when using class-based dark mode?dark: prefix roledark: prefix in Tailwind applies styles only when the dark class is present on a parent element.dark class presence.dark class. -> Option Adark: prefix = styles with dark class [OK]dark: works only if dark class exists [OK]dark: auto-detects system dark modedark classdark: with global dark mode toggledarkMode: 'class' in tailwind.config.js.darkMode: 'media' uses system preference, not class. The dark class must be added manually; no dark attribute exists. The class name must be exactly dark.<html class="dark">
<body>
<div class="bg-white dark:bg-gray-800 text-black dark:text-white p-4">Hello</div>
</body>
</html>dark class<html> tag has the dark class, so all dark: styles apply.div has bg-white and text-black normally, but with dark:bg-gray-800 and dark:text-white when dark class is present. So background is gray-800 and text is white.dark class exists, dark: styles override [OK]dark class on htmldark class on body. Which of these JavaScript snippets correctly toggles dark mode?classList.toggle('className').classList.toggle('dark'), which is correct. document.body.toggleClass('dark') uses a non-existent toggleClass method. document.body.className = 'dark' overwrites all classes, removing others. document.body.setAttribute('dark', true) sets an attribute, not a class.dark class on the html element. Which React code snippet correctly implements this using Tailwind's class-based dark mode?dark class on the html element (document.documentElement). Toggling means adding or removing the class, so classList.toggle is best.dark on html correctly. const toggleDark = () => { document.body.classList.add('dark'); };
return Toggle Dark; only adds dark to body (no toggle). const toggleDark = () => { document.documentElement.setAttribute('dark', 'true'); };
return Toggle Dark; sets an attribute instead of class. const toggleDark = () => { document.body.className = 'dark'; };
return Toggle Dark; overwrites all classes on body, removing others.dark class on html with classList.toggle [OK]dark class on html using classList.toggle [OK]bodyPass the test and this pattern moves unknown