Giter Site home page Giter Site logo

colinespinas / darken Goto Github PK

View Code? Open in Web Editor NEW
607.0 11.0 33.0 742 KB

๐ŸŒ‘ Dark mode made easy

Home Page: https://colinespinas.github.io/darken/

License: MIT License

JavaScript 100.00%
css-variables user-preference javascript javascript-library prefers-color-scheme darkmode vanilla-javascript

darken's Introduction

darken

Codacy Badge Size badge Npm badge Issue Badge Licence Badge

A lightweight and cross-browser library that allows you to easely manage your dark mode for your websites and applications.

Written in plain vanilla javascript.

Features

  • Easy control over your dark mode
  • Custom class on dark mode
  • Switch CSS variables values on light/dark modes
  • Switch between different stylesheets on light/dark modes
  • Use prefers-color-scheme to get user preference
  • Use timestamps to change modes at chosen times
  • Save user preference to local storage

Check the demo to get a live exemple.

Table of Contents

Getting Started

โœˆ๏ธ Using the CDN

Just use this snippet to include darken to your code:

<script src="https://unpkg.com/darken"></script>
<script>
	const darkmode = new darken();
</script>

๐Ÿ“ฆ Using NPM

Install darken with npm:

npm install darken

And import it in your code:

import darken from 'darken';

const darkmode = new darken();

Usage

Here is a basic usage of darken:

<!-- index.html -->

<button id="darkmode-button">Toggle dark mode</button>

<script src="path/to/darken"></script>
<script>
	const darkmode = new darken({
		class: "darkmode-active",
		variables: {
			"--primary-color" : ["#000000", "#fafafa"],
			"--background-color" : ["#fafafa", "#000000"]
		},
		toggle: "#darkmode-button",
	});
</script>

You can either use a class and/or CSS variables to customize your styles.

Options

const defaultOptions = {
	container: null,
	default: "light",
	toggle: null,
	remember: "darken-mode",
	usePrefersColorScheme: true,
	timestamps: {},
	class: "darken",
	variables: {},
	stylesheets: {},
}

container

Type: String, Default: null

Element selector to the container of darken. The dark mode will be applied only to the selected container.

If the value is left to null, the document element will be selected instead.

default

Type: String, Default: "light"

Defines the default mode on page load.

toggle

Type: String, Default: null

Element selector to the toggle button, the selected element will call the toggle method on click.

remember

Type: String, Default: "darken-mode"

Name of the value stored in the browser local storage, this value contains the active mode ("dark" or "light").

If this option is set to null the active mode is not stored.

usePrefersColorScheme

Type: Boolean, Default: true

If true the prefers-color-scheme media query will be used to determine the default mode.

If the remember option is active, this will only be used if no active mode is stored (in most case the first time a user comes to the app/website). If the remember option is not active then a listener will be added to the prefers-color-scheme media query for live update.

stylesheets

Type: Object, Default: {}

Define the stylesheet path of each mode. This stylesheet href will be changed on mode switch dynamically. The stylesheet will be removed if no path is given for a mode.

Default stylesheet id is darken-stylesheet but can be changed.

stylesheets: {
	id: "darkmode-stylesheet",
	dark: "dark.css",
	light: "light.css",
}

timestamps

Type: Object, Default: {}

Define a timestamps mode switch to start light and dark modes at given times of the day.

There is 2 keys to the object, dark and light, the values of those keys are defining the times (using the format <hours>:<minutes>) at wich the corresponding mode will start being active.

If the remember option is active, this will only be used if no active mode is stored (in most case the first time a user comes to the app/website).

If the usePrefersColorScheme option is active, this option will not be used.

timestamps: {
	dark: "20:00",
	light: "6:00",
}

class

Type: String, Default: "darken"

Class that will be added to the selected container when the dark Fmode is active. The class is removed of the selected container once the dark mode is disabled.

If no container is selected, the class will be added to the body element instead.

variables

Type: Object, Default: {}

List of CSS variables that will change when the dark mode is active.

The keys of the object are the variables names, the value are arrays where the 1th index is the value the variable will take in lightmode and the 2nd index the value the variable will take in dark mode. This is the most compact way to use variables.

variables: {
	"--name-of-the-variable": ["light mode value", "dark mode value"],
	"--background-color": ["white", "black"],
}

If you prefer, you can use an object based syntax. This syntax is more clear but less compact.

variables: {
	"--name-of-the-variable": {
		light: "light mode value",
		dark: "dark mode value"
	},
	"--background-color": {
		light: "white", 
		dark: "black"
	},
}

API

The darken class is the entry point to the library.

const darkmode = new darken(options, callback);

See details about options above. The options are optional.

The callback method will be called at every mode switch with a active boolean parameter. The callback is optional.

const darkmode = new darken(function(active) {
	if (active) console.log("Dark mode is active");
	else console.log("Dark mode is inactive");
});

toggle()

Return: none

Toggles dark mode.

darkmode.toggle();

on()

Return: none

Enables dark mode.

darkmode.on();

off()

Return: none

Disables dark mode.

darkmode.off();

Testing

You can launch tests and generate a coverage report using the following npm command :

npm test

If you want to test while developing, another command triggers the watch mode (But it does not generate coverage report) :

npm run test:watch

Contributing

Any help and contribution is always welcome, feel free to submit issues and/or contribute to the project.

  1. Fork the Project
  2. Create your Feature or Fix Branch (git checkout -b feature/feature-name or git checkout -b fix/fix-name)
  3. Commit your Changes (git commit -m 'Add some feature or fix')
  4. Push to the Branch (git push origin feature/feature-name or git push origin fix/fix-name)
  5. Open a Pull Request

License

darken is distributed under the MIT License. See LICENSE for more information.

Contact

Buy me a coffee badge LinkedIn badge

Colin Espinas - Website - [email protected]

Project link: https://github.com/ColinEspinas/darken

Acknowledgements

darken's People

Contributors

codacy-badger avatar colinespinas avatar geopic avatar southhill 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

darken's Issues

TypeScript type definition file (I can add if you wish)

Is your feature request related to a problem? Please describe.
Yes, when people use this package with TypeScript they will need to have type definitions in order to know which type of arguments / return value is expected.

Describe the solution you'd like
Developers using this package in their TypeScript projects being notified about the proper type of value passed in to this module.

Describe alternatives you've considered
N/A

Additional context
It helps keep developers' code type-safe.

Thank you.

Doesn't work with checkbox/switch element instead of button

Is your feature request related to a problem? Please describe.
I tried using this script with a modified checkbox element, styled to look like a toggle switch. This project's scripts only seem to work with a button element. Toggling the switch just has no effect.

Describe the solution you'd like
It'd be cool if this could also work with a toggle switch/modified checkbox and have it remember the checked/unchecked status of the checkbox.

Describe alternatives you've considered
I've seen buttons styled as switches using CSS, that's a possibility, but I already have checkbox/switches in my project that are styled how I want them to look. Additionally I could just use a button instead, it's just not my preference.

Additional context
My project is the Tint and Shade Generator.

Screen Shot 2020-12-27 at 11 20 32 AM

Update/Add tests using Jest

Is your feature request related to a problem? Please describe.
Tests are an important part of trusting that a bunch of code will work as intended and ensure not to break a project when updating it. It would be great to add/update tests for Darken.

Describe the solution you'd like
Update and add missing tests using Jest (already installed in deps).

Dynamic stylesheet change on mode switch

Is your feature request related to a problem? Please describe.
Yes, it could be good to have an option to change stylesheets by changing from light to dark mode.

Describe the solution you'd like
The option can take a stylesheet path for each mode.

Exemple of implementation :

const darkmode = new darken({
	class: "darkmode-active",
	stylesheets: {
		dark: "dark.css",
		light: "light.css",
	}
	variables: {
		"--primary-color" : ["#000000", "#fafafa"],
		"--background-color" : ["#fafafa", "#000000"]
	},
});

Usage as render-blocking script

Is your feature request related to a problem? Please describe.
My issue is that every new page always loads in light mode initially. Meaning that there's a very short flicker when the browser renders the light mode before finding out the user wants dark mode. This makes the implementation look a little unprofessional and is actually annoying when using the site in total darkness.

Describe the solution you'd like
I've looked around and it seems that the solution would be to initialize the dark mode and do all the checks in a script tag placed in <head>. This way, the check is blocking the render for a few extra milliseconds until it figures out the right theme.

Describe alternatives you've considered
I tried to place my darkmode = new darken(options, callback) function in the <head> right after calling the CDN but the browser says Uncaught TypeError: Cannot read property classList of null which occurs 3 times (lines 31, 36, and 96 of the prettified Chrome console).

My options object doesn't set a toggle target nor a container as the DOM is not rendered yet. So I don't know what the issue is.

Additional context
Any other solution that could help me eliminate the flicker would also be appreciated! Cheers

Uncaught TypeError: Cannot read property 'addEventListener'

There is a return error Uncaught: TypeError

Uncaught TypeError: Cannot read property 'addEventListener' of null
    at new e (darken.umd.js:1)
    at event:26

Screenshots

The error causes the toggle to not function.

Desktop

  • OS: Deepin Linux
  • Browser: Chrome
  • Version: Version 78.0.3904.97 (Official Build) (64-bit)

Timestamps based mode switch

Is your feature request related to a problem? Please describe.
Yes, when a user come to an app/website to a fixed hour it will be cool to change the theme depending on the time.

Describe the solution you'd like
The option can take timestamps as an input to switch modes.

Exemple of implementation :

const darkmode = new darken({
	class: "darkmode-active",
	timestamps: {
		dark: "20:00",
		light: "6:00",
	}
	variables: {
		"--primary-color" : ["#000000", "#fafafa"],
		"--background-color" : ["#fafafa", "#000000"]
	},
});

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.