-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
206 lines (193 loc) · 6.59 KB
/
main.js
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @package @wcj/dark-mode
* Web Component that toggles dark mode 🌒
* Github: https://github.com/jaywcjlove/dark-mode.git
* Website: https://jaywcjlove.github.io/dark-mode
*
* Licensed under the MIT license.
* @license Copyright © 2022. Licensed under the MIT License
* @author kenny wong <[email protected]>
*/
const doc = document;
const LOCAL_NANE = '_dark_mode_theme_'
const PERMANENT = 'permanent';
const COLOR_SCHEME_CHANGE = 'colorschemechange';
const PERMANENT_COLOR_SCHEME = 'permanentcolorscheme';
const LIGHT = 'light';
const DARK = 'dark';
// See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html ↵
// #reflecting-content-attributes-in-idl-attributes.
const installStringReflection = (obj, attrName, propName = attrName) => {
Object.defineProperty(obj, propName, {
enumerable: true,
get() {
const value = this.getAttribute(attrName);
return value === null ? '' : value;
},
set(v) {
this.setAttribute(attrName, v);
},
});
};
const installBoolReflection = (obj, attrName, propName = attrName) => {
Object.defineProperty(obj, propName, {
enumerable: true,
get() {
return this.hasAttribute(attrName);
},
set(v) {
if (v) {
this.setAttribute(attrName, '');
} else {
this.removeAttribute(attrName);
}
},
});
};
class DarkMode extends HTMLElement {
static get observedAttributes() {
return ['mode', LIGHT, DARK, PERMANENT];
}
LOCAL_NANE = LOCAL_NANE;
constructor() {
super();
this._initializeDOM();
}
connectedCallback() {
installStringReflection(this, 'mode');
installStringReflection(this, DARK);
installStringReflection(this, LIGHT);
installBoolReflection(this, PERMANENT);
const rememberedValue = localStorage.getItem(LOCAL_NANE);
if (rememberedValue && [LIGHT, DARK].includes(rememberedValue)) {
this.mode = rememberedValue;
this.permanent = true;
}
if (this.permanent && !rememberedValue) {
localStorage.setItem(LOCAL_NANE, this.mode);
}
const hasNativePrefersColorScheme = [LIGHT, DARK].includes(rememberedValue);
if (this.permanent && rememberedValue) {
this._changeThemeTag();
} else {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
this.mode = DARK;
this._changeThemeTag();
}
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
this.mode = LIGHT;
this._changeThemeTag();
}
}
if (!this.permanent && !hasNativePrefersColorScheme) {
window.matchMedia('(prefers-color-scheme: light)').onchange = (event) => {
this.mode = event.matches ? LIGHT : DARK;
this._changeThemeTag();
}
window.matchMedia('(prefers-color-scheme: dark)').onchange = (event) => {
this.mode = event.matches ? DARK : LIGHT;
this._changeThemeTag();
}
}
const observer = new MutationObserver((mutationsList, observer) => {
this.mode = doc.documentElement.dataset.colorMode;
if (this.permanent && hasNativePrefersColorScheme) {
localStorage.setItem(LOCAL_NANE, this.mode);
this._dispatchEvent(PERMANENT_COLOR_SCHEME, {
permanent: this.permanent,
});
}
this._changeContent();
this._dispatchEvent(COLOR_SCHEME_CHANGE, { colorScheme: this.mode });
});
// Start observing the target node with the above configuration
observer.observe(doc.documentElement, { attributes: true });
// After that, stop observing
// observer.disconnect();
this._dispatchEvent(COLOR_SCHEME_CHANGE, { colorScheme: this.mode });
this._changeContent();
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'mode' && oldValue !== newValue && [LIGHT, DARK].includes(newValue)) {
const rememberedValue = localStorage.getItem(LOCAL_NANE);
if (this.mode === rememberedValue) {
this.mode = newValue;
this._changeContent();
this._changeThemeTag();
} else if (this.mode && this.mode !== rememberedValue) {
this._changeContent();
this._changeThemeTag();
}
} else if ((name === LIGHT || name === DARK) && oldValue !== newValue) {
this._changeContent();
}
if (name === 'permanent' && typeof this.permanent === 'boolean') {
this.permanent ? localStorage.setItem(LOCAL_NANE, this.mode) : localStorage.removeItem(LOCAL_NANE);
}
}
_changeThemeTag() {
doc.documentElement.setAttribute('data-color-mode', this.mode);
}
_changeContent() {
this.icon.textContent = this.mode === LIGHT ? '🌒' : '🌞';
this.text.textContent = this.mode === LIGHT ? this.getAttribute(DARK) : this.getAttribute(LIGHT);
if (!this.text.textContent && this.text.parentElement && this.text) {
this.text.parentElement.removeChild(this.text)
}
}
_initializeDOM() {
var shadow = this.attachShadow({ mode: 'open' });
this.label = doc.createElement('span');
this.label.setAttribute('class', 'wrapper');
this.label.onclick = () => {
this.mode = this.mode === LIGHT ? DARK : LIGHT;
if (this.permanent) {
localStorage.setItem(LOCAL_NANE, this.mode);
}
this._changeThemeTag();
this._changeContent();
}
shadow.appendChild(this.label);
this.icon = doc.createElement('span');
this.label.appendChild(this.icon);
this.text = doc.createElement('span');
this.label.appendChild(this.text);
const textContent = `
[data-color-mode*='dark'], [data-color-mode*='dark'] body {
color-scheme: dark;
--color-theme-bg: #0d1117;
--color-theme-text: #c9d1d9;
background-color: var(--color-theme-bg);
color: var(--color-theme-text);
}
[data-color-mode*='light'], [data-color-mode*='light'] body {
color-scheme: light;
--color-theme-bg: #fff;
--color-theme-text: #24292f;
background-color: var(--color-theme-bg);
color: var(--color-theme-text);
}`;
const STYLE_ID = '_dark_mode_style_';
const styleDom = doc.getElementById(STYLE_ID);
if (!styleDom) {
var initstyle = doc.createElement('style');
initstyle.id = STYLE_ID;
initstyle.textContent = textContent;
doc.head.appendChild(initstyle);
}
var style = doc.createElement('style');
style.textContent = `
.wrapper { cursor: pointer; user-select: none; position: relative; }
.wrapper > span + span { margin-left: .4rem; }
`;
shadow.appendChild(style);
}
_dispatchEvent(type, value) {
this.dispatchEvent(new CustomEvent(type, {
bubbles: true,
composed: true,
detail: value,
}));
}
}
customElements.define('dark-mode', DarkMode);