2023-05-08 16:45:08 +03:00
|
|
|
{{define "component/themeSwitchTemplate"}}
|
|
|
|
<template>
|
2023-12-04 21:17:38 +03:00
|
|
|
<a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
|
2023-05-08 16:45:08 +03:00
|
|
|
@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';
|
2024-01-23 22:46:33 +03:00
|
|
|
document.querySelector('body').setAttribute('class', theme)
|
2023-05-08 16:45:08 +03:00
|
|
|
return {
|
|
|
|
isDarkTheme,
|
|
|
|
get currentTheme() {
|
|
|
|
return this.isDarkTheme ? 'dark' : 'light';
|
|
|
|
},
|
|
|
|
toggleTheme() {
|
|
|
|
this.isDarkTheme = !this.isDarkTheme;
|
|
|
|
localStorage.setItem('dark-mode', this.isDarkTheme);
|
2024-01-23 22:46:33 +03:00
|
|
|
document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light')
|
2024-02-21 15:02:18 +03:00
|
|
|
document.getElementById('message').className = themeSwitcher.currentTheme;
|
2023-05-08 16:45:08 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const themeSwitcher = createThemeSwitcher();
|
|
|
|
|
|
|
|
Vue.component('theme-switch', {
|
|
|
|
props: [],
|
|
|
|
template: `{{template "component/themeSwitchTemplate"}}`,
|
|
|
|
data: () => ({ themeSwitcher }),
|
2024-02-21 15:02:18 +03:00
|
|
|
mounted() {
|
|
|
|
this.$message.config({getContainer: () => document.getElementById('message')});
|
|
|
|
document.getElementById('message').className = themeSwitcher.currentTheme;
|
|
|
|
}
|
2023-05-08 16:45:08 +03:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
{{end}}
|