๐ŸŒ“ Add dark mode/night mode custom elements to your website.

Overview

dark-mode

CI jsDelivr CDN NPM Downloads npm version Open in unpkg

A custom element that allows you to easily put a Dark Mode ๐ŸŒ’ toggle. so you can initially adhere to your users' preferences according to prefers-color-scheme, but also allow them to (optionally permanently) override their system setting for just your site.

Installation

Install from npm:

npm install --save @wcj/dark-mode

Or, alternatively, use a <script defer> tag (served from unpkg's CDN):

CDN: UNPKG | jsDelivr | Githack | Statically

<script src="https://unpkg.com/@wcj/dark-mode"></script>

Usage

There are two ways how you can use <dark-mode>:

<dark-mode></dark-mode>
<dark-mode light="Dark" dark="Light"></dark-mode>
<dark-mode dark="Dark" light="Light" style="border: 1px solid red; font-size: 12px;"></dark-mode>

Use in React:

import React from 'react';
import '@wcj/dark-mode';

function Demo() {
  return (
    <div>
      <dark-mode permanent light="Light" dark="Dark"></dark-mode>
    </div>
  );
}

Toggle in JavaScript:

const toggle = document.querySelector('dark-mode');
const button = document.createElement('button');
button.textContent = 'Change Theme';
button.onclick = () => {
  const theme = document.documentElement.dataset.colorMode;
  // or => const theme = toggle.mode
  document.documentElement.setAttribute('data-color-mode', theme === 'light' ? 'dark' : 'light');
}
document.body.appendChild(button);
// Listen for toggle changes
// and toggle the `dark` class accordingly.
document.addEventListener('colorschemechange', (e) => {
  console.log(`Color scheme changed to "${e.detail.colorScheme}" or "${toggle.mode}".`);
  button.textContent = toggle.mode === 'dark' ? 'Change Theme ๐ŸŒž' : 'Change Theme ๐ŸŒ’';
});

Interacting with the custom element:

const darkMode = document.querySelector('dark-mode');

// Set the mode to dark
darkMode.mode = 'dark';
// Set the mode to light
darkMode.mode = 'light';
// Set the mode to dark
document.documentElement.setAttribute('data-color-mode', 'dark');
// Set the mode to light
document.documentElement.setAttribute('data-color-mode', 'light');

// Set the light label to "off"
darkMode.light = 'off';
// Set the dark label to "on"
darkMode.dark = 'on';

// Set a "remember the last selected mode" label
darkMode.permanent = true;

// Remember the user's last color scheme choice
darkModeToggle.setAttribute('permanent', false);
// Forget the user's last color scheme choice
darkModeToggle.removeAttribute('permanent');

Reacting on color scheme changes:

/* On the page */
document.addEventListener('colorschemechange', (e) => {
  console.log(`Color scheme changed to ${e.detail.colorScheme}.`);
});

Reacting on "remember the last selected mode" functionality changes:

/* On the page */
document.addEventListener('permanentcolorscheme', (e) => {
  console.log(`${e.detail.permanent ? 'R' : 'Not r'}emembering the last selected mode.`);
});

Properties

Properties can be set directly on the custom element at creation time, or dynamically via JavaScript.

export type ColorScheme = 'light' | 'dark';
export class DarkMode extends HTMLElement {
  mode?: ColorScheme;
  /**
   * Defaults to not remember the last choice.
   * If present remembers the last selected mode (`dark` or `light`),
   * which allows the user to permanently override their usual preferred color scheme.
   */
  permanent?: boolean;
  /**
   * Any string value that represents the label for the "dark" mode.
   */
  dark?: string;
  /**
   * Any string value that represents the label for the "light" mode.
   */
  light?: string;
  style?: React.CSSProperties;
}

Events

  • colorschemechange: Fired when the color scheme gets changed.
  • permanentcolorscheme: Fired when the color scheme should be permanently remembered or not.

Alternatives

  • dark-mode-toggle A custom element that allows you to easily put a Dark Mode ๐ŸŒ’ toggle or switch on your site
  • Darkmode.js Add a dark-mode / night-mode to your website in a few seconds
  • darken Dark mode made easy
  • use-dark-mode A custom React Hook to help you implement a "dark mode" component.
  • Dark Mode Switch Add a dark-mode theme toggle with a Bootstrap Custom Switch

Contributors

As always, thanks to our amazing contributors!

Made with github-action-contributors.

License

Licensed under the MIT License.

Comments
  • fix(readme): Typo update 'Dart' to 'Dark'

    fix(readme): Typo update 'Dart' to 'Dark'

    Seems that this should be Dark, rather than Dart. I noticed this seemingly typo by someone else using this package, then found that it is in the package documentation too.

    This readme file seems to be the only place that this typo exists dart-dark

    opened by JayMartMedia 1
  • Pin dependencies - autoclosed

    Pin dependencies - autoclosed

    WhiteSource Renovate

    This PR contains the following updates:

    | Package | Type | Update | Change | |---|---|---|---| | shx | devDependencies | pin | ^0.3.4 -> 0.3.4 | | terser (source) | devDependencies | pin | ^5.10.0 -> 5.12.1 |

    ๐Ÿ“Œ Important: Renovate will wait until you have merged this Pin PR before creating any upgrade PRs for the affected packages. Add the preset :preserveSemverRanges to your config if you instead don't wish to pin dependencies.


    Configuration

    ๐Ÿ“… Schedule: At any time (no schedule defined).

    ๐Ÿšฆ Automerge: Disabled by config. Please merge this manually once you are satisfied.

    โ™ป Rebasing: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

    ๐Ÿ”• Ignore: Close this PR and you won't be reminded about these updates again.


    • [ ] If you want to rebase/retry this PR, click this checkbox.

    This PR has been generated by WhiteSource Renovate. View repository job log here.

    opened by renovate[bot] 0
  • Configure Renovate

    Configure Renovate

    WhiteSource Renovate

    Welcome to Renovate! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin.

    ๐Ÿšฆ To activate Renovate, merge this Pull Request. To disable Renovate, simply close this Pull Request unmerged.


    Detected Package Files

    • package.json (npm)

    Configuration Summary

    Based on the default config's presets, Renovate will:

    • Start dependency updates only once this onboarding PR is merged
    • Enable Renovate Dependency Dashboard creation
    • If semantic commits detected, use semantic commit type fix for dependencies and chore for all others
    • Ignore node_modules, bower_components, vendor and various test/tests directories
    • Autodetect whether to pin dependencies or maintain ranges
    • Rate limit PR creation to a maximum of two per hour
    • Limit to maximum 10 open PRs at any time
    • Group known monorepo packages together
    • Use curated list of recommended non-monorepo package groupings
    • Fix some problems with very old Maven commons versions
    • Ignore spring cloud 1.x releases
    • Ignore web3j 5.0.0 release
    • Ignore http4s digest-based 1.x milestones
    • Use node versioning for @types/node
    • Limit concurrent requests to reduce load on Repology servers until we can fix this properly, see issue 10133
    • Do not upgrade from Alpine stable to edge

    ๐Ÿ”ก Would you like to change the way Renovate is upgrading your dependencies? Simply edit the renovate.json in this branch with your custom config and the list of Pull Requests in the "What to Expect" section below will be updated the next time Renovate runs.


    What to Expect

    With your current configuration, Renovate will create 1 Pull Request:

    Pin dependencies
    • Schedule: ["at any time"]
    • Branch name: renovate/pin-dependencies
    • Merge into: main
    • Pin shx to 0.3.4
    • Pin terser to 5.12.1

    โ“ Got questions? Check out Renovate's Docs, particularly the Getting Started section. If you need any further assistance then you can also request help here.


    This PR has been generated by WhiteSource Renovate. View repository job log here.

    opened by renovate[bot] 0
  • ๅœจreactไธญไฝฟ็”จ๏ผŒๆ˜Žๆš—ๆ•ˆๆžœไธๆ˜Žๆ˜พ

    ๅœจreactไธญไฝฟ็”จ๏ผŒๆ˜Žๆš—ๆ•ˆๆžœไธๆ˜Žๆ˜พ

    53421652086247_ pic_hd

    import '@wcj/dark-mode';
    function App() {
      return (
        <div style={{background: '#fff'}}>
            <dark-mode permanent light="Light" dark="Dark"></dark-mode>
        </div>
      )
    }
    
    opened by cuilanxin 2
  • Dependency Dashboard

    Dependency Dashboard

    This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

    Other Branches

    These updates are pending. To force PRs open, click the checkbox below.

    • [ ] chore(deps): update jaywcjlove/changelog-generator action to v1.5.7
    • [ ] chore(deps): update jaywcjlove/create-tag-action action to v1.3.12
    • [ ] chore(deps): update actions/checkout action to v3
    • [ ] chore(deps): update actions/setup-node action to v3

    Detected dependencies

    github-actions
    .github/workflows/ci.yml
    • actions/checkout v2
    • actions/setup-node v2
    • jaywcjlove/create-tag-action v1.3.6
    • jaywcjlove/changelog-generator v1.5.0
    • peaceiris/actions-gh-pages v3
    • jaywcjlove/changelog-generator v1.5.0
    • ncipollo/release-action v1
    • JS-DevTools/npm-publish v1
    npm
    package.json
    • shx ^0.3.4
    • terser ^5.10.0

    • [ ] Check this box to trigger a request for Renovate to run again on this repository
    opened by renovate[bot] 0
Releases(v1.0.15)
  • v1.0.15(Jul 25, 2022)

    Documentation v1.0.15: https://raw.githack.com/jaywcjlove/dark-mode/d1fe033/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.14...v1.0.15

    • ๐Ÿ“– doc: Update README.md d08960c @jaywcjlove
    • ๐ŸŽจ style: remove empty element. 5c32eb7 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.14(Apr 1, 2022)

    Documentation v1.0.14: https://raw.githack.com/jaywcjlove/dark-mode/6ad2bb4/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.13...v1.0.14

    • ๐Ÿ“– doc: Update README.md 22609cd @jaywcjlove
    • ๐Ÿž fix(doc): Typo update 'Dart' to 'Dark' (#5) 8cff559 @JayMartMedia
    • ๐Ÿž fix(doc): fixed capitalization of Dark (#6) f3b4f27 @JayMartMedia
    • ๐ŸŒ website: update example. 02d242a @jaywcjlove
    • ๐Ÿž fix: fix auto switch theme issue. c8ef883 @jaywcjlove
    • ๐ŸŒ website: update demo. b8d8561 @jaywcjlove
    • ๐ŸŒ website: update demo. 5053649 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.13(Mar 19, 2022)

    Documentation v1.0.13: https://raw.githack.com/jaywcjlove/dark-mode/e4d5808/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.12...v1.0.13

    • ๐Ÿ“– doc: Update README.md 5466914 @jaywcjlove
    • ๐ŸŒ website: update example. b763908 @jaywcjlove
    • ๐Ÿž fix: typo thme -> theme. f7baa0a @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.12(Mar 17, 2022)

    Documentation v1.0.12: https://raw.githack.com/jaywcjlove/dark-mode/cd61cc8/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.11...v1.0.12

    • ๐Ÿ†Ž type: Fix type issue in react. fcfdf51 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.11(Mar 17, 2022)

    Documentation v1.0.11: https://raw.githack.com/jaywcjlove/dark-mode/a558bcc/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.10...v1.0.11

    • ๐Ÿ’„ chore: Update website. 07324f8 @jaywcjlove
    • ๐Ÿ’„ chore: add .gitattributes file. 81f8e48 @jaywcjlove
    • ๐Ÿ’„ chore: Update website. 38a9a68 @jaywcjlove
    • ๐Ÿ’„ chore: Update website. caeefc2 @jaywcjlove
    • ๐Ÿ’„ chore: Update website. 9eb7930 @jaywcjlove
    • ๐Ÿ’„ chore: Update website. ca8cb7d @jaywcjlove
    • ๐Ÿ’„ chore: Update website. 84ede75 @jaywcjlove
    • ๐Ÿ’„ chore: update workflows config. b3e7226 @jaywcjlove
    • ๐Ÿ’„ chore: add license comment. 382d370 @jaywcjlove
    • ๐Ÿ’„ chore: add homepage field in package.json b0fffae @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.10(Mar 16, 2022)

    Documentation v1.0.10: https://raw.githack.com/jaywcjlove/dark-mode/c088e55/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.9...v1.0.10

    • ๐Ÿž fix: Fix colorschemechange properties issue. 9e7f088 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.9(Mar 16, 2022)

    Documentation v1.0.9: https://raw.githack.com/jaywcjlove/dark-mode/81ffe30/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.8...v1.0.9

    • ๐ŸŒ website: update example. 9edf7ab @jaywcjlove
    • ๐Ÿ†Ž type: Fix type issue in react. 945e73b @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.8(Mar 16, 2022)

    Documentation v1.0.8: https://raw.githack.com/jaywcjlove/dark-mode/da214b4/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.7...v1.0.8

    • ๐Ÿ’Š clean: clean console.log a33181a @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.7(Mar 16, 2022)

    Documentation v1.0.7: https://raw.githack.com/jaywcjlove/dark-mode/b5a2718/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.6...v1.0.7

    • ๐Ÿ“– doc: Update README.md 428da4a @jaywcjlove
    • ๐ŸŒ website: update example. e843a6f @jaywcjlove
    • ๐ŸŒ website: update example. ca4c8ad @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.6(Mar 16, 2022)

    Documentation v1.0.6: https://raw.githack.com/jaywcjlove/dark-mode/91c9ae9/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.5...v1.0.6

    • ๐Ÿ’„ chore: update example. 16ca2dd @jaywcjlove
    • ๐Ÿ’„ chore: update example. 5e27557 @jaywcjlove
    • ๐ŸŒŸ feat: add permanent properties & permanentcolorscheme event. d1ff9ba @jaywcjlove
    • ๐Ÿ†Ž type: Fix type issue. 8fa5fbf @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.5(Mar 16, 2022)

    Documentation v1.0.5: https://raw.githack.com/jaywcjlove/dark-mode/55fe1dc/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.4...v1.0.5

    • ๐Ÿž fix: Move data-color-mode to the <html> node. 2fbb3c9 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.4(Mar 16, 2022)

    Documentation v1.0.4: https://raw.githack.com/jaywcjlove/dark-mode/9a087ac/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.3...v1.0.4

    • ๐Ÿ†Ž type: Fix type error in React. bd51df8 @jaywcjlove
    • ๐ŸŽจ style: Modify color-thme value. 4728202 @jaywcjlove
    • ๐Ÿ“– doc: Udpate document. 9f07c58 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.3(Mar 15, 2022)

    Documentation v1.0.3: https://raw.githack.com/jaywcjlove/dark-mode/050a351/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.2...v1.0.3

    • ๐Ÿž fix: fix init mode value issue. 603608d @jaywcjlove
    • ๐Ÿž fix: Fix type file does not exist issue. 877cd16 @jaywcjlove
    • ๐Ÿ“– doc: Update README.md 2d9098f @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Mar 15, 2022)

    Documentation v1.0.2: https://raw.githack.com/jaywcjlove/dark-mode/27fc1c1/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.1...v1.0.2

    • ๐Ÿ“– doc: Update README.md 4020a16 @jaywcjlove
    • ๐Ÿ†Ž type: Add type file. 8f5585b @jaywcjlove
    • ๐Ÿž fix: Fix CustomElement failed to construct (#4). a7dcb02 @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Mar 14, 2022)

    Documentation v1.0.1: https://raw.githack.com/jaywcjlove/dark-mode/bc30473/index.html
    Comparing Changes: https://github.com/jaywcjlove/dark-mode/compare/v1.0.0...v1.0.1

    • ๐Ÿ’„ chore: update workflows config. ee9a3d8 @jaywcjlove
    • ๐Ÿ’„ chore: update workflows config. e5b5f00 @jaywcjlove
    • ๐Ÿ’„ chore: update workflows config. 107a239 @jaywcjlove
    • ๐Ÿ“– doc: Update README.md 24b30ea @jaywcjlove
    npm i @wcj/[email protected]
    
    Source code(tar.gz)
    Source code(zip)
Owner
ๅฐๅผŸ่ฐƒ่ฐƒโ„ข
(อกยทฬฎฬƒยทฬƒ) ๅฆ‚ไปŠๆ’ธ็ ๏ผŒๅƒๆ˜ฏๆ—ฉไธŠ่ตทๆฅ้œ€่ฆๅˆท็‰™ไธ€ๆ ท็š„ไน ๆƒฏใ€‚
ๅฐๅผŸ่ฐƒ่ฐƒโ„ข
DoMe is a ToDo App. you can add, delete and reorder elements of the todo list using drag and drop. You can also toggle between dark&light mode

DO ME Todo App Live Preview : DO ME Built With : - ReactJS - TailwindCSS Make sure you have: - Git - Nodejs version 14 or higher (we recommend using

Medjahdi Islem 5 Nov 18, 2022
Create a deep copy of a set of matched elements with the dynamic state of all form elements copied to the cloned elements.

jq-deepest-copy FUNCTION: Create a deep copy of a set of matched elements while preserving the dynamic state of any matched form elements. Example Use

Michael Coughlin 5 Oct 28, 2022
๐ŸŒ— 1 line of code to apply auto dark / light theme and support custom theme for your website. Super fast and lightweight theme library.

themes.js A super lightweight and fast Theme library with auto system color scheme detection in JavaScript. Features Auto detect Dark / Light mode by

SerKo 4 Nov 29, 2022
A minimalist web app for the daily morning and night athkar.

Morning & Night Athkar | ุฃุฐูƒุงุฑ ุงู„ุตุจุงุญ ูˆุงู„ู…ุณุงุก Local Development Recommended: Use nvm to manage npm dependencies and versions. This app uses node versi

Mohammed Amarnah 4 Dec 2, 2022
A minimalistic web application made using Next.js and Twitter API for Saturday Hack Night by TinkerHub

The Twittr. On the social media platform Twitter, a ratio, or getting ratioed, is when replies to a tweet vastly outnumber likes or retweets. This mea

Aswin Asok 5 Apr 17, 2022
The easiest quiz night you'll ever run.

Quizzler The easiest quiz night you'll ever run. About The Project Quizzler is a fun new way to practice, improve, and test your Javascript skills. Du

Wasim Reja 11 Dec 4, 2022
This experimental library patches the global custom elements registry to allow re-defining or reload a custom element.

Redefine Custom Elements This experimental library patches the global custom elements registry to allow re-defining a custom element. Based on the spe

Caridy Patiรฑo 21 Dec 11, 2022
A TailwindCSS variant for class-based dark mode with CSS Modules.

A TailwindCSS variant for class-based dark mode with Svelte's scoped stylesheets and CSS modules. If you've ever tried to use TailwindCSS dark mode wi

Bryan Lee 13 Dec 1, 2022
Perfect SvelteKit dark mode in 2 lines of code. Support System preference and any other theme with no flashing

This library is a port of next-theme for SvelteKit. All credit goes to pacocoursey and all next-themes contributors While usable, this library is stil

null 42 Sep 30, 2022
Force dark mode for all web pages.

Dark Mode [WIP] Force dark mode for all web pages. Instalation Enable Epiphany extension. Optional if not done. Download the latest release from the .

null 17 Dec 2, 2022
Toggle Dark-Mode manually on InStream.de

InStream.de Dark-Mode Umschalter Was es macht Dieses Add-On fรผr Firefox fรผgt einen Button hinzu, mit dem man websites zwischen "Normal" und "Dark-Mode

InStream.de 2 Feb 7, 2022
ZaDark is an open source extension that helps you enable Dark Mode for Zalo on PC and Browser.

ZaDark โ€“ Zalo Dark Mode Table of Contents About Install Roadmap Contributing License Contact Awards Acknowledgments About ZaDark is an open source ext

Nguyแป…n Chรกnh ฤแบกi 64 Dec 22, 2022
A drop in replacement for Hacker News with support for dark mode and more.

Worker News A drop in replacement for Hacker News with support for dark mode, quotes in comments, user identicons and submission favicons. What's cool

Worker Tools 18 Dec 31, 2022
chakra-radix-colors provides radix-ui color palettes, automatic dark mode, and acessible colors to chakra-ui applications

chakra-radix-colors chakra-radix-colors provides radix-ui color palettes, automatic dark mode, and acessible colors to chakra-ui applications. About C

null 11 Dec 30, 2022
Automatically switch Terminal profile based on dark / light mode

auto-terminal-profile Automatically switch the macOS Terminal profile based on the system-wide dark / light appearance mode Prerequisites Node.js 14-1

Patrik Csak 17 Jan 2, 2023
Stylesheet and scripts for implementing dark mode with Bootstrap 4

Bootstrap Darkmode This project provides a stylesheet and two scripts that allow you to implement a dark theme on your website. It is initially loaded

Adrian Kunz 41 Nov 27, 2022
Kuldeep 2 Jun 21, 2022
A table component for your Mantine data-rich applications, supporting asynchronous data loading, column sorting, custom cell data rendering, row context menus, dark theme, and more.

Mantine DataTable A "dark-theme aware" table component for your Mantine UI data-rich applications, featuring asynchronous data loading support, pagina

Ionut-Cristian Florescu 331 Jan 4, 2023
Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan with <3

Gutpress(WordPress Gutenburg Block Plugin) Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan w

Arif Khan 4 Nov 23, 2022