forked from kognise/water.css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (103 loc) · 3.43 KB
/
script.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
/* global matchMedia, faviconModeSwitcher */
(function () {
const iconModeSwitcher = window.faviconModeSwitcher && faviconModeSwitcher.default
if (!iconModeSwitcher) return
iconModeSwitcher([
{
element: 'link[rel="shortcut icon"]',
href: { dark: 'icons/light-favicon.ico' }
},
{
element: 'link[rel="icon"][sizes="16x16"]',
href: { dark: 'icons/light-favicon-16x16.png' }
},
{
element: 'link[rel="icon"][sizes="32x32"]',
href: { dark: 'icons/light-favicon-32x32.png' }
}
])
})()
;(function (ThemeSwitcher) {
const themeSwitcher = new ThemeSwitcher('stylesheet')
const themeSwitchBtn = document.getElementById('switch')
const themes = {
dark: 'dark',
darkStandalone: 'dark.standalone',
light: 'light',
lightStandalone: 'light.standalone'
}
const getSwitchThemeName = function () {
// Case: switch to "light.standalone.css"
if (
((themeSwitcher.current === themes.dark) && themeSwitcher.isDark) ||
((themeSwitcher.current === themes.light) && themeSwitcher.isDark) ||
themeSwitcher.current === themes.darkStandalone
) {
return themes.lightStandalone
// Case: switch to "dark.standalone.css"
} else if (
((themeSwitcher.current === themes.dark) && themeSwitcher.isLight) ||
((themeSwitcher.current === themes.light) && themeSwitcher.isLight) ||
themeSwitcher.current === themes.lightStandalone
) {
return themes.darkStandalone
// Case: switch to "light.css"
} else if (themeSwitcher.current === themes.dark) {
return themes.light
// Case: switch to "dark.css"
} else if (themeSwitcher.current === themes.light) {
return themes.dark
// Case: switch destination is unknown
} else {
return themeSwitcher.current
}
}
const getGeneralThemeName = function () {
return themeSwitcher.current.replace(/\.standalone/g, '')
}
themeSwitchBtn.addEventListener('click', function () {
themeSwitcher.switch(getSwitchThemeName())
})
themeSwitcher.onChangeDark = function () {
themeSwitcher.switch(getGeneralThemeName())
}
themeSwitcher.onChangeLight = function () {
themeSwitcher.switch(getGeneralThemeName())
}
})(
(function () {
const ThemeSwitcher = function (stylesheet) {
const darkSchemeMql = matchMedia('(prefers-color-scheme: dark)')
const lightSchemeMql = matchMedia('(prefers-color-scheme: light)')
const that = this
this.themeDir = 'dist/'
this.stylesheet = document.getElementById(stylesheet)
this.current = this.getThemeName(this.stylesheet.href)
this.isDark = darkSchemeMql.matches
this.isLight = lightSchemeMql.matches
darkSchemeMql.addListener(function (mql) {
if (mql.matches && typeof that.onChangeDark === 'function') {
that.onChangeDark()
}
})
lightSchemeMql.addListener(function (mql) {
if (mql.matches && typeof that.onChangeLight === 'function') {
that.onChangeLight()
}
})
}
ThemeSwitcher.prototype = {
switch: function (themeName) {
this.stylesheet.href = this.themeDir + themeName + '.css'
this.current = themeName
},
getThemeName: function () {
const reg = new RegExp(this.themeDir + '(|.+?).css')
return this.stylesheet.getAttribute('href').replace(reg, '$1')
},
onChangeDark: null,
onChangeLight: null
}
return ThemeSwitcher
})()
)