All Projects → donavon → Use Dark Mode

donavon / Use Dark Mode

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Use Dark Mode

mantine
React components library with native dark theme support
Stars: ✭ 4,390 (+362.59%)
Mutual labels:  hooks, dark-theme
Adonis Ui
Lightweight UI toolkit for WPF applications offering classic but enhanced windows visuals
Stars: ✭ 878 (-7.48%)
Mutual labels:  dark-theme
Usestatewithlayoutanimation
Abstraction for `React Native`'s `LayoutAnimation` and `useState`
Stars: ✭ 19 (-98%)
Mutual labels:  hooks
Material Angular Dashboard
Material Angular Admin Template
Stars: ✭ 940 (-0.95%)
Mutual labels:  dark-theme
Pre Commit Terraform
pre-commit git hooks to take care of Terraform configurations
Stars: ✭ 902 (-4.95%)
Mutual labels:  hooks
Fisher
Simple yet powerful webhooks catcher
Stars: ✭ 11 (-98.84%)
Mutual labels:  hooks
Themekit
macOS theming library
Stars: ✭ 836 (-11.91%)
Mutual labels:  dark-theme
Use Instance
A custom React Hook that provides a sensible alternative to useRef for storing instance variables.
Stars: ✭ 28 (-97.05%)
Mutual labels:  hooks
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-98.52%)
Mutual labels:  hooks
Insomnia
🧛🏻‍♂️ Dark theme for Insomnia
Stars: ✭ 26 (-97.26%)
Mutual labels:  dark-theme
Genesis Simple Hook Guide
WordPress plugin that displays names of all Genesis hooks on the current page dynamically.
Stars: ✭ 25 (-97.37%)
Mutual labels:  hooks
New Moon
🌙 ‎ The optimized dark theme for web development.
Stars: ✭ 918 (-3.27%)
Mutual labels:  dark-theme
Axios Hooks
🦆 React hooks for axios
Stars: ✭ 862 (-9.17%)
Mutual labels:  hooks
New Moon Brackets
New Moon Theme for Brackets.
Stars: ✭ 19 (-98%)
Mutual labels:  dark-theme
Nightking
🌃 Google I/O 19 Extended, Dark Theme Demo
Stars: ✭ 20 (-97.89%)
Mutual labels:  dark-theme
Rooks
Essential hooks ⚓ to super charge your components!
Stars: ✭ 889 (-6.32%)
Mutual labels:  hooks
Awesome React Hooks
A curated list about React Hooks
Stars: ✭ 932 (-1.79%)
Mutual labels:  hooks
Vim
🧛🏻‍♂️ Dark theme for Vim
Stars: ✭ 857 (-9.69%)
Mutual labels:  dark-theme
Use Persisted State
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
Stars: ✭ 943 (-0.63%)
Mutual labels:  hooks
Mindnode
🧛🏻‍♂️ Dark theme for MindNode
Stars: ✭ 20 (-97.89%)
Mutual labels:  dark-theme

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].