npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-theme-mode

v1.0.13

Published

Quickly add dark-mode or custom theme modes to your react/nextjs application

Downloads

20

Readme

Description

This npm package is for nextjs projects. Simply to add dark-mode and light-mode functionalities with 2 lines of code.

Example

Example project with next-theme-mode

https://github.com/baristikir/next-theme-mode/tree/main/example

Requirements

To use next-theme-mode, you must use [email protected] or greater.

Installation

npm install next-theme-mode
# or
yarn add next-theme-mode

Usage

In order to use this package you need just to modify the _app.js and _document.js.

First of all you need to create a Theme.jsx/Theme.tsx file and put your theming into it. You can put your colors for the different theme modes into this file. Make sure to name the variables identically for light and dark theme to get full usage of the theme switching.

// theme/Theme.jsx
export const Theme = {
  light: {
    /**
     * Background Color
     */
    primaryBackground: '#FFFFFF',
    secondaryBackground: '#fafafa',
    /**
     * Text Colors
     */
    primaryText: '#192635',
  },
  dark: {
    /**
     * Background Color
     */
    primaryBackground: '#181818',
    secondaryBackground: '#0E141B',
    /**
     * Text Colors
     */
    primaryText: '#fbfbfc',
  },
}

You will need to customize the _app.js inside the pages directory.

// pages/_app.js

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

Add the ThemeModeProvider to your _app.js / _app.tsx

// pages/_app.js
import { ThemeModeProvider } from 'next-theme-mode'
import { Theme } from '../theme/Theme'

function MyApp({ Component, pageProps }) {
  return (
    <ThemeModeProvider customThemes={Theme}>
      <Component {...pageProps} />
    </ThemeModeProvider>
  )
}

export default MyApp

Also to prevent any flashing on reloads while using dark-mode add this to your _document.js / _document.tsx

import React from 'react'
import NextDocument, { Head, Html, Main, NextScript } from 'next/document'
import { ScriptHydrationTheme } from 'next-theme-mode'
import { Theme } from '../theme/Theme'

export default class Document extends NextDocument {
  render(): JSX.Element {
    return (
      <Html>
        <Head />
        <body>
          // Add this line to achieve non flashing
          <ScriptHydrationTheme themes={Theme} />
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

That's all it for the setup. Now you can use the useTheme() hook to change the theme modes and also use the colors in your css stylings.

useTheme

To know which theme is currently active you can use the useTheme() hook. The colorMode Object contains the current theme as a string. With the setColorMode function you can change the theme by passing a string into it.

const { colorMode, setColorMode } = useTheme()

const isDark = colorMode === 'dark' ? true : false

const changeTheme = () => {
  isDark ? setColorMode('light') : setColorMode('dark')
}
return <button onClick={() => changeTheme()}>Change Mode</button>

Css Variables

You can now use the theme variables in your css styles. The passed in theme variables have always the prefix --color- .

button {
  background-color: var(--color-primaryBackground);
  color: var(--color-primaryText);
}