darkify-js 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/darkify.d.ts +27 -0
- package/dist/darkify.js +106 -0
- package/dist/darkify.min.js +7 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Emilio Romero
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Darkify js
|
|
2
|
+
Create an easy dark mode for your site
|
|
3
|
+
## 📦 Installation
|
|
4
|
+
```bash
|
|
5
|
+
npm install darkify-js
|
|
6
|
+
```
|
|
7
|
+
## ⚙️ Setup
|
|
8
|
+
```js
|
|
9
|
+
import Darkify from 'darkify-js'
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
autoMatchTheme: true,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// autoMatchTheme: default is true
|
|
16
|
+
// useLocalStorage: default is true
|
|
17
|
+
// useSessionStorage: default is false
|
|
18
|
+
|
|
19
|
+
var darkMode = new Darkify('#element', options)
|
|
20
|
+
```
|
|
21
|
+
### CDN
|
|
22
|
+
|
|
23
|
+
```html
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/darkify-js"></script>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Compatibility
|
|
28
|
+
[Check browser compatibility](https://caniuse.com/mdn-css_properties_color-scheme)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface options {
|
|
2
|
+
autoMatchTheme?: boolean;
|
|
3
|
+
useLocalStorage?: boolean;
|
|
4
|
+
useSessionStorage?: boolean;
|
|
5
|
+
useColors: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare class Darkify {
|
|
9
|
+
options: options;
|
|
10
|
+
theme: {
|
|
11
|
+
value: string;
|
|
12
|
+
};
|
|
13
|
+
cssTag: HTMLStyleElement;
|
|
14
|
+
metaTag: HTMLMetaElement;
|
|
15
|
+
private static readonly storageKey;
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} element Button ID ( recommended ) or HTML element
|
|
18
|
+
* @param {object} options Options
|
|
19
|
+
*/
|
|
20
|
+
constructor(element: string, options: options);
|
|
21
|
+
getOsPreference(options: options): string | null;
|
|
22
|
+
createAttribute(): void;
|
|
23
|
+
savePreference(): void;
|
|
24
|
+
onClick(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { Darkify as default };
|
package/dist/darkify.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
+
* @version 1.1.0
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
(function (global, factory) {
|
|
8
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
9
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
10
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Darkify = factory());
|
|
11
|
+
})(this, (function () { 'use strict';
|
|
12
|
+
|
|
13
|
+
const isBrowser = typeof window !== 'undefined';
|
|
14
|
+
class Darkify {
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} element Button ID ( recommended ) or HTML element
|
|
17
|
+
* @param {object} options Options
|
|
18
|
+
*/
|
|
19
|
+
constructor(element, options) {
|
|
20
|
+
if (!isBrowser) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// avoid using both values
|
|
24
|
+
if (options.useLocalStorage && options.useSessionStorage) {
|
|
25
|
+
console.warn('Both storage options are enabled. Disabling useSessionStorage...');
|
|
26
|
+
options.useSessionStorage = false;
|
|
27
|
+
}
|
|
28
|
+
else if (!options.useLocalStorage && !options.useSessionStorage) {
|
|
29
|
+
console.warn('Both storage options are disabled. Enabling useLocalStorage...');
|
|
30
|
+
options.useLocalStorage = true;
|
|
31
|
+
}
|
|
32
|
+
else if (options.useSessionStorage) {
|
|
33
|
+
options.useLocalStorage = false;
|
|
34
|
+
}
|
|
35
|
+
const defaultOptions = {
|
|
36
|
+
autoMatchTheme: true,
|
|
37
|
+
useLocalStorage: true,
|
|
38
|
+
useSessionStorage: false,
|
|
39
|
+
useColors: ['#ffffff', '#000000'],
|
|
40
|
+
};
|
|
41
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
42
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
43
|
+
this.createAttribute();
|
|
44
|
+
const htmlElement = document.querySelector(element);
|
|
45
|
+
htmlElement?.addEventListener('click', () => this.onClick());
|
|
46
|
+
});
|
|
47
|
+
// sync with system changes
|
|
48
|
+
window
|
|
49
|
+
.matchMedia('(prefers-color-scheme: dark)')
|
|
50
|
+
.addEventListener('change', ({ matches: isDark }) => {
|
|
51
|
+
this.theme.value = isDark ? 'dark' : 'light';
|
|
52
|
+
this.savePreference();
|
|
53
|
+
return window.location.reload();
|
|
54
|
+
});
|
|
55
|
+
this.theme = {
|
|
56
|
+
value: this.getOsPreference(options) ?? 'light',
|
|
57
|
+
};
|
|
58
|
+
this.cssTag = document.createElement('style');
|
|
59
|
+
this.metaTag = document.createElement('meta');
|
|
60
|
+
this.createAttribute();
|
|
61
|
+
}
|
|
62
|
+
// get os color preference
|
|
63
|
+
getOsPreference(options) {
|
|
64
|
+
if (options.useLocalStorage && window.localStorage.getItem(Darkify.storageKey)) {
|
|
65
|
+
return window.localStorage.getItem(Darkify.storageKey);
|
|
66
|
+
}
|
|
67
|
+
else if (options.useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey)) {
|
|
68
|
+
return window.sessionStorage.getItem(Darkify.storageKey);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return options.autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
72
|
+
? 'dark'
|
|
73
|
+
: 'light';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
createAttribute() {
|
|
77
|
+
let dataTheme = document.getElementsByTagName('html')[0];
|
|
78
|
+
let css = `/**! Darkify / Easy dark mode for your site **/:root:is([data-theme="${this.theme.value}"]), [data-theme="${this.theme.value}"] {color-scheme: ${this.theme.value}}`;
|
|
79
|
+
let head = document.head;
|
|
80
|
+
// set theme-color meta tag
|
|
81
|
+
this.metaTag.setAttribute('name', 'theme-color');
|
|
82
|
+
this.metaTag.setAttribute('content', this.theme.value === 'light' ? this.options.useColors[0] : this.options.useColors[1]);
|
|
83
|
+
this.cssTag.setAttribute('type', 'text/css');
|
|
84
|
+
this.cssTag.innerHTML = css;
|
|
85
|
+
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
86
|
+
head.appendChild(this.metaTag);
|
|
87
|
+
head.appendChild(this.cssTag);
|
|
88
|
+
this.savePreference();
|
|
89
|
+
}
|
|
90
|
+
// save to local or session storage
|
|
91
|
+
savePreference() {
|
|
92
|
+
const STO = this.options.useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
93
|
+
const OTS = this.options.useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
94
|
+
OTS.removeItem(Darkify.storageKey);
|
|
95
|
+
STO.setItem(Darkify.storageKey, this.theme.value);
|
|
96
|
+
}
|
|
97
|
+
onClick() {
|
|
98
|
+
this.theme.value = this.theme.value === 'light' ? 'dark' : 'light';
|
|
99
|
+
this.createAttribute();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
Darkify.storageKey = 'darkify-theme';
|
|
103
|
+
|
|
104
|
+
return Darkify;
|
|
105
|
+
|
|
106
|
+
}));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
+
* @version 1.1.0
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
var Darkify=function(){"use strict";const e="undefined"!=typeof window;class t{constructor(t,s){if(!e)return;s.useLocalStorage&&s.useSessionStorage?(console.warn("Both storage options are enabled. Disabling useSessionStorage..."),s.useSessionStorage=!1):s.useLocalStorage||s.useSessionStorage?s.useSessionStorage&&(s.useLocalStorage=!1):(console.warn("Both storage options are disabled. Enabling useLocalStorage..."),s.useLocalStorage=!0);this.options=Object.assign({},{autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColors:["#ffffff","#000000"]},s),document.addEventListener("DOMContentLoaded",(()=>{this.createAttribute();document.querySelector(t)?.addEventListener("click",(()=>this.onClick()))})),window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(({matches:e})=>(this.theme.value=e?"dark":"light",this.savePreference(),window.location.reload()))),this.theme={value:this.getOsPreference(s)??"light"},this.cssTag=document.createElement("style"),this.metaTag=document.createElement("meta"),this.createAttribute()}getOsPreference(e){return e.useLocalStorage&&window.localStorage.getItem(t.storageKey)?window.localStorage.getItem(t.storageKey):e.useSessionStorage&&window.sessionStorage.getItem(t.storageKey)?window.sessionStorage.getItem(t.storageKey):e.autoMatchTheme&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}createAttribute(){let e=document.getElementsByTagName("html")[0],t=`/**! Darkify / Easy dark mode for your site **/:root:is([data-theme="${this.theme.value}"]), [data-theme="${this.theme.value}"] {color-scheme: ${this.theme.value}}`,s=document.head;this.metaTag.setAttribute("name","theme-color"),this.metaTag.setAttribute("content","light"===this.theme.value?this.options.useColors[0]:this.options.useColors[1]),this.cssTag.setAttribute("type","text/css"),this.cssTag.innerHTML=t,e.setAttribute("data-theme",this.theme.value),s.appendChild(this.metaTag),s.appendChild(this.cssTag),this.savePreference()}savePreference(){const e=this.options.useLocalStorage?window.localStorage:window.sessionStorage;(this.options.useLocalStorage?window.sessionStorage:window.localStorage).removeItem(t.storageKey),e.setItem(t.storageKey,this.theme.value)}onClick(){this.theme.value="light"===this.theme.value?"dark":"light",this.createAttribute()}}return t.storageKey="darkify-theme",t}();
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "darkify-js",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Create an easy dark mode for your site",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/darkify.js",
|
|
7
|
+
"types": "dist/darkify.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/darkify.d.ts",
|
|
10
|
+
"dist/darkify.js",
|
|
11
|
+
"dist/darkify.min.js",
|
|
12
|
+
"dist/darkify.min.js.map"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"_cls": "rm -rf dist",
|
|
16
|
+
"_bundle": "rollup -c rollup.config.ts --configPlugin typescript",
|
|
17
|
+
"prettier": "prettier --write src/**/*",
|
|
18
|
+
"build": "npm run _cls && npm run _bundle"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"color-scheme",
|
|
22
|
+
"toggle",
|
|
23
|
+
"dark-mode",
|
|
24
|
+
"dark-theme"
|
|
25
|
+
],
|
|
26
|
+
"author": "Emilio Romero <emrocode@gmail.com>",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/emrocode/darkify-js"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/emrocode/darkify-js#readme",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/emrocode/darkify-js/issues"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@rollup/plugin-terser": "^0.1.0",
|
|
38
|
+
"@rollup/plugin-typescript": "^9.0.2",
|
|
39
|
+
"prettier": "^2.8.0",
|
|
40
|
+
"rollup": "^3.4.0",
|
|
41
|
+
"rollup-plugin-dts": "^5.0.0",
|
|
42
|
+
"tslib": "^2.4.1",
|
|
43
|
+
"typescript": "^4.9.3"
|
|
44
|
+
}
|
|
45
|
+
}
|