All Projects â†’ ColinEspinas â†’ Darken

ColinEspinas / Darken

Licence: mit
🌑 Dark mode made easy

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Darken

previewer
A super light-weight JavaScript image previewer [not actively maintained]
Stars: ✭ 24 (-95.8%)
Mutual labels:  vanilla-javascript, javascript-library
Fine Uploader
Multiple file upload plugin with image previews, drag and drop, progress bars. S3 and Azure support, image scaling, form support, chunking, resume, pause, and tons of other features.
Stars: ✭ 8,158 (+1328.72%)
Mutual labels:  javascript-library, vanilla-javascript
lexicon-mono-seq
DOM Text Based Multiple Sequence Alignment Library
Stars: ✭ 15 (-97.37%)
Mutual labels:  vanilla-javascript, javascript-library
Bin
A tiny (<1kb) localStorage and sessionStorage helper library.
Stars: ✭ 70 (-87.74%)
Mutual labels:  javascript-library, vanilla-javascript
Josh.js
A JavaScript library to animate content on page scroll.
Stars: ✭ 345 (-39.58%)
Mutual labels:  javascript-library, vanilla-javascript
Creepyface
A JavaScript library that makes your face follow the pointer. đŸ€ȘđŸ–±ïžđŸ‘†
Stars: ✭ 412 (-27.85%)
Mutual labels:  javascript-library
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-17.16%)
Mutual labels:  vanilla-javascript
Scrollload
scroll bottom load more data pull refresh æ»šćŠšćˆ°ćș•éƒšćŠ èœœæ›Žć€šæ•°æź äž‹æ‹‰ćˆ·æ–°
Stars: ✭ 411 (-28.02%)
Mutual labels:  javascript-library
Adminkit
🧰 AdminKit is a free & open source Bootstrap 5 Admin Template
Stars: ✭ 395 (-30.82%)
Mutual labels:  vanilla-javascript
Typed Graphqlify
Build Typed GraphQL Queries in TypeScript. A better TypeScript + GraphQL experience.
Stars: ✭ 553 (-3.15%)
Mutual labels:  javascript-library
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (-5.43%)
Mutual labels:  javascript-library
Minio Js
MinIO Client SDK for Javascript
Stars: ✭ 468 (-18.04%)
Mutual labels:  javascript-library
Machinejs
[UNMAINTAINED] Automated machine learning- just give it a data file! Check out the production-ready version of this project at ClimbsRocks/auto_ml
Stars: ✭ 412 (-27.85%)
Mutual labels:  javascript-library
Integrations
Ways to incorporate Svelte into your stack
Stars: ✭ 487 (-14.71%)
Mutual labels:  javascript-library
Must Watch Css
A useful list of must-watch talks about CSS
Stars: ✭ 3,966 (+594.57%)
Mutual labels:  css-variables
Filterizr
✹ Filterizr is a JavaScript library that sorts, shuffles and filters responsive galleries using CSS3 transitions ✹
Stars: ✭ 546 (-4.38%)
Mutual labels:  javascript-library
Pageswitch
Just a page-Switch Javascript Library
Stars: ✭ 397 (-30.47%)
Mutual labels:  javascript-library
Jsstore
A complete IndexedDB wrapper with SQL like syntax.
Stars: ✭ 430 (-24.69%)
Mutual labels:  javascript-library
Heapify
The fastest JavaScript priority queue out there. Zero dependencies.
Stars: ✭ 520 (-8.93%)
Mutual labels:  javascript-library
Mainloop.js
Provides a well-constructed main loop useful for JavaScript games and other animated or time-dependent applications.
Stars: ✭ 425 (-25.57%)
Mutual labels:  javascript-library

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].