Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hook-dark-mode

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hook-dark-mode

React custom hook to handle dark mode based on user system appearence theme and localStorage

  • 0.1.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

React useDarkMode Custom Hook

React custom hook to handle dark mode based on user system appearence theme and localStorage.

Benefits

  • Gets the initial value from the user system about color scheme preference by using media query prefers-color-scheme
  • Saves the preffered color scheme to localStorage so on consecutive visits the value will be fetched from there. Useful for cased when the system color scheme is light, but the user prefer to use the dark scheme in your app or vice versa.
  • Simple to use, just install and import the hook in your app import { useDarkMode } from "hook-dark-mode"
  • No extra dependencies

Installation

The easiest way to use useDarkMode hook is to install it from npm or yarn.

npm install hook-dark-mode --save

Or

yarn add hook-dark-mode

Usage

Pull the hook into your component (usually the root one) and call the hook inside the functional component. Then based on the user preference you can add corresponding classes to the wrapper element.

import { useDarkMode } from "hook-dark-mode";

function App() {
  const [prefersDarkMode, setDarkMode] = useDarkMode();

  return (
    <div className={"App " + (prefersDarkMode ? "dark" : "light")}>
        <p>Current color mode: {prefersDarkMode ? "Dark" : "Light"}</p>

        <button type="button" onClick={() => setDarkMode(!prefersDarkMode)}>
          Set {prefersDarkMode ? "light" : "dark"} mode
        </button>
    </div>
  );
}

In your css styles you would need the classes for dark and light mode according to your app design. For example,

.dark {
  background-color: #282c34;
  color: white;
}

.light {
  background-color: white;
  color: #282c34;
}

FAQs

Package last updated on 06 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc