Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Dec 2, 2023
1 parent 0ac74bb commit 4932e99
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 53 deletions.
159 changes: 109 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ npm install nextjs-themes
$ yarn add nextjs-themes
```

## Want Lite Version? [![npm bundle size](https://img.shields.io/bundlephobia/minzip/nextjs-themes-lite)](https://www.npmjs.com/package/nextjs-themes-lite)
## Want Lite Version? [![npm bundle size](https://img.shields.io/bundlephobia/minzip/nextjs-themes-lite)](https://www.npmjs.com/package/nextjs-themes-lite) [![Version](https://img.shields.io/npm/v/nextjs-themes-lite.svg?colorB=green)](https://www.npmjs.com/package/nextjs-themes-lite) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/dt/nextjs-themes-lite.svg)](https://www.npmjs.com/package/nextjs-themes-lite)

```bash
$ pnpm add nextjs-themes-lite
Expand All @@ -50,24 +50,24 @@ $ yarn add nextjs-themes-lite

> You need Zustand as a peer-dependency
## Use
## Usage

### With pages/
### SPA (e.g., Vite, CRA) and Next.js pages directory (No server components)

The best way is to add a [Custom `App`](https://nextjs.org/docs/advanced-features/custom-app) to use by modifying `_app` as follows:

Adding dark mode support takes 2 lines of code:

```js
import { ThemeSwitcher } from "next-themes";
import { ThemeSwitcher } from "nextjs-themes";

function MyApp({ Component, pageProps }) {
return (
<>
<ThemeSwitcher forcedTheme={Component.theme} />
<Component {...pageProps} />
</>
);
return (
<>
<ThemeSwitcher forcedTheme={Component.theme} />
<Component {...pageProps} />
</>
);
}

export default MyApp;
Expand All @@ -77,44 +77,83 @@ export default MyApp;

Check out examples for advanced usage.

### With app/
### With Next.js `app` router (Server Components)

Update your `app/layout.jsx` to add `ThemeSwitcher` and `SSCWrapper` from `nextjs-themes`. `SSCWrapper` is required to avoid flash of un-themed content on reload.
#### Prefer static generation over SSR - No wrapper component

```js
> If your app is mostly serving static content, you do not want the overhead of SSR. Use `NextJsSSGThemeSwitcher` in this case.
> When using this approach, you need to use CSS general sibling Combinator (~) to make sure your themed CSS is properly applied. See (HTML & CSS)[#html--css].
Update your `app/layout.jsx` to add `ThemeSwitcher` from `nextjs-themes`, and `NextJsSSGThemeSwitcher` from `nextjs-themes/server`. `NextJsSSGThemeSwitcher` is required to avoid flash of un-themed content on reload.

```tsx
// app/layout.jsx
import { ThemeSwitcher } from "nextjs-themes";
import { NextJsSSGThemeSwitcher } from "nextjs-themes/server/nextjs";

export default function Layout({ children }) {
return (
<html lang="en">
<head />
<body>
/** use NextJsSSGThemeSwitcher as first element inside body */
<NextJsSSGThemeSwitcher />
<ThemeSwitcher />
{children}
</body>
</html>
);
}
```

Woohoo! You just added multiple theme modes and you can also use Server Component! Isn't that awesome!

#### Prefer SSR over SSG - Use wrapper component

> If your app is serving dynamic content and you want to utilize SSR, continue using `ServerSideWrapper` component to replace `html` tag in `layout.tsx` file.
Update your `app/layout.jsx` to add `ThemeSwitcher` and `ServerSideWrapper` from `nextjs-themes`. `ServerSideWrapper` is required to avoid flash of un-themed content on reload.

```tsx
// app/layout.jsx
import { ThemeSwitcher } from "next-themes";
import { SSCWrapper } from "next-themes/server/nextjs";
import { ThemeSwitcher } from "nextjs-themes";
import { ServerSideWrapper } from "nextjs-themes/server/nextjs";

export default function Layout({ children }) {
return (
<SSCWrapper tag="html" lang="en">
<head />
<body>
<ThemeSwitcher />
{children}
</body>
</SSCWrapper>
);
return (
<ServerSideWrapper tag="html" lang="en">
<head />
<body>
<ThemeSwitcher />
{children}
</body>
</ServerSideWrapper>
);
}
```

Woohoo! You just added dark mode and you can also use Server Component! Isn't that awesome!

### HTML & CSS

That's it, your Next.js app fully supports dark mode, including System preference with `prefers-color-scheme`. The theme is also immediately synced between tabs. By default, next-themes modifies the `data-theme` attribute on the `html` element, which you can easily use to style your app:
That's it, your Next.js app fully supports dark mode, including System preference with `prefers-color-scheme`. The theme is also immediately synced between tabs. By default, nextjs-themes modifies the `data-theme` attribute on the `html` element, which you can easily use to style your app:

```css
:root {
/* Your default theme */
--background: white;
--foreground: black;
/* Your default theme */
--background: white;
--foreground: black;
}

[data-theme="dark"] {
--background: black;
--foreground: white;
--background: black;
--foreground: white;
}

// v2 onwards when using NextJsSSGThemeSwitcher, we need to use CSS Combinators
[data-theme="dark"] ~ * {
--background: black;
--foreground: white;
}
```

Expand All @@ -126,18 +165,18 @@ In case your components need to know the current theme and be able to change it.
import { useTheme } from "nextjs-themes";

const ThemeChanger = () => {
/* you can also improve performance by using selectors
* const [theme, setTheme] = useTheme(state => [state.theme, state.setTheme]);
*/
const { theme, setTheme } = useTheme();

return (
<div>
The current theme is: {theme}
<button onClick={() => setTheme("light")}>Light Mode</button>
<button onClick={() => setTheme("dark")}>Dark Mode</button>
</div>
);
/* you can also improve performance by using selectors
* const [theme, setTheme] = useTheme(state => [state.theme, state.setTheme]);
*/
const { theme, setTheme } = useTheme();

return (
<div>
The current theme is: {theme}
<button onClick={() => setTheme("light")}>Light Mode</button>
<button onClick={() => setTheme("dark")}>Dark Mode</button>
</div>
);
};
```

Expand All @@ -149,12 +188,12 @@ const ThemeChanger = () => {
import { ForceTheme } from "nextjs-themes";

function MyPage() {
return (
<>
<ForceTheme theme={"my-theme"} />
...
</>
);
return (
<>
<ForceTheme theme={"my-theme"} />
...
</>
);
}

export default MyPage;
Expand All @@ -166,7 +205,7 @@ For pages router, you have 2 options. One is the same as the app router and the

```javascript
function MyPage() {
return <>...</>;
return <>...</>;
}

MyPage.theme = "my-theme";
Expand All @@ -178,14 +217,34 @@ In a similar way, you can also force color scheme.

Forcing color scheme will apply your defaultDark or defaultLight theme, configurable via hooks.

## Migrating from v1 to v2

#### Motivation:

For server side syncing, we need to use cookies and headers. This means that this component and its children can not be static. They will be rendered server side for each request. Thus, we are avoiding the wrapper. Now, only the `NextJsSSGThemeSwitcher` will be rendered server side for each request and rest of your app can be server statically.

Take care of the following while migrating to `v2`.

- No changes required for projects not using `Next.js` app router or server components other than updating cookies policy if needed.
- The persistent storage is realized with `cookies` in place of `localStorage`. (You might want to update cookies policy accordingly.)
- We have provided `NextJsSSGThemeSwitcher` in addition to `ServerSideWrapper` for `Next.js`. You no longer need to use a wrapper component which broke static generation and forced SSR.
- Visit [With Next.js `app` router (Server Components)](#with-nextjs-app-router-server-components)

## Migrating from v0 to v1

- `defaultDarkTheme` is renamed to `darkTheme`
- `setDefaultDarkTheme` is renamed to `setDarkTheme`
- `defaultLightTheme` is renamed to `lightTheme`
- `setDefaultLightTheme` is renamed to `setLightTheme`

> Full docs coming soon!
## Docs

[Typedoc](https://react18-tools.github.io/nextjs-themes)

### 🤩 Don't forger to start this repo!

Want handson course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://www.udemy.com/course/react-and-next-js-with-typescript/?referralCode=7202184A1E57C3DCA8B2)


## License

Expand Down
7 changes: 4 additions & 3 deletions packages/nextjs-themes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"types": "./index.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/mayank1513/nextjs-themes.git"
"url": "git+https://github.com/react18-tools/nextjs-themes.git"
},
"bugs": {
"url": "https://github.com/mayank1513/nextjs-themes/issues"
"url": "https://github.com/react18-tools/nextjs-themes/issues"
},
"homepage": "https://github.com/mayank1513/nextjs-themes#readme",
"homepage": "https://github.com/react18-tools/nextjs-themes#readme",
"sideEffects": false,
"license": "MIT",
"scripts": {
Expand Down Expand Up @@ -71,6 +71,7 @@
"nextjs13-themes",
"react",
"react18",
"react18-tools",
"react-themes",
"react18-themes",
"react-server-components",
Expand Down

0 comments on commit 4932e99

Please sign in to comment.