• Stars
    star
    1,288
  • Rank 36,518 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A custom React Hook to help you implement a "dark mode" component.

use-dark-mode

A custom React Hook to help you implement a "dark mode" component for your application. The user setting persists to localStorage.

❀️ it? ⭐️ it on GitHub or Tweet about it.

npm version Build Status All Contributors Tweet

usedarkmode-small

useDarkMode works in one of two ways:

  1. By toggling a CSS class on whatever element you specify (defaults to document.body). You then setup your CSS to display different views based on the presence of the selector. For example, the following CSS is used in the demo app to ease the background color in/out of dark mode.

    body.light-mode {
      background-color: #fff;
      color: #333;
      transition: background-color 0.3s ease;
    }
    body.dark-mode {
      background-color: #1a1919;
      color: #999;
    }
  2. If you don't use global classes, you can specify an onChange handler and take care of the implementation of switching to dark mode yourself.

New in Version 2.x

  • useDarkMode now persists between sessions. It stores the user setting in localStorage.

  • It shares dark mode state with all other useDarkMode components on the page.

  • It shares dark mode state with all other tabs/browser windows.

  • The initial dark mode is queried from the system. Note: this requires a browser that supports the prefers-color-scheme: dark media query (currently Chrome, Firefox, Safari and Edge) and a system that supports dark mode, such as macOS Mojave.

  • Changing the system dark mode state will also change the state of useDarkMode (i.e, change to light mode in the system will change to light mode in your app).

  • Support for Server Side Rendering (SSR) in version 2.2 and above.

Requirement

To use use-dark-mode, you must use [email protected] or greater which includes Hooks.

Installation

$ npm i use-dark-mode

Usage

const darkMode = useDarkMode(initialState, darkModeConfig);

Parameters

You pass useDarkMode an initialState (a boolean specifying whether it should be in dark mode by default) and an optional darkModeConfig object. The configuration object may contain the following keys.

Key Description
classNameDark The class to apply. Default = dark-mode.
classNameLight The class to apply. Default = light-mode.
element The element to apply the class name. Default = document.body.
onChange A function that will be called when the dark mode value changes and it is safe to access the DOM (i.e. it is called from within a useEffect). If you specify onChange then classNameDark, classNameLight, and element are ignored (i.e. no classes are automatically placed on the DOM). You have full control!
storageKey A string that will be used by the storageProvider to persist the dark mode value. If you specify a value of null, nothing will be persisted. Default = darkMode.
storageProvider A storage provider. Default = localStorage. You will generally never need to change this value.

Return object

A darkMode object is returned with the following properties.

Key Description
value A boolean containing the current state of dark mode.
enable() A function that allows you to set dark mode to true.
disable() A function that allows you to set dark mode to false.
toggle() A function that allows you to toggle dark mode.

Note that because the methods don't require any parameters, you can call them direcly from an onClick handler from a button, for example (i.e., no lambda function is required).

Example

Here is a simple component that uses useDarkMode to provide a dark mode toggle control. If dark mode is selected, the CSS class dark-mode is applied to document.body and is removed when de-selected.

import React from 'react';
import useDarkMode from 'use-dark-mode';

import Toggle from './Toggle';

const DarkModeToggle = () => {
  const darkMode = useDarkMode(false);

  return (
    <div>
      <button type="button" onClick={darkMode.disable}>
        β˜€
      </button>
      <Toggle checked={darkMode.value} onChange={darkMode.toggle} />
      <button type="button" onClick={darkMode.enable}>
        ☾
      </button>
    </div>
  );
};

export default DarkModeToggle;

That flash!

If your CSS is setup to default to light mode, but the user selects dark mode, the next time they visit your app, they will be in dark mode. However, the user will see a flash of light mode before the app is spun up and useDarkMode is called.

To prevent this, I've included some vanilla JavaScript that you can insert in your index.html just after the <body> tag. It is in a file named noflash.js.txt. You can either insert the contents of this file in a <script> tag or automate the step in your build process.

Note that if you change any of the defaultβ€”such as storageKey or classNameDark for exampleβ€”the noflash.js file will need to be modified with the same values.

Gatsby

Gatsby users may leverage gatsby-plugin-use-dark-mode to inject noflash.js for you.

Next.js

For next.js uses copy the noflash.js.txt to your public folder (public/noflash.js) and then create a _document.js and include the script before <Main />.

import Document, { Html, Head, Main, NextScript } from 'next/document';

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head />
        <body>
          <script src="noflash.js" />
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

Sample Apps

Here is a list of apps build with use-dark-mode. If you have an app you would like to include on this list, open a PR.

License

MIT Licensed

Contributors

Thanks goes to these wonderful people (emoji key):


Donavon West

πŸš‡ ⚠️ πŸ’‘ πŸ€” 🚧 πŸ‘€ πŸ”§ πŸ’»

Revel Carlberg West

πŸ€”

Mateusz BurzyΕ„ski

πŸ’»

Justin Hall

πŸ’»

Jeremy

