Darky.js

Add a dark mode to your website in seconds. No hassle, no reworking your entire codebase!


Download Documentation

Documentation


How to use Darky

Include the Darky.js library in your HTML file and Instantiate a new Darky object to add a darkmode toggle to your webpage:

The easy way (JSDelivr CDN)

<script src="https://cdn.jsdelivr.net/npm/[email protected]/darky.min.js"></script>
<script>
  const darkmode = new Darky();
  darkmode.enable();
</script>

Or download the library and host it yourself.

Using NPM

npm install darkyjs

Options

Here are the available options

Example:

const options = {
  bottom: "32px",
  right: "32px",
  left: "unset",
  time: "300ms",
  backgroundColor: "#fff",
  buttonColorDark: "#000",
  buttonColorLight: "#fff",
  buttonShadow: "0 0.2rem 0.5rem 0.1rem rgba(0,0,0,0.4)",
  zIndex: 9998,
  label: {
    dark: '<svg xmlns="http://www.w3.org/2000/svg" class="icon light" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M14.828 14.828a4 4 0 1 0 -5.656 -5.656a4 4 0 0 0 5.656 5.656z"></path><path d="M6.343 17.657l-1.414 1.414"></path><path d="M6.343 6.343l-1.414 -1.414"></path><path d="M17.657 6.343l1.414 -1.414"></path><path d="M17.657 17.657l1.414 1.414"></path><path d="M4 12h-2"></path><path d="M12 4v-2"></path><path d="M20 12h2"></path><path d="M12 20v2"></path></svg>',
    light: '<svg xmlns="http://www.w3.org/2000/svg" class="icon dark" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z"></path></svg>',
  },
  saveInCookies: false,
  autoMatchOsTheme: true,
  onChange: false
}
const darkmode = new Darky(options);
darkmode.enable();

Methods

Use the following methods to interact with the Darky library:

Example:

const darkmode = new Darky();
darkmode.enable();      // Activates the Darky widget
darkmode.toggle();      // Toggles darkmode on/off
darkmode.isEnabled();   // Returns true if darkmode is enabled

Events

The following events are available:

Example

const darkmode = new Darky(
  onChange: function() {
    console.log('Darkmode was toggled');
    // JS goes here
  }
);
darkmode.enable();

Overrides

By default the following elements are not converted to dark mode: img, picture, video. There are several ways to prevent an element from being converted:

  1. Add the class darkmode--ignore to the elements HTML:
    <span class="darkmode--ignore">I AM NOT DARKMODE<span>
  2. Add the following CSS to the element:
    .element {
      isolation: isolate;
    }
  3. Revert the darkmode like so
    .darkmode--enabled .element {
      mix-blend-mode: difference;
    }

Changelog

Download latest release or see previous releases.


v1.2.1

v1.2.0

v1.1.2

v1.1.1

v1.1.0

v1.0.4

v1.0.3

v1.0.2

v1.0.1

v1.0.0 (initial release)


Made by Tristan White © 2023-2024

The library is inspired by the work of Wei Gao

Documentation · Changelog · GitHub · npm
Download · Request feature · Report bug

This is Darky.js
Click me!