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-dark-mode-hook

v1.2.6

Published

useDarkMode Custom Hook for React

Downloads

35

Readme

use-dark-mode-hook

Small!

A simple library to add dark mode functionality to your React projects!

Demo

This library provides a custom hook useDarkMode and a button toggler component DarkModeToggler. You can use them together or use a different button toggler component. However, this library does not provide any styling regarding dark and light mode. You have to do that yourself.

By default, useDarkMode will apply either dark or light class to the body of the document based on the choice of the user.

Contents

Installation

in your terminal:

npm i use-dark-mode-hook

Usage

useDarkMode + DarkModeToggler

To use both the functionality and UI (more details about the options of each in below sections):

import useDarkMode, { DarkModeToggler } from 'use-dark-mode-hook'

function MyComponent () {
    const [isDarkMode, toggleDarkMode] = useDarkMode()

    return (
        <DarkModeToggler 
            isDarkMode={isDarkMode} 
            toggleDarkMode={toggleDarkMode}
            buttonClassName="some-classes"
        />
    )
}

useDarkMode hook

import useDarkMode from 'use-dark-mode-hook'

function myComponent (props) {
    const [isDarkMode, toggleDarkMode] = useDarkMode()

    //do something with it
}

isDarkMode: boolean state for whether dark mode is chosen or not toggleDarkMode: function that takes boolean value for whether dark mode should be enabled or not. Use this to change the user's preference (dark or light mode).

Options

You can pass to useDarkMode the following options:

| Name | Type | Description | Default | | -------------- | ------- | --------------------------------------------------------------- | ------- | | initialValue | boolean | should it initially be dark | false | | darkModeClass | string | the class that should be given when dark mode | dark | | lightModeClass | string | the class that should be given when light mode | light | | element | string | the selector of the element that the class should be applied to | body | | debug | boolean | should debug messages be shown in the console | false |

useDarkMode({
    initialValue: false,
    darkModeClass: 'dark',
    lightModeClass: 'light',
    element: 'body'
})

DarkModeToggler

import { DarkModeToggler } from 'use-dark-mode-hook'

function MyComponent () {
    //some code

    function toggleDarkMode (checked) {
        //logic to toggle dark mode
    }

    return (
        <DarkModeToggler isDarkMode={value} toggleDarkMode={toggleDarkMode} />
    )
}

If you use the button only for toggling dark mode, you need to pass it the following parameters:

Options

| Name | Type | Description | | --------------- | ---------------- | -------------------------------------------------------------- | | isDarkMode | boolean | Whether it's currently dark or light | | toggleDarkMode | Function | The function that will handle the change of login between dark | | | | and light mode. Takes boolean as parameter for whether dark is | | | | enabled or not. | | buttonClassName | string(optional) | custom class to assign to button |


Attribution

Icons from Feather.


License

MIT