<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sheryar Ahmed</title>
    <description>The latest articles on DEV Community by Sheryar Ahmed (@sheryar_ahmed).</description>
    <link>https://dev.to/sheryar_ahmed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2307239%2F55509ae9-f4f7-491a-93f4-95188b650c33.jpg</url>
      <title>DEV Community: Sheryar Ahmed</title>
      <link>https://dev.to/sheryar_ahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sheryar_ahmed"/>
    <language>en</language>
    <item>
      <title>I added dark mode by editing CSS variables not 100 components</title>
      <dc:creator>Sheryar Ahmed</dc:creator>
      <pubDate>Tue, 30 Jun 2026 13:21:06 +0000</pubDate>
      <link>https://dev.to/sheryar_ahmed/i-added-dark-mode-by-editing-css-variables-not-100-components-3n8o</link>
      <guid>https://dev.to/sheryar_ahmed/i-added-dark-mode-by-editing-css-variables-not-100-components-3n8o</guid>
      <description>&lt;h1&gt;
  
  
  I added dark mode by editing CSS variables - not 100 components
&lt;/h1&gt;

&lt;p&gt;My app had ~1,200 hardcoded &lt;code&gt;indigo-*&lt;/code&gt; and ~2,600 hardcoded &lt;code&gt;slate-*&lt;/code&gt; class usages across&lt;br&gt;
130-odd components. The brief was small to say and brutal to do: &lt;em&gt;"ship dark mode, and&lt;br&gt;
let me rebrand the accent color whenever I want."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The naive plan is a sweep: open every component, add &lt;code&gt;dark:&lt;/code&gt; variants, swap colors. That's&lt;br&gt;
weeks of work, and worse, it's &lt;em&gt;permanent&lt;/em&gt; work - every new component re-incurs it, and&lt;br&gt;
the design drifts the moment two people touch it. I wanted dark mode to be a property of&lt;br&gt;
the &lt;strong&gt;theme&lt;/strong&gt;, not of every component.&lt;/p&gt;
&lt;h2&gt;
  
  
  The decision
&lt;/h2&gt;

&lt;p&gt;Two ideas, both "edit the tokens, not the markup":&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. One brand token.&lt;/strong&gt; Instead of components knowing the color &lt;code&gt;indigo&lt;/code&gt;, they reference a&lt;br&gt;
semantic &lt;code&gt;brand&lt;/code&gt; scale. I did a one-time codemod - &lt;code&gt;indigo- → brand-&lt;/code&gt; across &lt;code&gt;app/&lt;/code&gt;,&lt;br&gt;
&lt;code&gt;components/&lt;/code&gt;, &lt;code&gt;lib/&lt;/code&gt; (~1,230 mechanical edits, no logic change) - and defined the brand&lt;br&gt;
scale once as CSS variables. Now recoloring the entire app, globally or per-user, is a&lt;br&gt;
variable swap. No component ever needs editing again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--brand-50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;#EEF4FF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* … */&lt;/span&gt; &lt;span class="py"&gt;--brand-500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;#0066FF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="py"&gt;--brand-600&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;#0052D6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* … */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;/* Tailwind v4: expose them so bg-/text-/border-/ring-brand-* all resolve to the vars */&lt;/span&gt;
&lt;span class="k"&gt;@theme&lt;/span&gt; &lt;span class="nb"&gt;inline&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--color-brand-500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--brand-500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="py"&gt;--color-brand-600&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--brand-600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c"&gt;/* …the full scale… */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Per-user accent "vibes" then become trivial - each preset just re-defines that scale under&lt;br&gt;
an attribute selector, so picking one recolors the app instantly with zero component churn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-accent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"emerald"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;--brand-500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;#059669&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="py"&gt;--brand-600&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;#047857&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* … */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Dark mode as a token remap, not a component sweep.&lt;/strong&gt; This is the part that surprised&lt;br&gt;
me.&lt;/p&gt;
&lt;h2&gt;
  
  
  The mechanism (and the wrong turn I took first)
&lt;/h2&gt;

