@thiagormoreira/nightwind 1.1.13
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/.github/FUNDING.yml +3 -0
- package/CHANGELOG.md +194 -0
- package/LICENSE +21 -0
- package/README.md +715 -0
- package/helper.js +100 -0
- package/package.json +32 -0
- package/src/index.js +749 -0
package/helper.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
init: () => {
|
|
3
|
+
const codeToRunOnClient = `
|
|
4
|
+
(function() {
|
|
5
|
+
function getInitialColorMode() {
|
|
6
|
+
const persistedColorPreference = window.localStorage.getItem('nightwind-mode');
|
|
7
|
+
const hasPersistedPreference = typeof persistedColorPreference === 'string';
|
|
8
|
+
if (hasPersistedPreference) {
|
|
9
|
+
return persistedColorPreference;
|
|
10
|
+
}
|
|
11
|
+
const mql = window.matchMedia('(prefers-color-scheme: dark)');
|
|
12
|
+
const hasMediaQueryPreference = typeof mql.matches === 'boolean';
|
|
13
|
+
if (hasMediaQueryPreference) {
|
|
14
|
+
return mql.matches ? 'dark' : 'light';
|
|
15
|
+
}
|
|
16
|
+
return 'light';
|
|
17
|
+
}
|
|
18
|
+
getInitialColorMode() == 'light' ? document.documentElement.classList.remove('dark') : document.documentElement.classList.add('dark');
|
|
19
|
+
})()
|
|
20
|
+
`;
|
|
21
|
+
return codeToRunOnClient;
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
beforeTransition: () => {
|
|
25
|
+
const doc = document.documentElement;
|
|
26
|
+
const onTransitionDone = () => {
|
|
27
|
+
doc.classList.remove('nightwind');
|
|
28
|
+
doc.removeEventListener('transitionend', onTransitionDone);
|
|
29
|
+
}
|
|
30
|
+
doc.addEventListener('transitionend', onTransitionDone);
|
|
31
|
+
if (!doc.classList.contains('nightwind')) {
|
|
32
|
+
doc.classList.add('nightwind');
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
toggle: () => {
|
|
37
|
+
module.exports.beforeTransition();
|
|
38
|
+
if (!document.documentElement.classList.contains('dark')) {
|
|
39
|
+
document.documentElement.classList.add('dark');
|
|
40
|
+
window.localStorage.setItem('nightwind-mode', 'dark');
|
|
41
|
+
} else {
|
|
42
|
+
document.documentElement.classList.remove('dark');
|
|
43
|
+
window.localStorage.setItem('nightwind-mode', 'light');
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
enable: (dark) => {
|
|
48
|
+
const mode = dark ? "dark" : "light";
|
|
49
|
+
const opposite = dark ? "light" : "dark";
|
|
50
|
+
|
|
51
|
+
module.exports.beforeTransition();
|
|
52
|
+
|
|
53
|
+
if (document.documentElement.classList.contains(opposite)) {
|
|
54
|
+
document.documentElement.classList.remove(opposite);
|
|
55
|
+
}
|
|
56
|
+
document.documentElement.classList.add(mode);
|
|
57
|
+
window.localStorage.setItem('nightwind-mode', mode);
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
// Old
|
|
61
|
+
|
|
62
|
+
checkNightMode: () => {
|
|
63
|
+
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
watchNightMode: () => {
|
|
67
|
+
if (!window.matchMedia) return;
|
|
68
|
+
window.matchMedia('(prefers-color-scheme: dark)').addListener(module.exports.addNightModeSelector());
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
addNightModeSelector: () => {
|
|
72
|
+
if (module.exports.checkNightMode()) {
|
|
73
|
+
document.documentElement.classList.add('dark');
|
|
74
|
+
} else {
|
|
75
|
+
document.documentElement.classList.remove('dark');
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
addNightTransitions: () => {
|
|
80
|
+
if (!document.documentElement.classList.contains('nightwind')) {
|
|
81
|
+
document.documentElement.classList.add('nightwind');
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
initNightwind: () => {
|
|
86
|
+
module.exports.watchNightMode();
|
|
87
|
+
module.exports.addNightModeSelector();
|
|
88
|
+
module.exports.addNightTransitions();
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
toggleNightMode: () => {
|
|
92
|
+
if (!document.documentElement.classList.contains('dark')) {
|
|
93
|
+
document.documentElement.classList.add('dark');
|
|
94
|
+
window.localStorage.setItem('nightwind-mode', 'dark');
|
|
95
|
+
} else {
|
|
96
|
+
document.documentElement.classList.remove('dark');
|
|
97
|
+
window.localStorage.setItem('nightwind-mode', 'light');
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thiagormoreira/nightwind",
|
|
3
|
+
"version": "1.1.13",
|
|
4
|
+
"description": "An automatic, overridable, customisable Tailwind dark mode plugin",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/thiagormoreira/nightwind.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"tailwind",
|
|
12
|
+
"tailwindcss",
|
|
13
|
+
"tailwindcss-plugin",
|
|
14
|
+
"dark-mode",
|
|
15
|
+
"dark-theme",
|
|
16
|
+
"postcss",
|
|
17
|
+
"css",
|
|
18
|
+
"javascript"
|
|
19
|
+
],
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"tailwindcss": "^2.0.0 || ^3.0.0 || ^4.0.0"
|
|
22
|
+
},
|
|
23
|
+
"author": "Thiago Moreira",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/thiagormoreira/nightwind/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/thiagormoreira/nightwind#readme",
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
31
|
+
}
|
|
32
|
+
}
|