Socket
Book a DemoInstallSign in
Socket

dark-mode-react-hook

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dark-mode-react-hook

React custom hook to apply dark mode to any website

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

React Hook Dark Mode

This package contains a React custom hook that applies dark mode to your website. It uses css filter to transform light schemes into dark themes (and viceversa). I took the idea from this post and extracted it into a single hook.

It works by injecting a global stylesheet with the following css:

html {
  filter: invert(1) hue-rotate(180deg);
}

img {
  
  filter: invert(1) hue-rotate(180deg);
}

.no-dark-mode {
  filter: invert(1) hue-rotate(180deg);
}

What this css is doing is inverting the colors in the <body> and then converting all colors to their complementaries. The final result is a dark variant (or light, if the original is dark) of the theme, while more-or-less keeping the main colors.

If there is an element that you do not wish to invert (for example, a dark modal background should probably not be bright under dark-mode), you can apply the no-dark-mode class to it.

Installation

npm i --save react-hook-dark-mode
# or
yarn add react-hook-dark-mode

Usage

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

function darkModeToggle() {
  const [darkMode, setDarkMode] = useDarkMode();

  return (
    <button onClick={() => setDarkMode(!darkMode)}>
      {darkMode ? 🌙 : ☀️}
    </button>
  )
}

Options

The hook accepts a configuration object with the following options:

NameTypeDescriptionDefault
autoDetectbooleanUse a prefers-color-scheme media query to detect user preferences and automatically set dark modetrue
defaultValuebooleanApply dark mode by defaultfalse

Keywords

react

FAQs

Package last updated on 15 Oct 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.