&lt;p&gt;My first attempt failed silently. I tried mapping Tailwind's slate scale through my own&lt;br&gt;
indirection in &lt;code&gt;@theme inline&lt;/code&gt; - and text stayed dark in dark mode. Tailwind's &lt;em&gt;default&lt;/em&gt;&lt;br&gt;
&lt;code&gt;slate-*&lt;/code&gt; utilities weren't picking up my override.&lt;/p&gt;

&lt;p&gt;Then I read the compiled CSS, and the trick fell out of it. Tailwind v4 emits its own&lt;br&gt;
palette as real CSS variables, and the utilities reference them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.text-slate-900&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--color-slate-900&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I don't need to touch a single utility. I just &lt;strong&gt;redefine the slate ramp inside the&lt;br&gt;
&lt;code&gt;.dark&lt;/code&gt; selector&lt;/strong&gt; - reversed, so "dark text" becomes light and "light background" becomes&lt;br&gt;
dark:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.dark&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--color-slate-900&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f1f5f9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* was near-black → now near-white text */&lt;/span&gt;
  &lt;span class="py"&gt;--color-slate-700&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#cbd5e1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--color-slate-200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#3a4659&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* was light border → now a dark border */&lt;/span&gt;
  &lt;span class="py"&gt;--color-slate-50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;#273345&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* was page-white → now an elevated surface */&lt;/span&gt;
  &lt;span class="py"&gt;color-scheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dark&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c"&gt;/* native scrollbars/date pickers follow too */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;text-slate-900&lt;/code&gt;, &lt;code&gt;bg-slate-50&lt;/code&gt;, &lt;code&gt;border-slate-200&lt;/code&gt; in the codebase flips&lt;br&gt;
automatically. Thousands of usages, one block of CSS.&lt;/p&gt;
&lt;h2&gt;
  
  
  The gotcha
&lt;/h2&gt;

&lt;p&gt;A reversed ramp isn't a straight inversion - &lt;strong&gt;surface hierarchy&lt;/strong&gt; breaks if you're naive.&lt;br&gt;
My first ramp made &lt;code&gt;slate-50&lt;/code&gt; darker than my card color, so every "subtle gray" inner box&lt;br&gt;
(inside drawers and cards) suddenly looked like the page bleeding &lt;em&gt;through&lt;/em&gt; the card. The&lt;br&gt;
fix was to treat the low slate shades as &lt;strong&gt;elevated&lt;/strong&gt; surfaces that sit &lt;em&gt;above&lt;/em&gt; the card,&lt;br&gt;
not below it: &lt;code&gt;canvas (#0b1120) &amp;lt; card (#1e293b) &amp;lt; slate-50/100/200&lt;/code&gt;. Dark mode isn't&lt;br&gt;
"flip the colors" - it's "preserve the depth ordering with a dark palette." Miss that and&lt;br&gt;
everything looks flat and muddy.&lt;/p&gt;

&lt;p&gt;A second, smaller one: native &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; option lists. Components set a dark background on&lt;br&gt;
the control, but the option popup inherited the dark bg &lt;em&gt;without&lt;/em&gt; a light text color -&lt;br&gt;
dark-on-dark, invisible. One global rule fixed all of them at once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;select&lt;/span&gt; &lt;span class="nt"&gt;option&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--card&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--card-foreground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I'd do next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A no-flash inline script in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; to set the theme before first paint (right now a
provider gates render, which avoids the flash but costs a frame).&lt;/li&gt;
&lt;li&gt;Audit the handful of &lt;em&gt;intentionally&lt;/em&gt; dark elements (a dark CTA, a tooltip) that I had to
pin to fixed colors so the remap couldn't invert them - those are the exceptions that
prove the rule.&lt;/li&gt;
&lt;li&gt;A contrast-ratio check in CI so a future accent preset can't ship an unreadable pair.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The meta-lesson: if changing a visual property means editing many files, the property is&lt;br&gt;
in the wrong layer. Push color and theme into tokens, and "rebrand the app" or "add dark&lt;br&gt;
mode" turns from a sprint into a diff.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What's your dark-mode strategy - &lt;code&gt;dark:&lt;/code&gt; everywhere, CSS variables, or something else?&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>tailwindcss</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
