Skip to content

Commit

Permalink
Update docs with tailwind example
Browse files Browse the repository at this point in the history
  • Loading branch information
pacocoursey committed Nov 18, 2020
1 parent e96c7b8 commit 676e277
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const ThemeChanger = () => {
}
```

> **Warning!** The above code is hydration _unsafe_ and will throw a hydration mismatch warning when rendering with SSG or SSR. This is because we cannot know the `theme` on the server, so it will always be `undefined` until mounted on the client.
>
> **Warning!** The above code is hydration _unsafe_ and will throw a hydration mismatch warning when rendering with SSG or SSR. This is because we cannot know the `theme` on the server, so it will always be `undefined` until mounted on the client.
>
> You should delay rendering any theme toggling UI until mounted on the client. See the [example](#avoid-hydration-mismatch).
## API
Expand Down Expand Up @@ -314,10 +314,10 @@ import { useTheme } from 'next-themes'
const ThemeChanger = () => {
const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()

// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), [])

if (!mounted) return null

return (
Expand All @@ -332,6 +332,36 @@ const ThemeChanger = () => {

To avoid Content Layout Shift, consider rendering a skeleton until mounted on the client side.

### With Tailwind

[Visit the live example](https://next-themes-tailwind.vercel.app) • [View the example source code](https://github.com/pacocoursey/next-themes/tree/master/examples/tailwind)

> NOTE! Tailwind only supports dark mode in version >2.
In your `tailwind.config.js`, set the dark mode property to class:

```js
// tailwind.config.js
module.exports = {
darkMode: 'class'
}
```

Set the attribute for your Theme Provider to class:

```js
// pages/_app.js
<ThemeProvider attribute="class">
```

If you're using the `value` prop to specify different attribute values, make sure your dark theme explicitly uses the "dark" value, as required by Tailwind.

That's it! Now you can use dark-mode specific classes:

```js
<h1 className="text-black dark:text-white">
```

## Discussion

### The Flash
Expand Down

0 comments on commit 676e277

Please sign in to comment.