• Stars
    star
    161
  • Rank 232,197 (Top 5 %)
  • Language
    JavaScript
  • License
    GNU General Publi...
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A zero dependency javascript library that enables the night mode in your website easily

Nightly.js

GitHub license dependencies Status npm version Open Source Helpers

A zero dependency Javascript library for enabling dark/night mode in you UI.

Usage

  1. Include Nightly.js
  • Via <script> tag

    <!-- Download this repository then use "dist/nightly.min.js" -->
    <script src="nightly.min.js"></script>
    
    <!-- Or use CDN -->
    <script src="https://cdn.jsdelivr.net/gh/fcmam5/[email protected]/dist/nightly.min.js"></script>
  • Or if you prefer npm, run following command at you project's directory:

    npm install --save nightly.js
    

    Then include it

    var Nightly = require('nightly.js');
  1. In your main JavaScript file initiate the object

    • Using default Parameters with persistence disabled:

      var Nightly = new Nightly();
    • Or customize parameters yourself

      The first parameter is to customize default settings and the second is to enable persistence

      var nightModeConfig = {
        body: 'background color', // Default: #282828
        texts: 'texts color', // Default: #f5f5f5
        inputs: {
          color: 'text color inside inputs', // Default: #f5f5f5
          backgroundColor: 'background color', // Default #313131
        },
        buttons: {
          color: "button's text color", // Default: #f5f5f5
          backgroundColor: "button's backgournd color", // #757575
        },
        links: 'links color (normal state)', // Default: #009688
        classes: [
          // Classes to apply when enabling the dark mode on certain elements
          {
            apply: 'my-selected-class', // just the class name (without the .)
            to: '.my-dark-class-to-the-selected-class .some-nested-class', // uses querySelectorAll
          },
          {
            apply: 'another-selected-class',
            to:
              '.another-dark-class-to-the-selected-class.some-class .some-nested-class',
          },
        ],
      };
      
      var Nightly = new Nightly(nightModeConfig, true); // To disable persistence, set false instead of true
  2. Call the darkify() or the toggle() function

// To enable the dark mode
Nightly.darkify();

// To disable the dark mode
Nightly.lightify();

// To toggle between dark and light modes
Nightly.toggle();
  • You can also pass callbacks to darkify(), lightify(). And toggle() takes two callbacks (enableDarkModeCallback, enableLightModeCallback), for example:
var sayGoodMorning = function () {
  console.log('Good morning !');
};

var sayGoodNight = function () {
  console.log('Good night!');
};

// Pass sayGoodMorning() as callback to darkify
Nightly.darkify(sayGoodMorning);

// toggle() takes two callbacks (darkifyCallback, lightifyCallback)
Nightly.toggle(sayGoodNight, sayGoodMorning);

Example

In our first example we created a simple page as the following:

<style media="screen">
  body {
    padding: 50px;
  }
  #btn {
    height: 50px;
    width: 150px;
  }
  .red-text {
    color: #d32f2f;
  }
</style>

<h1>
  Hello, world <span class="red-text">!</span>
  <button type="button" name="button" id="btn">Toggle</button>
</h1>

<p>Please, <a href="#">Click here</a></p>

<div id="form-container">
  <form>
    <label for="name">Your name</label>
    <input type="text" name="name" value="Hello world" placeholder="name" />
  </form>
</div>

Then the result was as following:

Before using Nightly.js

We included nightly.js just before closing the body tag, initiated Nightly object with no arguments, then set an event listener to a button to execute our toggle() method, that switches between darkify() and lightify()

<script src="../src/nightly.js" charset="utf-8"></script>
<script type="text/javascript">
  // Persistence disabled
  var Nightly = new Nightly();
  document.getElementById("btn").addEventListener("click", function(){
    Nightly.toggle();
  });

</script>
</body>

The result was as following:

After using Nightly.js

TODO

  • Add state persistence: use localstorage
  • Add supported browsers section after testing it
  • Improve usage section
  • Document and refactor the code
  • Continue writing tests
  • Write plugins for frameworks like Bootstrap:
    • Bootstrap
    • Foundation
    • Materialize

License

This project is licensed under the GNU GPL v3.0 License - see the LICENSE file for details

More Repositories

1

algeria-api

The 🇩🇿 API (Work in progress)
JavaScript
78
star
2

jelban-js

Another email addresses filter; filters out disposable and addresses' alias
TypeScript
54
star
3

kr-and-expert-system

Knowledge representation and expert systems examples
Python
51
star
4

oktob.js

مكتبة جافاسكريبت تقوم باستبدال الأحرف اللاتنية عند الكتابة بأحرف عربية (والعكس) مع واجهة برمجة مرنة
JavaScript
38
star
5

telefonilo.js

Make "click to phone-call" links for your mobile users only (accepts encrypted phone numbers)
JavaScript
25
star
6

nx-mermaid-grapher

