Skip to content
Astro Eclipse
Esc
↑↓navigate↵open⌘Jpreview
On this page

Tailwind

Use dark mode utilities with Astro Eclipse, with the class attribute and the Tailwind 4 custom variant.

Tailwind’s dark: utilities need to know what “dark” means in your page. There are two setups. Both work with Astro Eclipse; pick one.

Option 1: class attribute

Tell Astro Eclipse to toggle classes on <html> instead of setting data-theme:

import eclipse from 'astro-eclipse'

export default defineConfig({
  integrations: [eclipse({ attribute: 'class' })],
})

With themes: ['light', 'dark'] the element ends up with the class light or dark, and the other one is removed. Any other classes on <html> are left alone.

Then declare the variant in Tailwind 4:

@import 'tailwindcss';

@custom-variant dark (&:where(.dark, .dark *));
<body class="bg-white text-black dark:bg-black dark:text-white"></body>

Different class names than the themes

When the class must not be the theme name (a dark theme that adds theme-dark, or several classes at once), map it with values. Storage and the API keep the theme names.

eclipse({
  attribute: 'class',
  themes: ['light', 'dark'],
  values: { dark: 'dark theme-night' },
})

The theme dark then puts the classes dark and theme-night on <html>, and the classes of every other theme are removed first. A theme without an entry uses its own name. The same option works with data-theme: { dark: 'theme-dark' } writes data-theme="theme-dark".

Option 2: keep data-theme

If you would rather keep the default attribute, point the variant at it. No attribute option is needed:

export default defineConfig({
  integrations: [eclipse()],
})
@import 'tailwindcss';

@custom-variant dark (&:where([data-theme='dark'], [data-theme='dark'] *));

Which config to choose

Setup Astro Eclipse config Tailwind 4 CSS
Class eclipse({ attribute: 'class' }) @custom-variant dark (&:where(.dark, .dark *));
Attribute eclipse() @custom-variant dark (&:where([data-theme='dark'], [data-theme='dark'] *));

The attribute setup keeps your CSS variables and Tailwind on the same selector, which makes it the simpler one when you use both. The class setup matches what most Tailwind tutorials show.

The names in the variant must match your themes. If you rename dark to night, the selector becomes .night or [data-theme='night'].

Related: CSS variables, config reference.

Was this page helpful?