-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdarkmode.js
102 lines (94 loc) · 2.55 KB
/
darkmode.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
const plugin = (hook, vm) => {
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition');
}, 800);
};
let setColor = ({
background,
toggleBtnBg,
textColor,
codeBackground,
codeFontColor,
header,
}) => {
document.documentElement.style.setProperty(
'--docsify_dark_mode_bg',
background
);
document.documentElement.style.setProperty(
'--docsify_dark_mode_btn',
toggleBtnBg
);
document.documentElement.style.setProperty(
'--docsify_dark_mode_code',
codeBackground
);
document.documentElement.style.setProperty(
'--docsify_dark_mode_code_color',
codeFontColor
);
document.documentElement.style.setProperty(
'--docsify_dark_mode_header',
header
);
document.documentElement.style.setProperty('--text_color', textColor);
};
let theme = { dark: {}, light: {} };
let defaultConfig = {
dark: {
background: '#1c2022',
toggleBtnBg: '#97b1cb',
textColor: '#b4b4b4',
codeBackground: '#393e46',
codeFontColor: '#eeeeee',
header: '#20202f',
},
light: {
background: 'white',
toggleBtnBg: '#34495e',
textColor: '#2c3e50',
codeBackground: '#f8f8f8',
header: '#231f20',
},
};
theme = { ...defaultConfig, ...vm.config.darkMode };
hook.afterEach(function (html, next) {
let darkEl = ` <div id="dark_mode">
<input class="container_toggle" type="checkbox" id="switch" name="mode" />
<label for="switch">Toggle</label>
</div>`;
html = `${darkEl}${html}`;
next(html);
});
hook.doneEach(function () {
let currColor;
if (localStorage.getItem('DOCSIFY_DARK_MODE')) {
currColor = localStorage.getItem('DOCSIFY_DARK_MODE');
setColor(theme[`${currColor}`]);
} else {
currColor = 'light';
setColor(theme.light);
}
let checkbox = document.querySelector('input[name=mode]');
if (!checkbox) {
return;
}
checkbox.addEventListener('change', function () {
// dark
if (currColor === 'light') {
trans();
setColor(theme.dark);
localStorage.setItem('DOCSIFY_DARK_MODE', 'dark');
currColor = 'dark';
} else {
trans();
setColor(theme.light);
localStorage.setItem('DOCSIFY_DARK_MODE', 'light');
currColor = 'light';
}
});
});
};
window.$docsify.plugins = [].concat(plugin, window.$docsify.plugins);