-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy paththeme.js
More file actions
58 lines (54 loc) · 1.75 KB
/
theme.js
File metadata and controls
58 lines (54 loc) · 1.75 KB
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
function toStyleVariable(variable) {
return '--' + variable.replace(/[A-Z]/g, '-$&').toLowerCase();
}
var isBrowser = typeof window !== 'undefined';
var isNativeSupport = isBrowser && window.CSS && window.CSS.supports && window.CSS.supports('(--a: 0)');
function setRootStyle (theme) {
var targetTheme = null;
window.themeConfig.forEach(function (item) {
if (item.label === theme) {
targetTheme = item;
};
})
var acceptedThemeStyle = targetTheme || window.themeConfig[1];
var variables = {};
var themeKeys = Object.keys(acceptedThemeStyle);
themeKeys.forEach(function(key) {
var varKey = toStyleVariable(key);
variables[varKey] = acceptedThemeStyle[key];
})
var rootStyleSelector = ':root';
var antdStyleId = 'sm-theme-style';
var rootStyle = rootStyleSelector + ' ' + JSON.stringify(variables, null, 2)
.replace(/(:.+),/g, '$1;')
.replace(/"/g, '');
var rootStyleTag = document.getElementById(antdStyleId);
if (!rootStyleTag) {
rootStyleTag = document.createElement('style');
rootStyleTag.setAttribute('id', antdStyleId);
rootStyleTag.setAttribute('type', 'text/css');
document.head.insertBefore(rootStyleTag, document.head.firstChild);
}
var options = {
include: 'style#sm-theme-style, link[href*=css]',
silent: true,
onlyLegacy: true,
variables: {},
watch: false
};
if (!isNativeSupport) {
options.onlyLegacy = false;
options.watch = true;
options.variables = variables;
}
cssVars(options);
rootStyleTag.innerHTML = rootStyle;
}
initSkin();
function initSkin() {
var skin = utils.getCookie('theme') || 'dark';
var rootElem = document.querySelector('html');
rootElem.classList.remove("light", "dark");
rootElem.classList.add(skin);
setRootStyle(skin);
}