Files
trihuy-russian/web/html/xui/component/themeSwitch.html
T

58 lines
1.9 KiB
HTML
Raw Normal View History

2023-05-08 18:15:08 +04:30
{{define "component/themeSwitchTemplate"}}
<template>
2023-12-04 19:17:38 +01:00
<a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
2023-05-08 18:15:08 +04:30
@change="themeSwitcher.toggleTheme()">
</a-switch>
</template>
{{end}}
{{define "component/themeSwitcher"}}
<script>
function createThemeSwitcher() {
const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
2024-02-28 14:35:01 +03:30
const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
if (isUltra) {
document.documentElement.setAttribute('data-theme', 'ultra-dark');
}
2023-05-08 18:15:08 +04:30
const theme = isDarkTheme ? 'dark' : 'light';
2024-02-28 14:35:01 +03:30
document.querySelector('body').setAttribute('class', theme);
2023-05-08 18:15:08 +04:30
return {
isDarkTheme,
2024-02-28 14:35:01 +03:30
isUltra,
2023-05-08 18:15:08 +04:30
get currentTheme() {
return this.isDarkTheme ? 'dark' : 'light';
},
toggleTheme() {
this.isDarkTheme = !this.isDarkTheme;
localStorage.setItem('dark-mode', this.isDarkTheme);
2024-02-28 14:35:01 +03:30
document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
2024-02-21 15:32:18 +03:30
document.getElementById('message').className = themeSwitcher.currentTheme;
2023-05-08 18:15:08 +04:30
},
2024-02-28 14:35:01 +03:30
toggleUltra() {
this.isUltra = !this.isUltra;
if (this.isUltra) {
document.documentElement.setAttribute('data-theme', 'ultra-dark');
} else {
document.documentElement.removeAttribute('data-theme');
}
localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
}
2023-05-08 18:15:08 +04:30
};
}
const themeSwitcher = createThemeSwitcher();
Vue.component('theme-switch', {
props: [],
template: `{{template "component/themeSwitchTemplate"}}`,
2024-02-28 14:35:01 +03:30
data: () => ({
themeSwitcher
}),
2024-02-21 15:32:18 +03:30
mounted() {
2024-02-28 14:35:01 +03:30
this.$message.config({
getContainer: () => document.getElementById('message')
});
2024-02-21 15:32:18 +03:30
document.getElementById('message').className = themeSwitcher.currentTheme;
}
2023-05-08 18:15:08 +04:30
});
</script>
2024-02-28 14:35:01 +03:30
{{end}}