1.0.4 • Published 5 years ago

use-prefer-dark-mode v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

use-prefer-dark-mode

Small react hook to determine if your user prefers dark mode on their system.

A lot of packages that deal with determining if you should use dark mode come with unneeded components, Contexts Providers & Consumers, configs objects you have to pass in, etc.

This one is simple. It only tells you if your user prefers dark mode based on their os settings. We let you determine where and how you deal with that preference. 🤙🏽

install

npm install --save use-prefer-dark-mode
# or
yarn add use-prefer-dark-mode

TypeScript Compatible

use-prefer-dark-mode is written in TypeScript so no need to install any addition types 👍🏽

usage

import React from "react"
import usePDM from "use-prefer-dark-mode"

const HelloWorld = () => {
  const prefersDarkMode = usePDM() // Boolean of if the user prefers dark mode according to their OS system
  
  const darkStyles = {
    backgroundColor: "black",
    color: "white"
  }
  
  const lightStyles = {
    backgroundColor: "white",
    color: "black"
  }
  
  const styles = prefersDarkMode ? darkStyles : lightStyles
  
  return <div style={styles} >Hello, World!</div>
}

Testing

Jest Mock

use-prefer-dark-mode uses window.mediaMatch to see if the user prefers dark mode or not. If you use the Jest testing library you might need to mock this function.

Help write tests

If you are interested in helping out with tests for this package, simply open a PR and let's work together!

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago