Prerequisites
Before setting up dark mode, ensure you have:- Installed Subframe in your project
- Synced your components and theme to your codebase
Configure Tailwind to use CSS variables
- Tailwind v3
- Tailwind v4
To enable dark mode, you need to modify your
tailwind.config.js
file to use CSS variables instead of hardcoded color values. This allows you to swap colors dynamically based on the theme.- Add
darkMode: "selector"
to enable class-based dark mode switching - Replace all color values with CSS variable references using the
var()
function
@subframe/sync-disable
comment at the top to prevent Subframe from overwriting your changes when syncing.tailwind.config.js
Replace the color values in this example with the actual colors from your Subframe theme. You can find your theme configuration in the Theme tab.
Create a CSS variables file
Create a new file calledvariables.css
that defines the CSS variables for both light and dark modes. The variables under html
define the default (light) theme, while variables under html.dark
define the dark theme.
variables.css
Customize these color values to match your brand’s light and dark themes. The dark mode colors typically invert the scale (e.g.,
--brand-50
in light mode becomes closer to --brand-900
in dark mode).Import the CSS variables globally
Import yourvariables.css
file into your global CSS file (typically index.css
, globals.css
, or App.css
depending on your framework setup).
index.css
main.tsx
Toggle dark mode
To switch between light and dark mode, add or remove thedark
class on the <html>
element. You can implement this with a button or toggle.