Git Product home page Git Product logo

darkmode.js's Introduction

Darkmode.js

npm version npm downloads License Medium Badge Twitter: sandochee

πŸŒ“ Add a Dark Mode / Night Mode to your website in a few seconds

This library uses the CSS mix-blend-mode to bring Dark Mode to any of your websites. Just copy-paste the snippet and you will get a widget to turn on and off the Dark Mode. You can also use it without the widget programmatically. The plugin is lightweight, built-in Vanilla. It also uses local storage by default, so your last setting will be remembered!

I have been inspired by this article: https://www.aworkinprogress.dev/sun-moon-blending-mode

Features

  • Widget appears automatically
  • Saving users choice
  • Automatically shows Darkmode if the OS preferred theme is dark (if the browsers support prefers-color-scheme)
  • Can be used programmatically without widget

✨ Demo

Check out the demo in these websites:

Wordpress plugins

If you are using Wordpress you may want to have a look at these plugins based on Darkmode.js:

Drupal plugin

If you are using Drupal you may want to have a look at this plugin based on Darkmode.js:

Nuxt.js module

If you are using Nuxt.js there is a module for Darkmode.js:

πŸ“– How to use

Darkmode.js is very easy to use, just copy-paste the following code or use the npm package.

πŸš€ Easy way (using the JSDelivr CDN)

Just add this code to your HTML page:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
  function addDarkmodeWidget() {
    new Darkmode().showWidget();
  }
  window.addEventListener('load', addDarkmodeWidget);
</script>

πŸ“¦ Using NPM

npm install darkmode-js

Then add the following JavaScript code:

import Darkmode from 'darkmode-js';

new Darkmode().showWidget();

βš™οΈ Options

Here are the option availables:

const options = {
  bottom: '64px', // default: '32px'
  right: 'unset', // default: '32px'
  left: '32px', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#100f2c',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: false, // default: true,
  label: 'πŸŒ“', // default: ''
  autoMatchOsTheme: true // default: true
}

const darkmode = new Darkmode(options);
darkmode.showWidget();

▢️ Methods

If you don't want to show the widget and enable/disable Darkmode programatically you can use the method toggle(). You can also check if the Dark Mode is activated with the method isActivated(). See them in action in the following example:

const darkmode =  new Darkmode();
darkmode.toggle();
console.log(darkmode.isActivated()) // will return true

Override style

  • A CSS class darkmode--activated is added to the body tag when the darkmode is activated. You can take advantage of it to override the style and have a custom style
  • Use the class darkmode-ignore where you don't want to apply darkmode
  • You can also add this style: isolation: isolate; in your CSS, this will also ignore the darkmode.
  • It is also possible to revert the Dark Mode with this style mix-blend-mode: difference;

Examples

.darkmode--activated p, .darkmode--activated li {
  color: #000;
}

.button {
  isolation: isolate;
}

.darkmode--activated .logo {
  mix-blend-mode: difference;
}
<span class="darkmode-ignore">😬<span>

Debug

If it does not work you may have to add the following code, but this will invalidate the classes to override.

.darkmode-layer, .darkmode-toggle {
  z-index: 500;
}

Browser compatibility

This library uses the CSS mix-blend-mode: difference; to provide the Dark Mode. It may not be compatible with all the browsers. Therefore the widget has been hidden in Internet Explorer and Edge. This library also uses prefers-color-scheme: dark to automatically enable the Dark Mode if the OS prefered theme is dark.

Check the compatibility here:

Development

  • yarn build or npm run build - produces a production version of your library under the lib folder
  • yarn dev or npm run dev - produces development version of your library and runs a watcher
  • yarn test or npm run test - it runs the tests :)
  • yarn test:watch or npm run test:watch - same as above but in a watch mode

⭐️ Show your support

Please ⭐️ this repository if this project helped you!

patreon.png

🍺 Buy me a beer