πŸ““ πŸ›

Janosh Riebesell

πŸ“–

Andrew Lisowski

πŸ“–

Jorge Gonzalez

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

hook-flow

A flowchart that explains the new lifecycle of a Hooks component. https://dwe.st/hf
Shell
2,157
star
2

use-persisted-state

A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
JavaScript
1,378
star
3

use-event-listener

A custom React Hook that provides a declarative useEventListener
JavaScript
356
star
4

thwack

A tiny modern data fetching solution
JavaScript
269
star
5

undefined-is-a-function

"undefined is not a function"? β€” It is now!
JavaScript
241
star
6

use-interval

A custom React Hook that provides a declarative setInterval called useInterval.
TypeScript
161
star
7

react-af

Allows you to code using certain React.next features today! Perfect for component library maintainers.
JavaScript
138
star
8

styled-shortcuts

Provides convenient props shortcut helper for Styled Components πŸ’…
JavaScript
88
star
9

superjson-remix

TypeScript
84
star
10

render-props

Easily and reliably support Render Props, Component Injection, and Function as a Child
JavaScript
83
star
11

use-firebase-auth

A custom React Hook that provides a declarative interface for Firebase Auth.
JavaScript
78
star
12

remix-etag

HTML
70
star
13

use-instance

A custom React Hook that provides a sensible alternative to useRef for storing instance variables.
TypeScript
38
star
14

use-visibility-change

Know how long it's been since a user has "seen" your app.
TypeScript
33
star
15

big-sur-icons

A collection of re-imagined icons for popular apps in the macOS Big Sir style
24
star
16

reclass

Write React stateful components without worrying about this or binding methods.
JavaScript
24
star
17

transformer-open-graph

TypeScript
23
star
18

splashr

A React component that takes the effort out of adding a Splash Screen to your web application.
JavaScript
21
star
19

react-router-relative-link

Allows react-router's Link component to accept relative paths.
JavaScript
19
star
20

use-firebase-app

JavaScript
17
star
21

json_

Converts camelCase JavaScript objects to JSON snake_case and vise versa.
TypeScript
17
star
22

use-prev-prop

A custom React Hook that maintains a previous value for a prop (or any other value).
JavaScript
16
star
23

lambdog-server

Write Netlify functions without pulling your hair out.
JavaScript
15
star
24

use-firebase-database

JavaScript
14
star
25

styled-units

You might want to try https://github.com/donavon/styled-shortcuts instead
JavaScript
14
star
26

styled-shortcut-components

A convenience package that wraps styled-components with styled-shortcuts.
JavaScript
13
star
27

init-readme

An opinionated README.md generator for npm utility packages
JavaScript
12
star
28

garage-door

JavaScript
12
star
29

hrd

An npx utility to make it easy to hoard, um… reserve an npm name.
Shell
10
star
30

use-pdf

TypeScript
9
star
31

intl-parse-accept-language

TypeScript
8
star
32

lloop

A React JSX Loop component with the lloopy name
JavaScript
6
star
33

xcop

An XHR Cross Origin Proxy
JavaScript
5
star
34

react-proxy-hook

Helps you test a React Hook
JavaScript
5
star
35

use-cached-state

JavaScript
5
star
36

binclock

A binary clock written in React using TypeScript
TypeScript
4
star
37

remix-patches

4
star
38

donavon

3
star
39

react-extended-render

Extends React's `Component` by passing `props`, `state`, and `context` to the `render` method of a class.
JavaScript
3
star
40

render-fragment

JavaScript
3
star
41

consoleit

Utility to append console messages to the DOM. Useful when using RequireBin, JSFiddle, or the like.
JavaScript
3
star
42

Star-Wars-Episode-I-The-Phantom-Menace

Script for "Star Wars: Episode I – The Phantom Menace" entirely using git commit messages. Like it? Give it a ⭐️ on GitHub.
3
star
43

use-step-multi-step-form-demo

Created with CodeSandbox
JavaScript
3
star
44

crapp-ts

An npx utility to run create-react-app with the TypeScript template.
Shell
3
star
45

animated-placeholder

JavaScript
2
star
46

lambdog-client

JavaScript
2
star
47

spreadlove.io

HTML
2
star
48

react-wobbly-spinner

A Wobbly Spinner Component for React
JavaScript
2
star
49

multi-cookie-session

TypeScript
2
star
50

chime-devices

This package contains a React context provider and a hook around AWS Chime for capturing system audio and video devices.
TypeScript
1
star
51

build-uri-path

Reliably build a Uri path
JavaScript
1
star
52

resume

1
star
53

prevent-default

A wrapper that calls `event.preventDefault()` for you.
JavaScript
1
star
54

render-array

JavaScript
1
star
55

netlify-faunadb-example

JavaScript
1
star
56

thwack-resolve

A tiny functional equivalent to` new URL(url, base).href`
TypeScript
1
star
57

babel-plugin-transform-class-private-properties

A Babel transform to convert pseudo-private "underscore convention" private class properties to truly private properties.
1
star