darkify-js 1.1.4 → 1.1.5
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/README.md +1 -1
- package/dist/darkify.cjs.js +86 -60
- package/dist/darkify.d.ts +12 -11
- package/dist/darkify.esm.js +86 -58
- package/dist/darkify.min.js +2 -2
- package/dist/darkify.umd.js +2 -2
- package/package.json +25 -20
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Darkify JS
|
|
2
|
+
[](https://github.com/emrocode/darkify-js/actions/workflows/tests.yml)
|
|
2
3
|
|
|
3
4
|
🌚 A simple dark mode toggle library that makes it easy to implement dark mode on your website without additional configuration
|
|
4
5
|
|
|
5
|
-
> [!NOTE]
|
|
6
6
|
> Please make sure to read the [Wiki] for detailed documentation and examples
|
|
7
7
|
|
|
8
8
|
### 📦 Installation
|
package/dist/darkify.cjs.js
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
3
|
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
-
* @version 1.1.
|
|
4
|
+
* @version 1.1.5
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
10
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
11
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
12
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
13
|
+
}
|
|
14
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
15
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
16
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
17
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
18
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
19
|
+
}
|
|
20
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
21
|
+
var e = new Error(message);
|
|
22
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
23
|
+
};
|
|
10
24
|
|
|
11
25
|
const isBrowser = typeof window !== 'undefined';
|
|
12
26
|
|
|
@@ -17,72 +31,84 @@ const defaultOptions = {
|
|
|
17
31
|
useColorScheme: ['#ffffff', '#000000'],
|
|
18
32
|
};
|
|
19
33
|
|
|
34
|
+
var _Darkify_instances, _a, _Darkify_theme, _Darkify_cssTag, _Darkify_metaTag, _Darkify_init, _Darkify_getOsPreference, _Darkify_createAttribute, _Darkify_updateTags, _Darkify_savePreference, _Darkify_syncThemeBetweenTabs;
|
|
20
35
|
class Darkify {
|
|
21
36
|
constructor(element, options) {
|
|
37
|
+
_Darkify_instances.add(this);
|
|
22
38
|
this.options = {};
|
|
23
|
-
|
|
39
|
+
_Darkify_theme.set(this, void 0);
|
|
40
|
+
_Darkify_cssTag.set(this, void 0);
|
|
41
|
+
_Darkify_metaTag.set(this, void 0);
|
|
42
|
+
if (!isBrowser)
|
|
24
43
|
return;
|
|
25
|
-
|
|
26
|
-
options
|
|
27
|
-
options
|
|
28
|
-
options = { ...defaultOptions, ...options };
|
|
44
|
+
(options === null || options === void 0 ? void 0 : options.useLocalStorage) && (options.useSessionStorage = false);
|
|
45
|
+
(options === null || options === void 0 ? void 0 : options.useSessionStorage) && (options.useLocalStorage = false);
|
|
46
|
+
options = Object.assign(Object.assign({}, defaultOptions), options);
|
|
29
47
|
this.options = options;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
.matchMedia('(prefers-color-scheme: dark)')
|
|
37
|
-
.addEventListener('change', ({ matches: isDark }) => {
|
|
38
|
-
this.theme.value = isDark ? 'dark' : 'light';
|
|
39
|
-
this.savePreference();
|
|
40
|
-
});
|
|
41
|
-
this.theme = {
|
|
42
|
-
value: this.getOsPreference(options) ?? 'light',
|
|
43
|
-
};
|
|
44
|
-
this.cssTag = document.createElement('style');
|
|
45
|
-
this.metaTag = document.createElement('meta');
|
|
46
|
-
this.createAttribute();
|
|
47
|
-
}
|
|
48
|
-
getOsPreference(options) {
|
|
49
|
-
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
50
|
-
const STO = (useLocalStorage && window.localStorage.getItem(Darkify.storageKey)) ||
|
|
51
|
-
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey));
|
|
52
|
-
return (STO ||
|
|
53
|
-
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
54
|
-
? 'dark'
|
|
55
|
-
: 'light'));
|
|
48
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_init).call(this, element);
|
|
49
|
+
__classPrivateFieldSet(this, _Darkify_theme, { value: __classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_getOsPreference).call(this, options) }, "f");
|
|
50
|
+
__classPrivateFieldSet(this, _Darkify_cssTag, document.createElement('style'), "f");
|
|
51
|
+
__classPrivateFieldSet(this, _Darkify_metaTag, document.createElement('meta'), "f");
|
|
52
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
53
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_syncThemeBetweenTabs).call(this);
|
|
56
54
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let css = `/**! Darkify / A simple dark mode toggle library **/:root:is([data-theme="${this.theme.value}"]), [data-theme="${this.theme.value}"] {color-scheme: ${this.theme.value}}`;
|
|
61
|
-
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
62
|
-
this.updateTags(css, useColorScheme || []);
|
|
63
|
-
this.savePreference();
|
|
55
|
+
toggleTheme() {
|
|
56
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = __classPrivateFieldGet(this, _Darkify_theme, "f").value === 'light' ? 'dark' : 'light';
|
|
57
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
64
58
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.metaTag.setAttribute('name', 'theme-color');
|
|
68
|
-
this.metaTag.setAttribute('content', this.theme.value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
69
|
-
this.cssTag.setAttribute('type', 'text/css');
|
|
70
|
-
this.cssTag.innerHTML = css;
|
|
71
|
-
head.appendChild(this.metaTag);
|
|
72
|
-
head.appendChild(this.cssTag);
|
|
73
|
-
}
|
|
74
|
-
savePreference() {
|
|
75
|
-
const { useLocalStorage } = this.options;
|
|
76
|
-
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
77
|
-
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
78
|
-
OTS.removeItem(Darkify.storageKey);
|
|
79
|
-
STO.setItem(Darkify.storageKey, this.theme.value);
|
|
80
|
-
}
|
|
81
|
-
onClick() {
|
|
82
|
-
this.theme.value = this.theme.value === 'light' ? 'dark' : 'light';
|
|
83
|
-
this.createAttribute();
|
|
59
|
+
getCurrentTheme() {
|
|
60
|
+
return __classPrivateFieldGet(this, _Darkify_theme, "f").value;
|
|
84
61
|
}
|
|
85
62
|
}
|
|
63
|
+
_a = Darkify, _Darkify_theme = new WeakMap(), _Darkify_cssTag = new WeakMap(), _Darkify_metaTag = new WeakMap(), _Darkify_instances = new WeakSet(), _Darkify_init = function _Darkify_init(element) {
|
|
64
|
+
window
|
|
65
|
+
.matchMedia('(prefers-color-scheme: dark)')
|
|
66
|
+
.addEventListener('change', ({ matches: isDark }) => {
|
|
67
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = isDark ? 'dark' : 'light';
|
|
68
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_savePreference).call(this);
|
|
69
|
+
});
|
|
70
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
71
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
72
|
+
const htmlElement = document.querySelector(element);
|
|
73
|
+
htmlElement === null || htmlElement === void 0 ? void 0 : htmlElement.addEventListener('click', () => this.toggleTheme());
|
|
74
|
+
});
|
|
75
|
+
}, _Darkify_getOsPreference = function _Darkify_getOsPreference(options) {
|
|
76
|
+
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
77
|
+
const STO = (useLocalStorage && window.localStorage.getItem(_a.storageKey)) ||
|
|
78
|
+
(useSessionStorage && window.sessionStorage.getItem(_a.storageKey));
|
|
79
|
+
return (STO ||
|
|
80
|
+
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
81
|
+
? 'dark'
|
|
82
|
+
: 'light'));
|
|
83
|
+
}, _Darkify_createAttribute = function _Darkify_createAttribute() {
|
|
84
|
+
const dataTheme = document.getElementsByTagName('html')[0];
|
|
85
|
+
const { useColorScheme } = this.options;
|
|
86
|
+
let css = `/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${__classPrivateFieldGet(this, _Darkify_theme, "f").value}"]),[data-theme="${__classPrivateFieldGet(this, _Darkify_theme, "f").value}"]{color-scheme:${__classPrivateFieldGet(this, _Darkify_theme, "f").value}}`;
|
|
87
|
+
dataTheme.setAttribute('data-theme', __classPrivateFieldGet(this, _Darkify_theme, "f").value);
|
|
88
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_updateTags).call(this, css, useColorScheme || []);
|
|
89
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_savePreference).call(this);
|
|
90
|
+
}, _Darkify_updateTags = function _Darkify_updateTags(css, useColorScheme) {
|
|
91
|
+
const head = document.head || document.getElementsByTagName('head')[0];
|
|
92
|
+
__classPrivateFieldGet(this, _Darkify_metaTag, "f").setAttribute('name', 'theme-color');
|
|
93
|
+
__classPrivateFieldGet(this, _Darkify_metaTag, "f").setAttribute('content', __classPrivateFieldGet(this, _Darkify_theme, "f").value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
94
|
+
__classPrivateFieldGet(this, _Darkify_cssTag, "f").setAttribute('type', 'text/css');
|
|
95
|
+
__classPrivateFieldGet(this, _Darkify_cssTag, "f").innerHTML = css;
|
|
96
|
+
head.appendChild(__classPrivateFieldGet(this, _Darkify_metaTag, "f"));
|
|
97
|
+
head.appendChild(__classPrivateFieldGet(this, _Darkify_cssTag, "f"));
|
|
98
|
+
}, _Darkify_savePreference = function _Darkify_savePreference() {
|
|
99
|
+
const { useLocalStorage } = this.options;
|
|
100
|
+
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
101
|
+
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
102
|
+
OTS.removeItem(_a.storageKey);
|
|
103
|
+
STO.setItem(_a.storageKey, __classPrivateFieldGet(this, _Darkify_theme, "f").value);
|
|
104
|
+
}, _Darkify_syncThemeBetweenTabs = function _Darkify_syncThemeBetweenTabs() {
|
|
105
|
+
window.addEventListener('storage', (e) => {
|
|
106
|
+
if (e.key === 'theme' && e.newValue) {
|
|
107
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = e.newValue;
|
|
108
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
};
|
|
86
112
|
Darkify.storageKey = 'theme';
|
|
87
113
|
|
|
88
|
-
exports
|
|
114
|
+
module.exports = Darkify;
|
package/dist/darkify.d.ts
CHANGED
|
@@ -6,24 +6,25 @@ type Options = {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
declare class Darkify {
|
|
9
|
+
#private;
|
|
9
10
|
private static readonly storageKey;
|
|
10
|
-
options: Options;
|
|
11
|
-
theme: {
|
|
12
|
-
value: string;
|
|
13
|
-
};
|
|
14
|
-
cssTag: HTMLStyleElement;
|
|
15
|
-
metaTag: HTMLMetaElement;
|
|
11
|
+
readonly options: Options;
|
|
16
12
|
/**
|
|
17
13
|
* @param {string} element Button ID ( recommended ) or HTML element
|
|
18
14
|
* @param {object} options Options
|
|
19
15
|
* @see {@link https://github.com/emrocode/darkify-js/wiki|Documentation}
|
|
20
16
|
*/
|
|
21
17
|
constructor(element: string, options: Options);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Toggles the theme between light and dark modes
|
|
20
|
+
* @returns {void}
|
|
21
|
+
*/
|
|
22
|
+
toggleTheme(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the current active theme
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
getCurrentTheme(): string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export { Darkify as default };
|
package/dist/darkify.esm.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
3
|
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
-
* @version 1.1.
|
|
4
|
+
* @version 1.1.5
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
}
|
|
12
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
13
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
14
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
15
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
16
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
17
|
+
}
|
|
18
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
19
|
+
var e = new Error(message);
|
|
20
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
21
|
+
};
|
|
22
|
+
|
|
7
23
|
const isBrowser = typeof window !== 'undefined';
|
|
8
24
|
|
|
9
25
|
const defaultOptions = {
|
|
@@ -13,72 +29,84 @@ const defaultOptions = {
|
|
|
13
29
|
useColorScheme: ['#ffffff', '#000000'],
|
|
14
30
|
};
|
|
15
31
|
|
|
32
|
+
var _Darkify_instances, _a, _Darkify_theme, _Darkify_cssTag, _Darkify_metaTag, _Darkify_init, _Darkify_getOsPreference, _Darkify_createAttribute, _Darkify_updateTags, _Darkify_savePreference, _Darkify_syncThemeBetweenTabs;
|
|
16
33
|
class Darkify {
|
|
17
34
|
constructor(element, options) {
|
|
35
|
+
_Darkify_instances.add(this);
|
|
18
36
|
this.options = {};
|
|
19
|
-
|
|
37
|
+
_Darkify_theme.set(this, void 0);
|
|
38
|
+
_Darkify_cssTag.set(this, void 0);
|
|
39
|
+
_Darkify_metaTag.set(this, void 0);
|
|
40
|
+
if (!isBrowser)
|
|
20
41
|
return;
|
|
21
|
-
|
|
22
|
-
options
|
|
23
|
-
options
|
|
24
|
-
options = { ...defaultOptions, ...options };
|
|
42
|
+
(options === null || options === void 0 ? void 0 : options.useLocalStorage) && (options.useSessionStorage = false);
|
|
43
|
+
(options === null || options === void 0 ? void 0 : options.useSessionStorage) && (options.useLocalStorage = false);
|
|
44
|
+
options = Object.assign(Object.assign({}, defaultOptions), options);
|
|
25
45
|
this.options = options;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.matchMedia('(prefers-color-scheme: dark)')
|
|
33
|
-
.addEventListener('change', ({ matches: isDark }) => {
|
|
34
|
-
this.theme.value = isDark ? 'dark' : 'light';
|
|
35
|
-
this.savePreference();
|
|
36
|
-
});
|
|
37
|
-
this.theme = {
|
|
38
|
-
value: this.getOsPreference(options) ?? 'light',
|
|
39
|
-
};
|
|
40
|
-
this.cssTag = document.createElement('style');
|
|
41
|
-
this.metaTag = document.createElement('meta');
|
|
42
|
-
this.createAttribute();
|
|
46
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_init).call(this, element);
|
|
47
|
+
__classPrivateFieldSet(this, _Darkify_theme, { value: __classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_getOsPreference).call(this, options) }, "f");
|
|
48
|
+
__classPrivateFieldSet(this, _Darkify_cssTag, document.createElement('style'), "f");
|
|
49
|
+
__classPrivateFieldSet(this, _Darkify_metaTag, document.createElement('meta'), "f");
|
|
50
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
51
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_syncThemeBetweenTabs).call(this);
|
|
43
52
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey));
|
|
48
|
-
return (STO ||
|
|
49
|
-
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
50
|
-
? 'dark'
|
|
51
|
-
: 'light'));
|
|
53
|
+
toggleTheme() {
|
|
54
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = __classPrivateFieldGet(this, _Darkify_theme, "f").value === 'light' ? 'dark' : 'light';
|
|
55
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
52
56
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const { useColorScheme } = this.options;
|
|
56
|
-
let css = `/**! Darkify / A simple dark mode toggle library **/:root:is([data-theme="${this.theme.value}"]), [data-theme="${this.theme.value}"] {color-scheme: ${this.theme.value}}`;
|
|
57
|
-
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
58
|
-
this.updateTags(css, useColorScheme || []);
|
|
59
|
-
this.savePreference();
|
|
60
|
-
}
|
|
61
|
-
updateTags(css, useColorScheme) {
|
|
62
|
-
const head = document.head || document.getElementsByTagName('head')[0];
|
|
63
|
-
this.metaTag.setAttribute('name', 'theme-color');
|
|
64
|
-
this.metaTag.setAttribute('content', this.theme.value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
65
|
-
this.cssTag.setAttribute('type', 'text/css');
|
|
66
|
-
this.cssTag.innerHTML = css;
|
|
67
|
-
head.appendChild(this.metaTag);
|
|
68
|
-
head.appendChild(this.cssTag);
|
|
69
|
-
}
|
|
70
|
-
savePreference() {
|
|
71
|
-
const { useLocalStorage } = this.options;
|
|
72
|
-
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
73
|
-
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
74
|
-
OTS.removeItem(Darkify.storageKey);
|
|
75
|
-
STO.setItem(Darkify.storageKey, this.theme.value);
|
|
76
|
-
}
|
|
77
|
-
onClick() {
|
|
78
|
-
this.theme.value = this.theme.value === 'light' ? 'dark' : 'light';
|
|
79
|
-
this.createAttribute();
|
|
57
|
+
getCurrentTheme() {
|
|
58
|
+
return __classPrivateFieldGet(this, _Darkify_theme, "f").value;
|
|
80
59
|
}
|
|
81
60
|
}
|
|
61
|
+
_a = Darkify, _Darkify_theme = new WeakMap(), _Darkify_cssTag = new WeakMap(), _Darkify_metaTag = new WeakMap(), _Darkify_instances = new WeakSet(), _Darkify_init = function _Darkify_init(element) {
|
|
62
|
+
window
|
|
63
|
+
.matchMedia('(prefers-color-scheme: dark)')
|
|
64
|
+
.addEventListener('change', ({ matches: isDark }) => {
|
|
65
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = isDark ? 'dark' : 'light';
|
|
66
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_savePreference).call(this);
|
|
67
|
+
});
|
|
68
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
69
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
70
|
+
const htmlElement = document.querySelector(element);
|
|
71
|
+
htmlElement === null || htmlElement === void 0 ? void 0 : htmlElement.addEventListener('click', () => this.toggleTheme());
|
|
72
|
+
});
|
|
73
|
+
}, _Darkify_getOsPreference = function _Darkify_getOsPreference(options) {
|
|
74
|
+
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
75
|
+
const STO = (useLocalStorage && window.localStorage.getItem(_a.storageKey)) ||
|
|
76
|
+
(useSessionStorage && window.sessionStorage.getItem(_a.storageKey));
|
|
77
|
+
return (STO ||
|
|
78
|
+
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
79
|
+
? 'dark'
|
|
80
|
+
: 'light'));
|
|
81
|
+
}, _Darkify_createAttribute = function _Darkify_createAttribute() {
|
|
82
|
+
const dataTheme = document.getElementsByTagName('html')[0];
|
|
83
|
+
const { useColorScheme } = this.options;
|
|
84
|
+
let css = `/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${__classPrivateFieldGet(this, _Darkify_theme, "f").value}"]),[data-theme="${__classPrivateFieldGet(this, _Darkify_theme, "f").value}"]{color-scheme:${__classPrivateFieldGet(this, _Darkify_theme, "f").value}}`;
|
|
85
|
+
dataTheme.setAttribute('data-theme', __classPrivateFieldGet(this, _Darkify_theme, "f").value);
|
|
86
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_updateTags).call(this, css, useColorScheme || []);
|
|
87
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_savePreference).call(this);
|
|
88
|
+
}, _Darkify_updateTags = function _Darkify_updateTags(css, useColorScheme) {
|
|
89
|
+
const head = document.head || document.getElementsByTagName('head')[0];
|
|
90
|
+
__classPrivateFieldGet(this, _Darkify_metaTag, "f").setAttribute('name', 'theme-color');
|
|
91
|
+
__classPrivateFieldGet(this, _Darkify_metaTag, "f").setAttribute('content', __classPrivateFieldGet(this, _Darkify_theme, "f").value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
92
|
+
__classPrivateFieldGet(this, _Darkify_cssTag, "f").setAttribute('type', 'text/css');
|
|
93
|
+
__classPrivateFieldGet(this, _Darkify_cssTag, "f").innerHTML = css;
|
|
94
|
+
head.appendChild(__classPrivateFieldGet(this, _Darkify_metaTag, "f"));
|
|
95
|
+
head.appendChild(__classPrivateFieldGet(this, _Darkify_cssTag, "f"));
|
|
96
|
+
}, _Darkify_savePreference = function _Darkify_savePreference() {
|
|
97
|
+
const { useLocalStorage } = this.options;
|
|
98
|
+
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
99
|
+
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
100
|
+
OTS.removeItem(_a.storageKey);
|
|
101
|
+
STO.setItem(_a.storageKey, __classPrivateFieldGet(this, _Darkify_theme, "f").value);
|
|
102
|
+
}, _Darkify_syncThemeBetweenTabs = function _Darkify_syncThemeBetweenTabs() {
|
|
103
|
+
window.addEventListener('storage', (e) => {
|
|
104
|
+
if (e.key === 'theme' && e.newValue) {
|
|
105
|
+
__classPrivateFieldGet(this, _Darkify_theme, "f").value = e.newValue;
|
|
106
|
+
__classPrivateFieldGet(this, _Darkify_instances, "m", _Darkify_createAttribute).call(this);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
};
|
|
82
110
|
Darkify.storageKey = 'theme';
|
|
83
111
|
|
|
84
112
|
export { Darkify as default };
|
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.5
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
|
-
var Darkify=function(){"use strict";
|
|
7
|
+
var Darkify=function(){"use strict";function e(e,t,s,o){if("a"===s&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?o:"a"===s?o.call(e):o?o.value:t.get(e)}function t(e,t,s,o,a){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?a.call(e,s):a?a.value=s:t.set(e,s),s}"function"==typeof SuppressedError&&SuppressedError;const s="undefined"!=typeof window,o={autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColorScheme:["#ffffff","#000000"]};var a,i,r,n,h,l,c,u,d,m,f;class g{constructor(i,d){a.add(this),this.options={},r.set(this,void 0),n.set(this,void 0),h.set(this,void 0),s&&((null==d?void 0:d.useLocalStorage)&&(d.useSessionStorage=!1),(null==d?void 0:d.useSessionStorage)&&(d.useLocalStorage=!1),d=Object.assign(Object.assign({},o),d),this.options=d,e(this,a,"m",l).call(this,i),t(this,r,{value:e(this,a,"m",c).call(this,d)},"f"),t(this,n,document.createElement("style"),"f"),t(this,h,document.createElement("meta"),"f"),e(this,a,"m",u).call(this),e(this,a,"m",f).call(this))}toggleTheme(){e(this,r,"f").value="light"===e(this,r,"f").value?"dark":"light",e(this,a,"m",u).call(this)}getCurrentTheme(){return e(this,r,"f").value}}return i=g,r=new WeakMap,n=new WeakMap,h=new WeakMap,a=new WeakSet,l=function(t){window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(({matches:t})=>{e(this,r,"f").value=t?"dark":"light",e(this,a,"m",m).call(this)})),document.addEventListener("DOMContentLoaded",(()=>{e(this,a,"m",u).call(this);const s=document.querySelector(t);null==s||s.addEventListener("click",(()=>this.toggleTheme()))}))},c=function(e){const{autoMatchTheme:t,useLocalStorage:s,useSessionStorage:o}=e;return s&&window.localStorage.getItem(i.storageKey)||o&&window.sessionStorage.getItem(i.storageKey)||(t&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light")},u=function(){const t=document.getElementsByTagName("html")[0],{useColorScheme:s}=this.options;let o=`/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${e(this,r,"f").value}"]),[data-theme="${e(this,r,"f").value}"]{color-scheme:${e(this,r,"f").value}}`;t.setAttribute("data-theme",e(this,r,"f").value),e(this,a,"m",d).call(this,o,s||[]),e(this,a,"m",m).call(this)},d=function(t,s){const o=document.head||document.getElementsByTagName("head")[0];e(this,h,"f").setAttribute("name","theme-color"),e(this,h,"f").setAttribute("content","light"===e(this,r,"f").value?s[0]:s[1]),e(this,n,"f").setAttribute("type","text/css"),e(this,n,"f").innerHTML=t,o.appendChild(e(this,h,"f")),o.appendChild(e(this,n,"f"))},m=function(){const{useLocalStorage:t}=this.options,s=t?window.localStorage:window.sessionStorage;(t?window.sessionStorage:window.localStorage).removeItem(i.storageKey),s.setItem(i.storageKey,e(this,r,"f").value)},f=function(){window.addEventListener("storage",(t=>{"theme"===t.key&&t.newValue&&(e(this,r,"f").value=t.newValue,e(this,a,"m",u).call(this))}))},g.storageKey="theme",g}();
|
package/dist/darkify.umd.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.5
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Darkify=t()}(this,(function(){"use strict";
|
|
7
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Darkify=t()}(this,(function(){"use strict";function e(e,t,o,s){if("a"===o&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?s:"a"===o?s.call(e):s?s.value:t.get(e)}function t(e,t,o,s,i){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?i.call(e,o):i?i.value=o:t.set(e,o),o}"function"==typeof SuppressedError&&SuppressedError;const o="undefined"!=typeof window,s={autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColorScheme:["#ffffff","#000000"]};var i,a,n,r,h,l,c,d,u,f,m;class g{constructor(a,u){i.add(this),this.options={},n.set(this,void 0),r.set(this,void 0),h.set(this,void 0),o&&((null==u?void 0:u.useLocalStorage)&&(u.useSessionStorage=!1),(null==u?void 0:u.useSessionStorage)&&(u.useLocalStorage=!1),u=Object.assign(Object.assign({},s),u),this.options=u,e(this,i,"m",l).call(this,a),t(this,n,{value:e(this,i,"m",c).call(this,u)},"f"),t(this,r,document.createElement("style"),"f"),t(this,h,document.createElement("meta"),"f"),e(this,i,"m",d).call(this),e(this,i,"m",m).call(this))}toggleTheme(){e(this,n,"f").value="light"===e(this,n,"f").value?"dark":"light",e(this,i,"m",d).call(this)}getCurrentTheme(){return e(this,n,"f").value}}return a=g,n=new WeakMap,r=new WeakMap,h=new WeakMap,i=new WeakSet,l=function(t){window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(({matches:t})=>{e(this,n,"f").value=t?"dark":"light",e(this,i,"m",f).call(this)})),document.addEventListener("DOMContentLoaded",(()=>{e(this,i,"m",d).call(this);const o=document.querySelector(t);null==o||o.addEventListener("click",(()=>this.toggleTheme()))}))},c=function(e){const{autoMatchTheme:t,useLocalStorage:o,useSessionStorage:s}=e;return o&&window.localStorage.getItem(a.storageKey)||s&&window.sessionStorage.getItem(a.storageKey)||(t&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light")},d=function(){const t=document.getElementsByTagName("html")[0],{useColorScheme:o}=this.options;let s=`/**! Darkify / A simple dark mode toggle library **/\n:root:is([data-theme="${e(this,n,"f").value}"]),[data-theme="${e(this,n,"f").value}"]{color-scheme:${e(this,n,"f").value}}`;t.setAttribute("data-theme",e(this,n,"f").value),e(this,i,"m",u).call(this,s,o||[]),e(this,i,"m",f).call(this)},u=function(t,o){const s=document.head||document.getElementsByTagName("head")[0];e(this,h,"f").setAttribute("name","theme-color"),e(this,h,"f").setAttribute("content","light"===e(this,n,"f").value?o[0]:o[1]),e(this,r,"f").setAttribute("type","text/css"),e(this,r,"f").innerHTML=t,s.appendChild(e(this,h,"f")),s.appendChild(e(this,r,"f"))},f=function(){const{useLocalStorage:t}=this.options,o=t?window.localStorage:window.sessionStorage;(t?window.sessionStorage:window.localStorage).removeItem(a.storageKey),o.setItem(a.storageKey,e(this,n,"f").value)},m=function(){window.addEventListener("storage",(t=>{"theme"===t.key&&t.newValue&&(e(this,n,"f").value=t.newValue,e(this,i,"m",d).call(this))}))},g.storageKey="theme",g}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "darkify-js",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "A simple dark mode toggle library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/darkify.cjs.js",
|
|
@@ -16,34 +16,39 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"_cls": "rm -rf dist",
|
|
18
18
|
"_bundle": "rollup -c rollup.config.ts --configPlugin typescript",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"build": "npm run _cls && npm run _bundle",
|
|
20
|
+
"format": "prettier --write src/",
|
|
21
|
+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest"
|
|
21
22
|
},
|
|
22
|
-
"keywords": [
|
|
23
|
-
"color-scheme",
|
|
24
|
-
"toggle",
|
|
25
|
-
"dark-mode",
|
|
26
|
-
"dark-theme"
|
|
27
|
-
],
|
|
28
|
-
"author": "Emilio Romero <emrocode@gmail.com>",
|
|
29
23
|
"repository": {
|
|
30
24
|
"type": "git",
|
|
31
25
|
"url": "git+https://github.com/emrocode/darkify-js"
|
|
32
26
|
},
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
27
|
+
"keywords": [
|
|
28
|
+
"dark mode",
|
|
29
|
+
"light mode",
|
|
30
|
+
"theme toggle",
|
|
31
|
+
"dark theme",
|
|
32
|
+
"color scheme",
|
|
33
|
+
"theme switcher",
|
|
34
|
+
"night mode",
|
|
35
|
+
"automatic theme"
|
|
36
|
+
],
|
|
37
|
+
"author": "Emilio Romero <emrocode@gmail.com>",
|
|
37
38
|
"license": "MIT",
|
|
38
39
|
"devDependencies": {
|
|
39
|
-
"@rollup/plugin-commonjs": "^25.0.8",
|
|
40
40
|
"@rollup/plugin-terser": "^0.4.4",
|
|
41
|
-
"@rollup/plugin-typescript": "^
|
|
42
|
-
"
|
|
43
|
-
"
|
|
41
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
42
|
+
"@types/jest": "^29.5.14",
|
|
43
|
+
"jest": "^29.7.0",
|
|
44
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
45
|
+
"prettier": "^3.4.2",
|
|
46
|
+
"rollup": "^4.34.1",
|
|
44
47
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
45
48
|
"rollup-plugin-dts": "^6.1.1",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
49
|
+
"ts-jest": "^29.2.5",
|
|
50
|
+
"ts-node": "^10.9.2",
|
|
51
|
+
"tslib": "^2.8.1",
|
|
52
|
+
"typescript": "^5.7.3"
|
|
48
53
|
}
|
|
49
54
|
}
|