Create Mermaid graphs for NX dependencies
TypeScript
16
star
7

mini-express-boilerplate

A minimal Express boilerplate with passport user authentication, mongoose and some security setup configured
JavaScript
15
star
8

nest-problem-details

A Nest.js HTTP exceptions filter returning RFC-7807 responses
TypeScript
12
star
9

tchamba

Tchamba.random, is a real random data genarator (letters, jokes, names...)
Python
11
star
10

hamid.js

A library to trigger an action if the user is idle
TypeScript
10
star
11

pin-my-youtube-video-js

A tiny Electron application, YouTube video player. Pin your video on top so you can watch & work
HTML
9
star
12

gharga-cli

GH-Actions-Readme-Generator
TypeScript
4
star
13

express-response-presenter

Standardize ExpressJS JSON responses format
JavaScript
4
star
14

eAlgeria-ladingpage

Trying Next.js by building a landing page for e-Algeria event
3
star
15

project-object

JavaScript
3
star
16

sdh_foundation

JavaScript
2
star
17

express-ms-template

Node/Express template
JavaScript
2
star
18

fcmam5.github.io

A simple portfolio
HTML
2
star
19

AlgerianAdministrativeDivision

Administrative division of Algeria (Wilayas - Daïras - baladiyahs)
1
star
20

berlitz-amel2016-boxyWebComic

A Webcomic hosting site for El Amel project by Berlitz & US Embassy in Algeria
CSS
1
star
21

terraform-playground

HCL
1
star
22

andoird-univ-course-homework

I will push every homework for the university's Android developement course
Java
1
star
23

diagflow-webhook-learn2code-gAssistant

Diagflow webhook for my Google Assistant: Learn to Code
JavaScript
1
star
24

compiler101-compilation-class

A basic compiler for compilation class.
C
1
star
25

learn-pwa

Learning https://developers.google.com/web/ilt/pwa/
1
star
26

jelban-express.js

Using https://github.com/Fcmam5/jelban-js
TypeScript
1
star
27

ab-redpen

A realtime "red pen" application for teachers, as a NodeJs/Expree/Jade/Socket.io first training
JavaScript
1
star
28

operating-systems-class

OS class homework for OS class (Oran University1) and for Udacity's course; Dealing with Processes, threads, POSIX, mutex, semaphores
C
1
star
29

awesome-igmo-informatique

S4 informatique materials All In One, منكم و إليكم :)
1
star
30

tf-spotify-playlist

It worked on my machine
HCL
1
star
31

BloodBank_project

LMD L2/Groupe1/Team1. OOP Project
Java
1
star
32

hackit-sayeh-backend

Backend for Sayeh application: A virtual reality tourism game made in 48 hours in Hack!T hackathon in Algeries 2018
JavaScript
1
star
33

dice-game-tp1

Java
1
star
34

testing-psqldb

Testing PostgreSQL databse
Python
1
star
35

micdrop-cli

Ugly tool for lazy people.. TBD
JavaScript
1
star
36

nestjs-kebab-params-pipe

JavaScript
1
star
37

Bash-Scripts

Bash Scripts
Shell
1
star
38

sidi-el-houari

Python
1
star
39

random-ayah-4u

Random Quran verse for you :)
HTML
1
star
40

fcc-recipe-box

FreeCodeCamp data viasualisation and React track: Build a recipe box with React.js and MobX (https://www.freecodecamp.org/challenges/build-a-recipe-box)
JavaScript
1
star
41

spring101-todo

Java
1
star
42

laughing-couscous

The repo name was happily chosen by Github's random repo-names generator
1
star
43

django_singleton_settings

Reusable "Site settings" Django application
Python
1
star
44

pokemon-fastify

An overengineered coding task
TypeScript
1
star
45

out-n-dor

MobileApplications course project, a simple Android application with a Node/Express microservice as backend
Java
1
star
46

Git4Newbies

Shell
1
star
47

asd-extra-exercices

Additional ASD exercices, Going further with C Inchaa Allah
C
1
star
48

dotfiles

My dotfiles. Script to apply my setups, and install the programs I need.
Shell
1
star
49

consuming-islamic-APIs

Command line adhan app, and more islamic applicaiton just consuming APIs
Python
1
star
50

fcc-nightlife-coordination-app

Building a Nightlife Coordination App for Freecodecamp Dynamic Web Application Projects
JavaScript
1
star
51

spaceAppsChallenge2016-ORApp

An autonomous explorer robot equipped by a Jet-pack, Controlled by distance , the robot can travel on Mars, draw a 2D map and avoid collisions, the Jetpack helps on fixing it on the ground while moving by generating the opposite force of gravity or to fly . The jetpack can also make jumps to check what’s there beyond the obstacles for example: a huge rock that the robot can’t cross.
1
star
52

cs-master-degree-iswt

Pratical work, homework, and exercices given in University for CS Master degree (Information System and Web technlogies speciality)
Python
1
star