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

nextjs-theme-mode

v1.0.0

Published

A helper for creating non-flickering and accessible themed applications

Downloads

2

Readme

Features

  • [x] 🙉 Non-flickering
  • [x] ♿ Accessible (supports prefers-color-scheme)
  • [x] 🐱 Dynamic theme values
  • [x] 🐄 No additional dependencies
  • [x] 🧠 Agnostic to the way you style your app

Installation

$ npm i --save nextjs-color-mode

# or

$ yarn add nextjs-color-mode

Setup

First, you need to import ColorModeScript from nextjs-color-mode and place it somewhere in the _app.js file.

If you're using styled-components or emotion, you can put the contents of criticalThemeCss to GlobalStyles. Just make sure it's critical css, and at the top of your global styles.

// _app.js

import Head from 'next/head'
import { ColorModeScript } from 'nextjs-color-mode'

const criticalThemeCss = `
.next-light-theme {
--background: #fff;
--text: #000;
}

.next-dark-theme {
--background: #000;
--text: #fff;
}

body {
  background: var(--background);
  color: var(--text);
}
`

function MyApp({ Component, pageProps }) {
  return (
    <>
      <Head>
        <style dangerouslySetInnerHTML={{ __html: criticalThemeCss }} />
      </Head>
      <ColorModeScript />
      <Component {...pageProps} />
    </>
  )
}

Theme switcher (useColorSwitcher)

To implement theme switcher, you should use the useColorSwitcher hook

Note that every component that explicitly uses this hook should be rendered only on the client-side. Check out how we do this in the example

import { ColorModeStyles, useColorModeValue, useColorSwitcher } from 'nextjs-color-mode'

export default function ColorSwitcher(props) {
  const { toggleTheme, colorMode } = useColorSwitcher()

  return (
    <button onClick={toggleTheme}>
      Change theme to {colorMode === 'light' ? 'dark' : 'light'}
    </button>
  )
}
function useColorSwitcher(): {
    colorMode: string;
    changeTheme: (colorMode: 'light' | 'dark') => void;
    toggleTheme: () => void;
};

Using dynamic variables (useColorModeValue)

Sometimes you may want to omit the design system or need to hotfix something fast. Here's the solution for that.

export default function SomeComponent() {
  const [boxBgColor, boxBgCss] = useColorModeValue('box-color', 'blue', 'red')
  const [boxBorderColor, boxBorderCss] = useColorModeValue('box-border-color', 'red', 'blue')
  // the first item of the array returns CSS variable name
  // and the second one returns a special object that then gets parsed into a themable CSS variable

  return (
    <>
      <ColorModeStyles styles={[boxBgCss, boxBorderCss]} />
      <div style={{ width: '24rem', height: '12rem', backgroundColor: boxBgColor, border: "10px solid", borderColor: boxBorderColor }} />
    </>
  )
}
function useColorModeValue(name: string, lightThemeValue: string, darkThemeValue: string);

Do not use the same name twice, it may cause variable overriding and is hard to debug. Also using things like unique id, UUID or any randomly generated set of characters is a bad idea - it will display mismatch content warning and make it even harder to debug!