Steve Kinney

Building Design Systems with Storybook

Adding Dark Mode to Our Components

There are a number of ways that we can add dark mode to our component, but let’s start by leveraging the path of least resistance using Tailwind since we already laid the groundwork earlier.

Tailwind supports a utility called dark: which only triggers the class when whatever we chose to trigger dark mode kicks into gear.

For example, this would apply the second class if-and-only-if we’re in dark mode.

<button class="bg-primary-600 dark:bg-primary-500">Button</button>

According to our design system, we’re using the following values for light and dark mode.

NameLightDark
Button / Primary
DefaultPrimary/600Primary/500
HoverPrimary/500Primary/400
ActivePrimary/400Primary/300
LabelWhiteWhite
BorderTransparentTransparent
Button / Secondary
DefaultWhiteSlate/900
HoverSlate/50Slate/800
ActiveSlate/100Slate/700
LabelSlate/900White
BorderSlate/300Slate/900
Button / Destructive
DefaultDanger/600Danger/500
HoverDanger/500Danger/400
ActiveDanger/400Danger/300
LabelWhiteWhite
BorderTransparentTransparent
Button / Ghost
DefaultTransparentTransparent
HoverSlate/50Slate/700
ActiveSlate/100Slate/700
LabelSlate/900White
BorderTransparentTransparent

So, we could update our component as follows:

@@ -33,6 +33,9 @@ export const variants = cva(
           'border-transparent',
           'hover:bg-primary-500',
           'active:bg-primary-400',
+          'dark:bg-primary-500',
+          'dark:hover:bg-primary-400',
+          'dark:active:bg-primary-300',
         ],
         secondary: [
           'bg-white',
@@ -40,6 +43,11 @@ export const variants = cva(
           'border-slate-300',
           'hover:bg-slate-50',
           'active:bg-slate-100',
+          'dark:bg-slate-900',
+          'dark:text-white',
+          'dark:border-slate-900',
+          'dark:hover:bg-slate-800',
+          'dark:active:bg-slate-700',
         ],
         destructive: [
           'bg-danger-600',
@@ -47,6 +55,9 @@ export const variants = cva(
           'border-transparent',
           'hover:bg-danger-500',
           'active:bg-danger-400',
+          'dark:bg-danger-500',
+          'dark:hover:bg-danger-400',
+          'dark:active:bg-danger-300',
         ],
       },
       size: {

Last modified on .