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

@leandroluk/next-dark-mode

v4.0.2

Published

@leandroluk/next-dark-mode (forked from xeoneux/xeoneux/next-dark-mode

Downloads

5

Readme

next-dark-mode

🌓 Theme your Next.js apps with a Dark Mode

license npm bundle size npm version

Contents:

Features

Auto mode

@leandroluk/next-dark-mode optionally supports auto mode which automatically switches the user's theme as per the color mode selected on their operating system.

Windows and macOS both support setting the dark or light mode based on the time of the day.

It is achieved via prefers-color-scheme media query.

No page load glitch

@leandroluk/next-dark-mode uses configurable cookies to persist the state of the current theme, one for the auto mode and the other for the dark mode.

This prevents the common page load glitch with the local storage approach where the app loads on the client and then the state of the user's theme is fetched.

You can see it in this implementation by Pantaley Stoyanov.

NOTE: This library is not compatible with Next.js 9's Auto Partial Static Export feature as it has to read the cookies in getInitialProps function, which makes all pages incompatible with Automatic Partial Static Export feature.

Requirements

To use @leandroluk/next-dark-mode, you must use [email protected] or greater which includes Hooks.

Installation

$ yarn add @leandroluk/next-dark-mode

or

$ npm install @leandroluk/next-dark-mode

Usage

  1. Wrap your _app.js component (located in /pages) with the HOC withDarkMode

    // _app.js
    import App from 'next/app'
    import withDarkMode from '@leandroluk/next-dark-mode'
    
    export default withDarkMode(App)
  2. You can now use the useDarkMode hook anywhere in your app

    import { useDarkMode } from '@leandroluk/next-dark-mode'
    
    const MyComponent = props => {
      const {
        autoModeActive,    // boolean - whether the auto mode is active or not
        autoModeSupported, // boolean - whether the auto mode is supported on this browser
        darkModeActive,    // boolean - whether the dark mode is active or not
        switchToAutoMode,  // function - toggles the auto mode on
        switchToDarkMode,  // function - toggles the dark mode on
        switchToLightMode, // function - toggles the light mode on
        toggleDarkMode,    // function - toggles the current mode
      } = useDarkMode()
    
     ...
    }

With CSS-in-JS libraries (like emotion or styled-components)

Wrap your _app.js component (located in /pages) with the HOC withDarkMode and pass the values to the ThemeProvider so that you can use it in your components

// _app.js
import { ThemeProvider } from '@emotion/react' // or styled-components
import withDarkMode from '@leandroluk/next-dark-mode'

function MyApp({ Component, darkMode, pageProps }) {
  const { autoModeActive, autoModeSupported, darkModeActive } = darkMode

  return (
    <ThemeProvider theme={{ darkMode: darkModeActive, ...(other values) }}>
      <Component {...pageProps} />
    </ThemeProvider>
  )
}

export default withDarkMode(MyApp)

Configuration

The withDarkMode function accepts a config object as its second argument. Every key is optional with default values mentioned:

  • disableAutoMode: boolean - Disable system theme detection. Defaults to false.
  • autoModeCookieName: string - Name of the cookie used to determine whether the auto preset is enabled. Defaults to 'autoMode'.
  • cookieOptions: object - Configuration options for the cookies that gets set on the client. Defaults to { sameSite: 'lax' }.
  • darkModeCookieName: string - Name of the cookie used to determine whether the dark preset is enabled. Defaults to 'darkMode'.
  • defaultMode: string - Determines the default color mode when there's no cookie set on the client. This usually happens on the first ever page load. It can either be 'dark' or 'light' and it defaults to 'light'.

Resources