darkify-js 1.1.0 → 1.1.1
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/dist/darkify.d.ts +6 -5
- package/dist/darkify.js +30 -20
- package/dist/darkify.min.js +2 -2
- package/package.json +1 -1
package/dist/darkify.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
interface
|
|
1
|
+
interface Options {
|
|
2
2
|
autoMatchTheme?: boolean;
|
|
3
3
|
useLocalStorage?: boolean;
|
|
4
4
|
useSessionStorage?: boolean;
|
|
5
|
-
|
|
5
|
+
useColorScheme: string[];
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
declare class Darkify {
|
|
9
|
-
options:
|
|
9
|
+
options: Options;
|
|
10
10
|
theme: {
|
|
11
11
|
value: string;
|
|
12
12
|
};
|
|
@@ -17,9 +17,10 @@ declare class Darkify {
|
|
|
17
17
|
* @param {string} element Button ID ( recommended ) or HTML element
|
|
18
18
|
* @param {object} options Options
|
|
19
19
|
*/
|
|
20
|
-
constructor(element: string, options:
|
|
21
|
-
getOsPreference(options:
|
|
20
|
+
constructor(element: string, options: Options);
|
|
21
|
+
getOsPreference(options: Options): string | null;
|
|
22
22
|
createAttribute(): void;
|
|
23
|
+
updateTags(css: string, useColorScheme: string[]): void;
|
|
23
24
|
savePreference(): void;
|
|
24
25
|
onClick(): void;
|
|
25
26
|
}
|
package/dist/darkify.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
3
|
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
-
* @version 1.1.
|
|
4
|
+
* @version 1.1.1
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
7
|
(function (global, factory) {
|
|
@@ -25,18 +25,15 @@
|
|
|
25
25
|
console.warn('Both storage options are enabled. Disabling useSessionStorage...');
|
|
26
26
|
options.useSessionStorage = false;
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
options.useLocalStorage = true;
|
|
31
|
-
}
|
|
32
|
-
else if (options.useSessionStorage) {
|
|
28
|
+
// adjust storage options
|
|
29
|
+
if (!options.useLocalStorage) {
|
|
33
30
|
options.useLocalStorage = false;
|
|
34
31
|
}
|
|
35
32
|
const defaultOptions = {
|
|
36
33
|
autoMatchTheme: true,
|
|
37
34
|
useLocalStorage: true,
|
|
38
35
|
useSessionStorage: false,
|
|
39
|
-
|
|
36
|
+
useColorScheme: ['#ffffff', '#000000'],
|
|
40
37
|
};
|
|
41
38
|
this.options = Object.assign({}, defaultOptions, options);
|
|
42
39
|
document.addEventListener('DOMContentLoaded', () => {
|
|
@@ -47,10 +44,12 @@
|
|
|
47
44
|
// sync with system changes
|
|
48
45
|
window
|
|
49
46
|
.matchMedia('(prefers-color-scheme: dark)')
|
|
50
|
-
.addEventListener('change', ({ matches: isDark }) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
.addEventListener('change', ({ matches: isDark, media }) => {
|
|
48
|
+
if (media !== 'print') {
|
|
49
|
+
this.theme.value = isDark ? 'dark' : 'light';
|
|
50
|
+
this.savePreference();
|
|
51
|
+
return window.location.reload();
|
|
52
|
+
}
|
|
54
53
|
});
|
|
55
54
|
this.theme = {
|
|
56
55
|
value: this.getOsPreference(options) ?? 'light',
|
|
@@ -61,37 +60,48 @@
|
|
|
61
60
|
}
|
|
62
61
|
// get os color preference
|
|
63
62
|
getOsPreference(options) {
|
|
64
|
-
|
|
63
|
+
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
64
|
+
const LSTO = useLocalStorage && window.localStorage.getItem(Darkify.storageKey);
|
|
65
|
+
const SSTO = useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey);
|
|
66
|
+
if (LSTO) {
|
|
65
67
|
return window.localStorage.getItem(Darkify.storageKey);
|
|
66
68
|
}
|
|
67
|
-
else if (
|
|
69
|
+
else if (SSTO) {
|
|
68
70
|
return window.sessionStorage.getItem(Darkify.storageKey);
|
|
69
71
|
}
|
|
70
72
|
else {
|
|
71
|
-
return
|
|
73
|
+
return autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
72
74
|
? 'dark'
|
|
73
75
|
: 'light';
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
78
|
createAttribute() {
|
|
77
79
|
let dataTheme = document.getElementsByTagName('html')[0];
|
|
80
|
+
let { useColorScheme } = this.options;
|
|
78
81
|
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}}`;
|
|
82
|
+
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
83
|
+
this.updateTags(css, useColorScheme);
|
|
84
|
+
this.savePreference();
|
|
85
|
+
}
|
|
86
|
+
updateTags(css, useColorScheme) {
|
|
79
87
|
let head = document.head;
|
|
80
|
-
//
|
|
88
|
+
// update theme-color meta tag
|
|
81
89
|
this.metaTag.setAttribute('name', 'theme-color');
|
|
82
|
-
this.metaTag.setAttribute('content', this.theme.value === 'light' ?
|
|
90
|
+
this.metaTag.setAttribute('content', this.theme.value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
91
|
+
// update style tag
|
|
83
92
|
this.cssTag.setAttribute('type', 'text/css');
|
|
84
93
|
this.cssTag.innerHTML = css;
|
|
85
|
-
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
86
94
|
head.appendChild(this.metaTag);
|
|
87
95
|
head.appendChild(this.cssTag);
|
|
88
|
-
this.savePreference();
|
|
89
96
|
}
|
|
90
97
|
// save to local or session storage
|
|
91
98
|
savePreference() {
|
|
92
|
-
const
|
|
93
|
-
const
|
|
99
|
+
const { useLocalStorage } = this.options;
|
|
100
|
+
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
101
|
+
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
102
|
+
// remove unused storage
|
|
94
103
|
OTS.removeItem(Darkify.storageKey);
|
|
104
|
+
// set storage value
|
|
95
105
|
STO.setItem(Darkify.storageKey, this.theme.value);
|
|
96
106
|
}
|
|
97
107
|
onClick() {
|
package/dist/darkify.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
3
|
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
-
* @version 1.1.
|
|
4
|
+
* @version 1.1.1
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
|
-
var Darkify=function(){"use strict";const e="undefined"!=typeof window;class t{constructor(t,s){if(!e)return;s.useLocalStorage&&s.useSessionStorage
|
|
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.useLocalStorage=!1);this.options=Object.assign({},{autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColorScheme:["#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,media:t})=>{if("print"!==t)return 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){const{autoMatchTheme:s,useLocalStorage:a,useSessionStorage:o}=e,r=a&&window.localStorage.getItem(t.storageKey),i=o&&window.sessionStorage.getItem(t.storageKey);return r?window.localStorage.getItem(t.storageKey):i?window.sessionStorage.getItem(t.storageKey):s&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}createAttribute(){let e=document.getElementsByTagName("html")[0],{useColorScheme:t}=this.options,s=`/**! Darkify / Easy dark mode for your site **/:root:is([data-theme="${this.theme.value}"]), [data-theme="${this.theme.value}"] {color-scheme: ${this.theme.value}}`;e.setAttribute("data-theme",this.theme.value),this.updateTags(s,t),this.savePreference()}updateTags(e,t){let s=document.head;this.metaTag.setAttribute("name","theme-color"),this.metaTag.setAttribute("content","light"===this.theme.value?t[0]:t[1]),this.cssTag.setAttribute("type","text/css"),this.cssTag.innerHTML=e,s.appendChild(this.metaTag),s.appendChild(this.cssTag)}savePreference(){const{useLocalStorage:e}=this.options,s=e?window.localStorage:window.sessionStorage;(e?window.sessionStorage:window.localStorage).removeItem(t.storageKey),s.setItem(t.storageKey,this.theme.value)}onClick(){this.theme.value="light"===this.theme.value?"dark":"light",this.createAttribute()}}return t.storageKey="darkify-theme",t}();
|