# dark-mode-react-hook

> React custom hook to apply dark mode to any website

Latest version **1.1.0** (published 2020-10-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install dark-mode-react-hook
pnpm add dark-mode-react-hook
yarn add dark-mode-react-hook
bun add dark-mode-react-hook
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-10-15 |
| First published | 2020-09-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Francesc Pellicero |
| Maintainers | fpellicero |
| Keywords | react, ui, hooks, dark-mode |

## Links

- npm: https://www.npmjs.com/package/dark-mode-react-hook
- Repository: https://github.com/fpellicero/dark-mode-react-hook
- Homepage: https://github.com/fpellicero/dark-mode-react-hook#readme
- Issues: https://github.com/fpellicero/dark-mode-react-hook/issues
- npm.io page: https://npm.io/package/dark-mode-react-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-10-15
- 1.0.0 — 2020-09-29

## README

# 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](https://dev.to/dip15739/dark-mode-with-only-1-css-property-17fl?utm_source=digest_mailer&utm_medium=email&utm_campaign=digest_email) and extracted it into a single hook.

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

```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

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

## Usage


```jsx
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:


| Name | Type | Description | Default |
| --- | --- | --- | --- |
| autoDetect | `boolean` | Use a `prefers-color-scheme` media query to detect user preferences and automatically set dark mode | `true` |
| defaultValue | `boolean` | Apply dark mode by default | `false` |

---
_Source: https://npm.io/package/dark-mode-react-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
