mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-02 01:30:51 +03:00
41 lines
1.3 KiB
HTML
41 lines
1.3 KiB
HTML
{{define "component/themeSwitchTemplate"}}
|
|
<template>
|
|
<a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
|
|
@change="themeSwitcher.toggleTheme()">
|
|
</a-switch>
|
|
</template>
|
|
{{end}}
|
|
|
|
{{define "component/themeSwitcher"}}
|
|
<script>
|
|
function createThemeSwitcher() {
|
|
const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
|
|
const theme = isDarkTheme ? 'dark' : 'light';
|
|
document.querySelector('body').setAttribute('class', theme)
|
|
return {
|
|
isDarkTheme,
|
|
get currentTheme() {
|
|
return this.isDarkTheme ? 'dark' : 'light';
|
|
},
|
|
toggleTheme() {
|
|
this.isDarkTheme = !this.isDarkTheme;
|
|
localStorage.setItem('dark-mode', this.isDarkTheme);
|
|
document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light')
|
|
document.getElementById('message').className = themeSwitcher.currentTheme;
|
|
},
|
|
};
|
|
}
|
|
|
|
const themeSwitcher = createThemeSwitcher();
|
|
|
|
Vue.component('theme-switch', {
|
|
props: [],
|
|
template: `{{template "component/themeSwitchTemplate"}}`,
|
|
data: () => ({ themeSwitcher }),
|
|
mounted() {
|
|
this.$message.config({getContainer: () => document.getElementById('message')});
|
|
document.getElementById('message').className = themeSwitcher.currentTheme;
|
|
}
|
|
});
|
|
</script>
|
|
{{end}} |