If you like this project, feel free to donate:

  • PayPal: https://www.paypal.me/kanbanote
  • Bitcoin: 19JiNZ1LkMaz57tewqJaTg2hQWH4RgW4Yp
  • Ethereum: 0xded81fa4624e05339924355fe3504ba9587d5419
  • Monero: 43jqzMquW2q989UKSrB2YbeffhmJhbYb2Yxu289bv7pLRh4xVgMKj5yTd52iL6x1dvCYs9ERg5biHYxMjGkpSTs6S2jMyJn
  • Motive: MOTIV-25T5-SD65-V7LJ-BBWRD (Get Motive Now: https://motive.network)

πŸ“„ License

MIT License

Copyright (c) Sandoche Adittane

darkmode.js's People

Contributors

aleccool213 avatar arthurbaghdas avatar cyrilkrylatov avatar dependabot[bot] avatar maacpiash avatar naveenda avatar p44v9n avatar paloranta avatar rsahoward avatar sandoche avatar shreyasm-dev avatar tmbv93 avatar uzitech avatar wgao19 avatar yogeshnikam671 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

darkmode.js's Issues

How to add switchDarkmode to the menu?

I'm not a programmer, but I want to add switchDarkmode to the menu.I don't know how? My wordpress theme is TwentyTwenty. In addition, this DarkMode is not work on search bar. Thanks

Emojis change its color in dark mode

I have a GatsbyJS blog and I can't make the emojis remain their original color in the dark mode, I tried everything from the darkmodejs documentation but I still can't achieve it. Is there any way to add the darkmode-ignore css class to an emoji in the markdown text?

Thanks!

Releasing "releases"?

Hi @sandoche,

Author of the "Darkmode" WordPress plugin here. Any chance you could submit new releases with changelogs, so we can "watch" the repo and get notified with new releases? Right now I don't even know which version is darkmode.js on.

How To Exclude background-image css property

Everything is working fine except making the background image of my page negative. I have added a backround image using css background-image peoperty. And now its changing the color of the image. How can can is solve the problem?

Hope for solution soon.

Thanks from reply in advance.

Problems with WordPress.

1: Widget doesn't show up on some themes.
2: Widget is not clickable on some themes.
3: Webpage Title and Website title are being darked out after activating it.

image

--> Only worked for me on WordPress's Twenty Nineteen Theme

Tip: Add topics to the GitHub repo.

Work on Browser (Mobile/PC) But not working on Android WebView Application

I have implented the darkmode.Js it works great on PC or mobile browser. But somehow the JS is not working on Android Webview Application. It doesnot show toggles or even I set the dark mode pre activated it does not switch on webview application.

PS note: I tried to activate the Javascript on webview App. Still no luck.

Starting in Dark Mode

Is there a way to start it in dark mode via the options or to force dark mode to be the default on initial load?

[Question] How to make it ignore images?

Hello, I've installed this in a project at my job but I'm trying to make it ignore images when dark mode is on, but it's not working for me, not sure if I'm doing something wrong:

image

image

image

All the rest of the system works, just the images it's wrong.

Background turned dark, but it didn't work for the texts

Hi, thanks for your work!

I used Darkmode.js to add dark mode for my blog powered by VuePress. But in the dark mode, only the background turned black, while the text didn't change at all, like the screenshot below, which looked very weird. In the meanwhile, the button didn't appear the emoji πŸŒ“ I added.

example

And I used the script as your instruction shows:

  1. Using NPM
npm install darkmode-js
  1. Add the javascript code and options:
// Dark Mode
import Darkmode from 'darkmode-js';

new Darkmode().showWidget();

var options = {
    label: 'πŸŒ“'
}
  
const darkmode = new Darkmode(options);
darkmode.showWidget();

It would be great if you can help me about the problem. Thanks in advance 😊

Flicker when loading pages

Hi,

I am using this plugin in a next.js application and I am having issues.

When dark mode is selected, pages seem to load in light first then toggle to dark mode.

I added autoMatchOsTheme: false. But that didn't work. Same thing in chrome and firefox.

Here is the staging site.

Darkmode.js, possible issue with jQuery and Bootstrap 4

Hi, I tried adding ```

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
    <script>
    $(document).ready(function () {
      var options = {
      bottom: '64px', // default: '32px'
      right: 'unset', // default: '32px'
      left: '32px', // default: 'unset'
      time: '0.5s', // default: '0.3s'
      mixColor: '#fff', // default: '#fff'
      backgroundColor: '#fff',  // default: '#fff'
      buttonColorDark: '#100f2c',  // default: '#100f2c'
      buttonColorLight: '#fff', // default: '#fff'
      saveInCookies: false, // default: true,
      label: 'πŸŒ“', // default: ''
      autoMatchOsTheme: true // default: true
    }
    const darkmode = new Darkmode(options);
    darkmode.showWidget();
    });
    
    </script>
To my website and It seems that it is not working at all, I simply cannot click on the Dakrmode button. Is there anything that can help me fix this issue?

Saving Mode

When I disconnect my app and connect it again darkmode shows the last set mode. How can i disable it so that darkmode is inactive while app initiates?

**<input id="darktoggle" type="checkbox"/>** Β 

          const options = {autoMatchOsTheme: false}
          const darkmode =  new Darkmode(options);

          $('#darktoggle').click(function(){
              darkmode.toggle();
          });

           if (darkmode.isActivated()) {
              document.getElementById('darktoggle').checked = true;
            }
          else {
                document.getElementById('darktoggle').checked = false;
          }

Blogger Error

I'm trying to add this on my blogger blog, but i get the following Error. the Ocde i implemented is below.

Uncaught TypeError: Cannot read property 'insertBefore' of null
at new e (darkmode-js.min.js:1)
at (index):37

`<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>

<script> var options = { bottom: '64px', // default: '32px' right: 'unset', // default: '32px' left: '32px', // default: 'unset' time: '0.5s', // default: '0.3s' mixColor: '#fff', // default: '#fff' backgroundColor: '#fff', // default: '#fff' buttonColorDark: '#100f2c', // default: '#100f2c' buttonColorLight: '#fff', // default: '#fff' saveInCookies: false, // default: true, label: 'πŸŒ“' // default: '' } const darkmode = new Darkmode(options); darkmode.showWidget(); </script>

`

Also, Is there a way to use it someway show that it appears with a set of FontAwesome Icons but on clicking the icon the website returns dark theme. I would prefer that instead of a floating Button.

Problem with Next/Nuxt.js

Hey,
as mentioned via mail is here the issue.

While integrating in my next.js-App its working fine with the widget. But if i dont use the widget, it breaks the app with "window not defined".

Your answer:

I had the same problem with Nuxt.js (the equivalent of Next for Vue.js)
I guess it's typical because Next/Nuxt is trying to render it.

I need to investigate on it. I am a bit busy these days I may try in the next two weeks.
Please open an issue on github so I don't forget!

so, here it is :D

Thank you very much!

Darkmode body color?

Hi! How do I set the background body color I need?
default color = #000000
need = #222222

Tips on how to avoid the darkmode from flashing?

Problem

I have added this library to an existing static site that I am building. However, every time someone clicks on a new page there is a flashing effect from the white background, which is the original site color.

ezgif com-video-to-gif (2)

This website is a static website, so there is no server rendering which makes it a bit more difficult to avoid the flash.

Do you guys have any tips on how to avoid or limit as much possible the flashing on static sites?

Darkmode does not work in Admin LTE 3 template

I am working on a web platform and I integrated Admin LTE template (version 3).
I integrated darkmode.js using CDN .
Darkmode does not work while running the project on local server and there is no error on browser's console.

Error "Identifier 'darkmode' has already been declared" appears in the console

Hi @sandoche :
Thank you very much for Darkmode.js.βœ”
How exciting it is!I configured it on my personal website.
It adds a lot of fun to the page, and it is very useful at night.❀

But there is currently a problem that bothers me. 😦
The following is a description of the problem.

Code

My code is written like this:

var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: 'white', // default: '#fff'
  backgroundColor: '#dedede', // default: '#fff'
  buttonColorDark: '#212121', // default: '#100f2c'
  buttonColorLight: '#dedede', // default: '#fff'
  saveInCookies: true, // default: true,
  label: nightAndDaySvg, // default: ''
  autoMatchOsTheme: true // default: true
}

// Set the default value of darkmode
if (window.localStorage.getItem('darkmode') == null) {
  window.localStorage.setItem('darkmode', 'true');
}

const darkmode = new Darkmode(options).showWidget();

ReproduceπŸ‘»

This error can be reproduced at here.This is a static page.

Darkmode toggle is behind other HTML element

So I can't click the toggle because it's behind an HTML element.

I tried making a CSS file and putting this:

.darkmode-layer, .darkmode-toggle{
    z-index: 999;
}

But I have 0 experience with CSS so I'm not sure if that's correct. Any ideas?

Undo hue inversion

By rotating the hue with 180 degreess, the original hue of elements can be retained in dark mode :)

filter: hue-rotate(180deg);

css width issue

in responsive websites class "darkmode-ignore" sets a custom width...

to prevent this, in css you have to add a class:

.darkmode-ignore{
width: 100%;
}

it will work fine then...

Hide-Toggle on IE11

I think it'd be good to make this run at all on IE11 and stop the widget icon from displaying. Could maybe do this in JavaScript or use CSS targeting.

I use this to hide in IE10/IE11

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
.darkmode-toggle {display: none !important}
}

Could not auto set darkmode after 1.3.4

I have try v1.3.4 v1.4.0 v1.5.0 and v1.5.1
all of these version can set Local Storage darkmode: true
but only v1.3.4 can read it and use darkmode automatically
and I try to visit other site which use darkmode.js in your README Demo
all of them can not auto set darkmode
here is my code

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
  let options = {
    label: 'πŸŒ“'
  };
  const darkmode = new Darkmode(options);
  darkmode.showWidget();
</script>

Highlighting Color Changes

Is it possible to make the text highlight color not change color in dark mode? If not, is this a feature that could be considered.

Opacity

How would you change the code to do it with opacity blend in instead of the current effect?

Using snippet won't work.

For my own website, I decided to give this a shot.

I added the snippet that was given to me to my own page:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
  new Darkmode().showWidget();
</script>

But to my surprise, nothing happened.

I checked the console, and found this:

darkmode-js.min.js:1 Uncaught TypeError: Cannot read property 'classList' of null
    at new e (darkmode-js.min.js:1)

I haven't done anything weird.

I even created a new html document with nothing in it except a default template with some text, added the snippet, and nothing happened.

Any help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.