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-doc-theme

v0.3.1

Published

React hook for switching theme between light, dark and system. Compatible with TailwindCSS.

Downloads

17

Readme


demo pure css

Features

This library for Reactjs includes two hooks:

  1. useDocTheme
    • Supports dark, light and system theme with body class of dark.
    • Supports Tailwind CSS.
    • By default apply system theme.
  2. useLocalStorage
    • Supports saving and loading data from browser's local storage.

Installation

npm install use-doc-theme --save

@latest version supports React 18.3.1. Use @0.2.0 for earlier React versions.

Usage

See the demo, built with Reactjs and pure CSS: https://codepen.io/gauravjot/full/yLQexGR

1. useDocTheme

This is the simplest implementation of the hook. Intializing the hook will apply the theme and will give you access to its methods.

/* import */
import { useDocTheme } from "use-doc-theme";

function App() {
  /* initialize */
  const theme = useDocTheme();

  return (
    <>
      {/* usage */}
      <button onClick={theme.toggle}>Toggle</button>
    </>
  );
}

If you only want to apply the theme, you can skip assigning the hook to a variable.

import { useDocTheme } from "use-doc-theme";

function App() {
  // applies the theme
  useDocTheme();

  return (...);
}

Default Behavior

Using the hook will apply the system theme or the theme saved in local storage. This behavior can be changed by passing false into hook initialization that will skip applying the theme.

// Does not automatically apply theme
const theme = useDocTheme(false);

Available Options

These are all the available methods and options.

const theme = useDocTheme();

// Switch to Light Mode
theme.light();

// Switch to Dark Mode
theme.dark();

// Apply System Theme
theme.system();

// Toggle between dark and light
theme.toggle();

/*
 * Check active theme
 */
if (theme.isDarkMode) {
 // Dark Theme is active
}
if (theme.isLightMode) {
 // Light Theme is active
}
if (theme.isSystemMode) {
 // System Theme is active
}

Tailwind CSS Support

Add this to your tailwind.config.js file.

module.exports = {
  darkMode: 'class',
  // ...
}

Learn more here: Dark Mode - Tailwind CSS.

Regular CSS

Use .dark class. For example:

.hello {
  background: white;
  color: black;
}

.dark .hello {
  /* dark theme */
  background: black;
  color: white;
}

For body tag, use

body.dark {
  /* dark theme */
  background: black;
  color: white;
}

2. useLocalStorage

The useLocalStorage hook takes two string parameters. The first parameter is the name of key in local storage and the second is the default value in case local storage does not already have value for the key.

It behaves identical to useState with plus side that the state is saved in local storage.

import { useLocalStorage } from "use-doc-theme";

function App() {
  const [book, setBook] = useLocalStorage<string>("book", "The Alchemist by Paulo Coelho");

  return (
    <>
      <button
        onClick={() => {
          setBook("Happy Place by Emily Henry");
        }}
      >
        Switch Book
      </button>

      <h1>Current book</h1>
      <p>{book}</p>
    </>
  );
}

Data saved into local storage gets JSON stringified so you may also save objects.

Contribution

Community contributions are welcomed.