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

use-react-dark-mode

v2.4.0

Published

simple tailwind compatible persistent dark mode react hook

Downloads

17

Readme

Simple dark mode hook with tailwind support

This custom hook makes your life easier dealing with dark mode. It's built with Typescript and compatible with tailwind's class dark mode.

It's got two styles: 1. toggle 2. selector. If you choose toggle mode then user can select light and dark mode. If you choose selector mode then user has 3 options light, dark and system. By default toggle mode selected. You can change it when you first time using hook by giving 'selector' argument to hook's function.

Usage (toggle)

First in your global.css(index.css) file craete root variables for light mode. Then make dark class and change root variables to dark versions.

index.css

:root {
  --body-bg: white; /* light version */
}

.dark {
  --body-bg: black; /* dark version */
}

body {
  background: var(--body-bg);
}

App.jsx In toggle mode you can use two properties from hook: isDark and toggle. You can call toggle function to change theme mode. isDark will change respectively.

import useDarkMode from "use-react-dark-mode";

function App() {
  const { isDark, toggle } = useDarkMode(); // toggle by defualt

  return (
    <>
      <p>Hello I'm not visible in dark mode. Try toggling</p>
      <button onClick={toggle}>toggle</button>
    </>
  );
}

Usage (selector)

Selector style uses 3 options: light, dark, system. By defualt hook uses toggle mode. To change it to selector mode you need to pass 'selector' string to hook's function call as an argument.

Then you can use 3 properties from hook. They are isDark, mode, setMode. You can't use toggle function in this case. Instead, we have now setMode. setMode accepts 3 string 'light' | 'dark' | 'system'. You can set the them to one of them. Result would be in mode property. You can always know dark or light by watching isDark property.

App.jsx

import useDarkMode from "use-react-dark-mode";

function App() {
  const { isDark, mode, setMode } = useDarkMode("selector");
  return (
    <>
      <select value={mode} onChange={e => setMode(e.target.value)}>
        <option value="light">light</option>
        <option value="dark">dark</option>
        <option value="system">system</option>
      </select>
      <p>Hello I'm not visible in dark mode. try selecting different modes.</p>
    </>
  );
}

export default App;

Tailwind

You can use two styles with tailwind toggle and selector. You just need to configure tailwind.

tailwind.config.js

/** @type {import('tailwindcss').Config} */
export default {
  darkMode: "class", // set darkMode to class just it!
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
};

For now I don't recommend it for production. We are still testing this package.

My team: akhror_web, Shohjahon, Shohrux