darkify-js 1.1.2 → 1.1.4
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 -21
- package/README.md +34 -28
- package/dist/darkify.cjs.js +88 -0
- package/dist/darkify.d.ts +12 -9
- package/dist/darkify.esm.js +84 -0
- package/dist/darkify.min.js +2 -2
- package/dist/darkify.umd.js +7 -0
- package/package.json +49 -46
- package/dist/darkify.js +0 -95
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present 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
CHANGED
|
@@ -1,28 +1,34 @@
|
|
|
1
|
-
# Darkify
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
# Darkify JS
|
|
2
|
+
|
|
3
|
+
🌚 A simple dark mode toggle library that makes it easy to implement dark mode on your website without additional configuration
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> Please make sure to read the [Wiki] for detailed documentation and examples
|
|
7
|
+
|
|
8
|
+
### 📦 Installation
|
|
9
|
+
|
|
10
|
+
Use npm or any other package manager:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install darkify-js
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### ⚙️ Setup
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
// main.js
|
|
20
|
+
import Darkify from 'darkify-js';
|
|
21
|
+
|
|
22
|
+
const options = {
|
|
23
|
+
autoMatchTheme: true,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// autoMatchTheme: default is true
|
|
27
|
+
// useLocalStorage: default is true
|
|
28
|
+
// useSessionStorage: default is false
|
|
29
|
+
// useColorScheme: default is ['#ffffff', '#000000']
|
|
30
|
+
|
|
31
|
+
new Darkify('#element', options);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
[Wiki]: https://github.com/emrocode/darkify-js/wiki
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
+
* @version 1.1.4
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
10
|
+
|
|
11
|
+
const isBrowser = typeof window !== 'undefined';
|
|
12
|
+
|
|
13
|
+
const defaultOptions = {
|
|
14
|
+
autoMatchTheme: true,
|
|
15
|
+
useLocalStorage: true,
|
|
16
|
+
useSessionStorage: false,
|
|
17
|
+
useColorScheme: ['#ffffff', '#000000'],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
class Darkify {
|
|
21
|
+
constructor(element, options) {
|
|
22
|
+
this.options = {};
|
|
23
|
+
if (!isBrowser) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
options?.useLocalStorage && (options.useSessionStorage = false);
|
|
27
|
+
options?.useSessionStorage && (options.useLocalStorage = false);
|
|
28
|
+
options = { ...defaultOptions, ...options };
|
|
29
|
+
this.options = options;
|
|
30
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
31
|
+
this.createAttribute();
|
|
32
|
+
const htmlElement = document.querySelector(element);
|
|
33
|
+
htmlElement?.addEventListener('click', () => this.onClick());
|
|
34
|
+
});
|
|
35
|
+
window
|
|
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'));
|
|
56
|
+
}
|
|
57
|
+
createAttribute() {
|
|
58
|
+
const dataTheme = document.getElementsByTagName('html')[0];
|
|
59
|
+
const { useColorScheme } = this.options;
|
|
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();
|
|
64
|
+
}
|
|
65
|
+
updateTags(css, useColorScheme) {
|
|
66
|
+
const head = document.head || document.getElementsByTagName('head')[0];
|
|
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();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
Darkify.storageKey = 'theme';
|
|
87
|
+
|
|
88
|
+
exports.default = Darkify;
|
package/dist/darkify.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
type Options = {
|
|
2
|
-
autoMatchTheme?: boolean;
|
|
3
|
-
useLocalStorage?: boolean;
|
|
4
|
-
useSessionStorage?: boolean;
|
|
5
|
-
useColorScheme?: string[];
|
|
1
|
+
type Options = {
|
|
2
|
+
autoMatchTheme?: boolean;
|
|
3
|
+
useLocalStorage?: boolean;
|
|
4
|
+
useSessionStorage?: boolean;
|
|
5
|
+
useColorScheme?: string[];
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
declare class Darkify {
|
|
9
9
|
private static readonly storageKey;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
options: Options;
|
|
11
|
+
theme: {
|
|
12
|
+
value: string;
|
|
13
|
+
};
|
|
14
|
+
cssTag: HTMLStyleElement;
|
|
15
|
+
metaTag: HTMLMetaElement;
|
|
14
16
|
/**
|
|
15
17
|
* @param {string} element Button ID ( recommended ) or HTML element
|
|
16
18
|
* @param {object} options Options
|
|
19
|
+
* @see {@link https://github.com/emrocode/darkify-js/wiki|Documentation}
|
|
17
20
|
*/
|
|
18
21
|
constructor(element: string, options: Options);
|
|
19
22
|
getOsPreference(options: Options): string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
+
* @version 1.1.4
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
const isBrowser = typeof window !== 'undefined';
|
|
8
|
+
|
|
9
|
+
const defaultOptions = {
|
|
10
|
+
autoMatchTheme: true,
|
|
11
|
+
useLocalStorage: true,
|
|
12
|
+
useSessionStorage: false,
|
|
13
|
+
useColorScheme: ['#ffffff', '#000000'],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
class Darkify {
|
|
17
|
+
constructor(element, options) {
|
|
18
|
+
this.options = {};
|
|
19
|
+
if (!isBrowser) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
options?.useLocalStorage && (options.useSessionStorage = false);
|
|
23
|
+
options?.useSessionStorage && (options.useLocalStorage = false);
|
|
24
|
+
options = { ...defaultOptions, ...options };
|
|
25
|
+
this.options = options;
|
|
26
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
27
|
+
this.createAttribute();
|
|
28
|
+
const htmlElement = document.querySelector(element);
|
|
29
|
+
htmlElement?.addEventListener('click', () => this.onClick());
|
|
30
|
+
});
|
|
31
|
+
window
|
|
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();
|
|
43
|
+
}
|
|
44
|
+
getOsPreference(options) {
|
|
45
|
+
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
46
|
+
const STO = (useLocalStorage && window.localStorage.getItem(Darkify.storageKey)) ||
|
|
47
|
+
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey));
|
|
48
|
+
return (STO ||
|
|
49
|
+
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
50
|
+
? 'dark'
|
|
51
|
+
: 'light'));
|
|
52
|
+
}
|
|
53
|
+
createAttribute() {
|
|
54
|
+
const dataTheme = document.getElementsByTagName('html')[0];
|
|
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();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
Darkify.storageKey = 'theme';
|
|
83
|
+
|
|
84
|
+
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.4
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
|
-
var Darkify=function(){"use strict";const e="undefined"!=typeof window;class
|
|
7
|
+
var Darkify=function(){"use strict";const e="undefined"!=typeof window,t={autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColorScheme:["#ffffff","#000000"]};class s{constructor(s,a){this.options={},e&&(a?.useLocalStorage&&(a.useSessionStorage=!1),a?.useSessionStorage&&(a.useLocalStorage=!1),a={...t,...a},this.options=a,document.addEventListener("DOMContentLoaded",(()=>{this.createAttribute();const e=document.querySelector(s);e?.addEventListener("click",(()=>this.onClick()))})),window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(({matches:e})=>{this.theme.value=e?"dark":"light",this.savePreference()})),this.theme={value:this.getOsPreference(a)??"light"},this.cssTag=document.createElement("style"),this.metaTag=document.createElement("meta"),this.createAttribute())}getOsPreference(e){const{autoMatchTheme:t,useLocalStorage:a,useSessionStorage:o}=e;return a&&window.localStorage.getItem(s.storageKey)||o&&window.sessionStorage.getItem(s.storageKey)||(t&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light")}createAttribute(){const e=document.getElementsByTagName("html")[0],{useColorScheme:t}=this.options;let s=`/**! Darkify / A simple dark mode toggle library **/: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){const s=document.head||document.getElementsByTagName("head")[0];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,t=e?window.localStorage:window.sessionStorage;(e?window.sessionStorage:window.localStorage).removeItem(s.storageKey),t.setItem(s.storageKey,this.theme.value)}onClick(){this.theme.value="light"===this.theme.value?"dark":"light",this.createAttribute()}}return s.storageKey="theme",s}();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
+
* @version 1.1.4
|
|
5
|
+
* @license MIT
|
|
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";const e="undefined"!=typeof window,t={autoMatchTheme:!0,useLocalStorage:!0,useSessionStorage:!1,useColorScheme:["#ffffff","#000000"]};class s{constructor(s,a){this.options={},e&&(a?.useLocalStorage&&(a.useSessionStorage=!1),a?.useSessionStorage&&(a.useLocalStorage=!1),a={...t,...a},this.options=a,document.addEventListener("DOMContentLoaded",(()=>{this.createAttribute();const e=document.querySelector(s);e?.addEventListener("click",(()=>this.onClick()))})),window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",(({matches:e})=>{this.theme.value=e?"dark":"light",this.savePreference()})),this.theme={value:this.getOsPreference(a)??"light"},this.cssTag=document.createElement("style"),this.metaTag=document.createElement("meta"),this.createAttribute())}getOsPreference(e){const{autoMatchTheme:t,useLocalStorage:a,useSessionStorage:o}=e;return a&&window.localStorage.getItem(s.storageKey)||o&&window.sessionStorage.getItem(s.storageKey)||(t&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light")}createAttribute(){const e=document.getElementsByTagName("html")[0],{useColorScheme:t}=this.options;let s=`/**! Darkify / A simple dark mode toggle library **/: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){const s=document.head||document.getElementsByTagName("head")[0];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,t=e?window.localStorage:window.sessionStorage;(e?window.sessionStorage:window.localStorage).removeItem(s.storageKey),t.setItem(s.storageKey,this.theme.value)}onClick(){this.theme.value="light"===this.theme.value?"dark":"light",this.createAttribute()}}return s.storageKey="theme",s}));
|
package/package.json
CHANGED
|
@@ -1,46 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "darkify-js",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/darkify.js",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
"dist/darkify.
|
|
11
|
-
"dist/darkify.
|
|
12
|
-
"dist/darkify.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
"rollup": "^4.
|
|
41
|
-
"rollup
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "darkify-js",
|
|
3
|
+
"version": "1.1.4",
|
|
4
|
+
"description": "A simple dark mode toggle library",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/darkify.cjs.js",
|
|
7
|
+
"module": "dist/darkify.esm.js",
|
|
8
|
+
"types": "dist/darkify.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/darkify.d.ts",
|
|
11
|
+
"dist/darkify.cjs.js",
|
|
12
|
+
"dist/darkify.esm.js",
|
|
13
|
+
"dist/darkify.umd.js",
|
|
14
|
+
"dist/darkify.min.js"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"_cls": "rm -rf dist",
|
|
18
|
+
"_bundle": "rollup -c rollup.config.ts --configPlugin typescript",
|
|
19
|
+
"prettier": "prettier --write src/",
|
|
20
|
+
"build": "npm run _cls && npm run _bundle"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"color-scheme",
|
|
24
|
+
"toggle",
|
|
25
|
+
"dark-mode",
|
|
26
|
+
"dark-theme"
|
|
27
|
+
],
|
|
28
|
+
"author": "Emilio Romero <emrocode@gmail.com>",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/emrocode/darkify-js"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/emrocode/darkify-js#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/emrocode/darkify-js/issues"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@rollup/plugin-commonjs": "^25.0.8",
|
|
40
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
41
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
42
|
+
"prettier": "^3.2.5",
|
|
43
|
+
"rollup": "^4.18.0",
|
|
44
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
45
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
46
|
+
"tslib": "^2.6.2",
|
|
47
|
+
"typescript": "^5.4.5"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/dist/darkify.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* @author Emilio Romero <emrocode@gmail.com>
|
|
4
|
-
* @version 1.1.2
|
|
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
|
-
constructor(element, options) {
|
|
16
|
-
this.options = {};
|
|
17
|
-
if (!isBrowser) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
if (options?.useLocalStorage) {
|
|
21
|
-
options.useSessionStorage = false;
|
|
22
|
-
}
|
|
23
|
-
else if (options?.useSessionStorage) {
|
|
24
|
-
options.useLocalStorage = false;
|
|
25
|
-
}
|
|
26
|
-
const defaultOptions = {
|
|
27
|
-
autoMatchTheme: true,
|
|
28
|
-
useLocalStorage: true,
|
|
29
|
-
useSessionStorage: false,
|
|
30
|
-
useColorScheme: ['#ffffff', '#000000'],
|
|
31
|
-
};
|
|
32
|
-
options = { ...defaultOptions, ...options };
|
|
33
|
-
this.options = options;
|
|
34
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
35
|
-
this.createAttribute();
|
|
36
|
-
const htmlElement = document.querySelector(element);
|
|
37
|
-
htmlElement?.addEventListener('click', () => this.onClick());
|
|
38
|
-
});
|
|
39
|
-
window
|
|
40
|
-
.matchMedia('(prefers-color-scheme: dark)')
|
|
41
|
-
.addEventListener('change', ({ matches: isDark }) => {
|
|
42
|
-
this.theme.value = isDark ? 'dark' : 'light';
|
|
43
|
-
this.savePreference();
|
|
44
|
-
return window.location.reload();
|
|
45
|
-
});
|
|
46
|
-
this.theme = {
|
|
47
|
-
value: this.getOsPreference(options) ?? 'light',
|
|
48
|
-
};
|
|
49
|
-
this.cssTag = document.createElement('style');
|
|
50
|
-
this.metaTag = document.createElement('meta');
|
|
51
|
-
this.createAttribute();
|
|
52
|
-
}
|
|
53
|
-
getOsPreference(options) {
|
|
54
|
-
const { autoMatchTheme, useLocalStorage, useSessionStorage } = options;
|
|
55
|
-
const STO = (useLocalStorage && window.localStorage.getItem(Darkify.storageKey)) ||
|
|
56
|
-
(useSessionStorage && window.sessionStorage.getItem(Darkify.storageKey));
|
|
57
|
-
return (STO ||
|
|
58
|
-
(autoMatchTheme && window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
59
|
-
? 'dark'
|
|
60
|
-
: 'light'));
|
|
61
|
-
}
|
|
62
|
-
createAttribute() {
|
|
63
|
-
const dataTheme = document.getElementsByTagName('html')[0];
|
|
64
|
-
const { useColorScheme } = this.options;
|
|
65
|
-
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}}`;
|
|
66
|
-
dataTheme.setAttribute('data-theme', this.theme.value);
|
|
67
|
-
this.updateTags(css, useColorScheme || []);
|
|
68
|
-
this.savePreference();
|
|
69
|
-
}
|
|
70
|
-
updateTags(css, useColorScheme) {
|
|
71
|
-
const head = document.head;
|
|
72
|
-
this.metaTag.setAttribute('name', 'theme-color');
|
|
73
|
-
this.metaTag.setAttribute('content', this.theme.value === 'light' ? useColorScheme[0] : useColorScheme[1]);
|
|
74
|
-
this.cssTag.setAttribute('type', 'text/css');
|
|
75
|
-
this.cssTag.innerHTML = css;
|
|
76
|
-
head.appendChild(this.metaTag);
|
|
77
|
-
head.appendChild(this.cssTag);
|
|
78
|
-
}
|
|
79
|
-
savePreference() {
|
|
80
|
-
const { useLocalStorage } = this.options;
|
|
81
|
-
const STO = useLocalStorage ? window.localStorage : window.sessionStorage;
|
|
82
|
-
const OTS = useLocalStorage ? window.sessionStorage : window.localStorage;
|
|
83
|
-
OTS.removeItem(Darkify.storageKey);
|
|
84
|
-
STO.setItem(Darkify.storageKey, this.theme.value);
|
|
85
|
-
}
|
|
86
|
-
onClick() {
|
|
87
|
-
this.theme.value = this.theme.value === 'light' ? 'dark' : 'light';
|
|
88
|
-
this.createAttribute();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
Darkify.storageKey = 'darkify-theme';
|
|
92
|
-
|
|
93
|
-
return Darkify;
|
|
94
|
-
|
|
95
|
-
}));
|