Skip to content

docs: add Tailwind v4 data-theme styling guidance - #395

Open
luisangelrod wants to merge 2 commits into
pacocoursey:mainfrom
luisangelrod:docs/tailwind-data-theme-avoids-hydration
Open

docs: add Tailwind v4 data-theme styling guidance#395
luisangelrod wants to merge 2 commits into
pacocoursey:mainfrom
luisangelrod:docs/tailwind-data-theme-avoids-hydration

Conversation

@luisangelrod

@luisangelrod luisangelrod commented Jun 24, 2026

Copy link
Copy Markdown

Summary

Fixes #364

Adds a Tailwind v4 example that uses next-themes' default data-theme attribute with @custom-variant.

For CSS-only theme styling, this lets Tailwind respond to the attribute set by next-themes' injected browser script without branching rendered markup on useTheme. The example keeps <html suppressHydrationWarning>, and the docs still direct readers to the mounted pattern when useTheme values change rendered markup.

What's added

globals.css

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

ThemeProvider — no attribute prop is needed because data-theme is the default:

<html suppressHydrationWarning>
  <body>
    <ThemeProvider>{children}</ThemeProvider>
  </body>
</html>

Tailwind dark classes then work normally:

<h1 className="text-black dark:text-white">Hello</h1>

Validation

  • prettier next-themes/README.md --check
  • git diff --check

The README's 'Avoid Hydration Mismatch' section only documents the
mounted-state workaround. But when using Tailwind v4 with next-themes'
default data-theme attribute, hydration mismatch can be avoided
entirely — because the attribute is set by next-themes' injected inline
script before React hydrates, so component class names never change
between server and client render.

Fixes pacocoursey#364

Adds a new '#### Tailwind v4 with data attributes' subsection under
'With TailwindCSS' showing:
- The @custom-variant dark CSS rule for globals.css
- That ThemeProvider needs no attribute prop (data-theme is the default)
- Why this sidesteps the hydration mismatch (attribute is CSS-driven,
  not React-driven)
@luisangelrod luisangelrod changed the title docs: add Tailwind v4 data-theme approach that avoids hydration mismatch docs: add Tailwind v4 data-theme styling guidance Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Readme] Clear up hydration issue explanation

1 participant