Nextjs Darkmode 

Nextjs Darkmode is a versatile library crafted to fully utilize React 18 server components, ensuring a seamless dark mode experience in Next.js applications. Lightweight and efficient, it respects both user preferences and system settings through the prefers-color-scheme media query, and integrates effortlessly with React/Vite, Remix, and Next.js.
Motivation
The nextjs-themes library was initially created to achieve a similar functionality to next-themes with React Server Components. While effective, it felt bulky for those supporting only dark/light mode. Thus, nextjs-darkmode was developed to offer a minimal footprint while utilizing Next.js Server Components, avoiding any flash of unthemed content, and ensuring theme synchronization with the server.
Features
-
✅ Simple API to toggle between dark and light modes
-
✅ Perfect dark mode with just 2 lines of code
-
✅ Compatible with Tailwind CSS, StyledComponents, emotion, Material UI, ...
-
✅ Secure by design - we support nonce when you want to apply Content Security Policy
-
✅ Fully treeshakable (e.g., import from nextjs-darkmode/hooks)
-
✅ Full TypeScript support
-
✅ Utilizes React 18 Server components
-
✅ Compatible with all React 18 build systems/tools/frameworks
-
✅ System setting with prefers-color-scheme
-
✅ Supports Next.js 13 & 14 appDir
-
✅ No flash on load (supports SSG, SSR, ISG, and Server Components)
-
✅ Sync theme across tabs and windows
-
✅ Apply custom transitions when changing themes
-
✅ Manipulate theme via the useMode hook
-
✅ No cookies when not using the corresponding ServerTarget
-
✅ Comprehensive documentation with Typedoc
Feel free to request new features, discuss, or report bugs.
Please consider starring this repository and sharing it with your friends.
Getting Started
Installation
$ pnpm add nextjs-darkmode
or
$ npm install nextjs-darkmode
or
$ yarn add nextjs-darkmode
Import Styles
Import styles globally or within layout component.
@import "nextjs-darkmode/css";
import "nextjs-darkmode/css";
Lite Version
For a lighter version, use nextjs-darkmode-lite:

$ pnpm add nextjs-darkmode-lite
or
$ npm install nextjs-darkmode-lite
or
$ yarn add nextjs-darkmode-lite
You need r18gs as a peer-dependency.
Usage
Please explore examples and packages/shared-ui for more working examples. (updates coming soon...)
SPA (e.g., Vite, CRA) and Next.js pages directory (No server components)
Modify _app to add dark mode support:
import { Core } from "nextjs-darkmode";
import { Switch } from "nextjs-darkmode/switch";
function MyApp({ Component, pageProps }) {
return (
<>
<Core />
<header>
<Switch />
</header>
<Component {...pageProps} />
</>
);
}
export default MyApp;
⚡🎉Boom! Just a couple of lines and your dark mode is ready, complete with a color switch for user preferences. Check out examples for advanced usage.
For vite or any other build tool, find a similar root component, e.g., <App /> in CRA and vite.
With Next.js app router (Server Components)
Update app/layout.jsx to add Core and ServerTarget to avoid flash of un-themed content:
import { Core } from "nextjs-darkmode";
import { ServerTarget } from "nextjs-darkmode/server";
export default function Layout({ children }) {
return (
<html lang="en">
<head />
<body>
<ServerTarget />
<Core />
{children}
</body>
</html>
);
}
If you prefer SSR over SSG for your entire app, you can wrap entire app with ServerTarget as follows:
import { Core } from "nextjs-darkmode";
import { ServerTarget } from "nextjs-darkmode/server";
export default function Layout({ children }) {
return (
<ServerTarget tag="html" lang="en">
<head />
<body>
<Core />
{children}
</body>
</ServerTarget>
);
}
Switch
An elegant color switch to toggle color schemes:
<Switch />
HTML & CSS
Fully support dark mode, including system preference with prefers-color-scheme. The dark/light mode is synced between tabs and modifies the className and data-attributes on the html or ServerTarget element:
:root {
--background: white;
--foreground: black;
}
.dark {
--background: black;
--foreground: white;
}
[data-rm="dark"] {...}
When using ServerTarget, use the CSS general sibling combinator (~):
.selector,
.selector *,
.selector ~ *,
.selector ~ * * {
--th-variable: value;
}
Using the data-attributes
data-rm -> Resolved Mode
data-m -> User's preference
data-sm -> System preference
Content Security Policy
If you are using CSP rules for CSS files, you can pass nonce argument to the Core component. If nonce is not supplied transition styles will not be applied. This may allow patched transitions throught the page in some cases.
<Core nonce={yourNonce} t="transition: all .5s" />
Images
Show different images based on the current theme:
import Image from "next/image";
import { useMode } from "nextjs-darkmode/hooks";
function ThemedImage() {
const { resolvedMode } = useMode();
let src;
switch (resolvedMode) {
case "light":
src = "/light-mode-image.png";
break;
case "dark":
src = "/dark-mode-image.png";
break;
default:
src = "/default-image.png";
break;
}
return <Image src={src} alt="Themed Image" />;
}
useMode
The useMode hook provides mode information:
import { useMode } from "nextjs-darkmode";
const ThemeChanger = () => {
const { resolvedMode, setMode } = useMode();
return (
<div>
The current resolved mode is: {resolvedMode}
<button onClick={() => setMode("light")}>Light Mode</button>
<button onClick={() => setMode("dark")}>Dark Mode</button>
</div>
);
};
useMode hook returns the following object:
export interface UseModeInterface {
mode: ColorSchemePreference;
systemMode: ResolvedScheme;
resolvedMode: ResolvedScheme;
setMode: (mode: ColorSchemePreference) => void;
}
Force per page mode
Apply appropriate class names and data attributes to force a mode for the page:
export default function Page() {
return <div className="dark ndm-scoped data-rm='dark'">...</div>;
}
With Styled Components and any CSS-in-JS
Next Themes works with any library. For example, with Styled Components:
import { createGlobalStyle } from "styled-components";
import { Core } from "nextjs-darkmode";
const GlobalStyle = createGlobalStyle`
:root {
--fg: #000;
--bg: #fff;
}
[data-rm="dark"] {
--fg: #fff;
--bg: #000;
}
`;
function MyApp({ Component, pageProps }) {
return (
<>
<GlobalStyle />
<Core />
<Component {...pageProps} />
</>
);
}
With Tailwind
In tailwind.config.js, set the dark mode property to class:
module.exports = {
darkMode: "class",
};
Now you can use dark-mode specific classes:
<h1 className="text-black dark:text-white">
When you use Tailwind, make sure you replace html in app/layout to <ServerTarget tag="html"... to avoid FOUC.
Performance
nextjs-darkmode is designed to be fully tree-shakable, including only the code you use. For instance, if you only use the useMode hook, the rest of the library's code will be removed during the build process.
Contributing
We welcome contributions! Check out the Contributing Guide for more details.
🤩 Don't forget to star this repo!
Explore hands-on courses to get started with Turborepo:

License
MPL-2.0
Feel free to use, modify, and distribute this library under the MPL-2.0 license.
Please consider enrolling in our courses or sponsoring our work.
with 💖 by Mayank Kumar Chaudhari