Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nextjs-themes

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-themes

This project is inspired by next-themes. Next-themes is an awesome package, however, it requires wrapping everything in a provider. The provider has to be a client component as it uses hooks. And thus, it takes away all the benefits of Server Components.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1K
decreased by-49.15%
Maintainers
1
Weekly downloads
 
Created
Source

Nextjs-Themes Version

This project is inspired by next-themes. Next-themes is an awesome package, however, it requires wrapping everything in a provider. The provider has to be a client component as it uses hooks. And thus, it takes away all the benefits of Server Components.

nextjs-themes removes this limitation and enables you to unleash the full power of React 18 Server Components. In addition, more features are coming up soon... Stay tuned!

  • ✅ Designed for excellence
  • ✅ Unleash the full power of React18 Server components
  • ✅ Works with all build systems/tools/frameworks for React18
  • ✅ Perfect dark mode in 2 lines of code
  • ✅ System setting with prefers-color-scheme
  • ✅ Themed browser UI with color-scheme
  • ✅ Support for Next.js 13 appDir
  • ✅ Sync theme across tabs and windows
  • ✅ Disable flashing when changing themes
  • ✅ Force pages to specific themes
  • ✅ Class or data attribute selector
  • ✅ Manipulate theme via useTheme hook

Check out the live example.

Install

$ pnpm add nextjs-themes
# or
$ npm install nextjs-themes
# or
$ yarn add nextjs-themes

Want Lite Version?

$ pnpm add nextjs-themes-lite
# or
$ npm install nextjs-themes-lite
# or
$ yarn add nextjs-themes-lite

You need Zustand as a peer-dependency

Use

With pages/

The best way is to add a Custom App to use by modifying _app as follows:

Adding dark mode support takes 2 lines of code:

import { ThemeSwitcher } from "next-themes";

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

export default MyApp;

⚡🎉Boom! Just a couple of lines and your dark mode is ready!

Check out examples for advanced usage.

With app/

Update your app/layout.jsx to add ThemeSwitcher from nextjs-themes.

// app/layout.jsx
import { ThemeSwitcher } from "next-themes";

export default function Layout({ children }) {
  return (
    <html>
      <head />
      <body>
        <ThemeSwitcher />
        {children}
      </body>
    </html>
  );
}

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:

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

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

useTheme

In case your components need to know the current theme and be able to change it. The useTheme hook provides theme information:

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>
  );
};

Force per page theme and color-scheme

Next.js app router

import { ForceTheme } from "nextjs-themes";

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

export default MyPage;

Next.js pages router

For pages router, you have 2 options. One is the same as the app router and the other option which is compatible with next-themes is to add theme to your page component as follows.

function MyPage() {
    return (<>...</>)
}

MyPage.theme = "my-theme";

export default MyPage;

In a similar way, you can also force color scheme.

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

Full docs coming soon!

License

Licensed as MIT open source.


with 💖 by Mayank Kumar Chaudhari

Keywords

FAQs

Package last updated on 06 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc