New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dark-mode-context

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dark-mode-context

A simple context provider for dark mode

  • 1.0.0-alpha.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

dark-mode-context

A simple context provider for dark mode. It can be used in combination with Tailwind CSS to apply dark mode class with support for respecting the user's system preferences.

It also includes a few components that Utilise Headless UI to make a accesable menu to control the dark mode. Note: this not tested yet.

A Demo can be found here, and the source code can be found here.

NPM

Install

npm install --save dark-mode-context
yarn add dark-mode-context

Usage

import React, { Component } from 'react'

import { DarkModeProvider,DarkModeMenu } from 'dark-mode-context'

const App = () => (
  <DarkModeProvider injectDarkClass>
    <div>
      <h1>Hello World</h1>
      <DarkModeMenu />
    </div>
  </DarkModeProvider>
)
import React, { Component } from 'react'
import { useDarkModeContext, DarkModeProvider } from 'dark-mode-context'

const MyButton = () => {
  const {isDark, toggleDarkMode} = useDarkModeContext()

  return (
    <button onClick={toggleDarkMode}>
      {isDark ? 'Dark Mode' : 'Light Mode'}
    </button>
  )
}

const App = () => (
  <DarkModeProvider injectDarkClass>
    <div>
      <h1>Hello World</h1>
      <MyButton />
    </div>
  </DarkModeProvider>
)

Props

DarkModeProvider

  • injectDarkClass: boolean - Whether the DarkModeProvider should start in dark mode or not. Defaults to false
  • wrapAs: React.ElementType | undefined - The element to wrap the children in. Defaults to undefined
  • darkClassName: string - The class name to be used for dark mode. Defaults to dark
  • lightClassName: string - The class name to be used for light mode. Defaults to ''
  • children: React.ReactNode - The children of the DarkModeProvider

Note: when wrapAs is not set, and injectDarkClass is set to true, the dark class will be added to the root element <html>.

DarkModeMenu

  • as: React.ElementType - The element to be used for the menu. Defaults to React.Fragment
  • className: string | undefined - The class name to be used for the menu. Defaults to undefined
  • darkBtn: React.ReactNode - The button to be used for dark mode. Defaults to DarkBtn
  • lightBtn: React.ReactNode - The button to be used for light mode. Defaults to LightBtn
  • systemBtn: React.ReactNode - The button to be used for system mode. Defaults to SystemBtn
  • menuToggleBtn: React.ReactNode - The button to be used for toggling the menu. Defaults to MenuToggleBtn

Note: This component requires HeadlessUI to be installed, which can be done by running npm install --save headlessui.

DarkBtn, LightBtn, SystemBtn, MenuToggleBtn

  • onClick: () => void - The function to be called when the button is clicked. Defaults to undefined
  • className: string | undefined - The class name to be used for the button. Defaults to undefined

Note: These components uses Tailwind CSS to style the button. they can be styled by passing a class name to the className prop.

License

MIT © NatoNathan

Keywords

FAQs

Package last updated on 07 Feb 2022

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc