1
0

themeSwitch.html 900 B

12345678910111213141516171819202122232425262728293031323334
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
  4. @change="themeSwitcher.toggleTheme()">
  5. </a-switch>
  6. </template>
  7. {{end}}
  8. {{define "component/themeSwitcher"}}
  9. <script>
  10. function createThemeSwitcher() {
  11. const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
  12. const theme = isDarkTheme ? 'dark' : 'light';
  13. return {
  14. isDarkTheme,
  15. get currentTheme() {
  16. return this.isDarkTheme ? 'dark' : 'light';
  17. },
  18. toggleTheme() {
  19. this.isDarkTheme = !this.isDarkTheme;
  20. localStorage.setItem('dark-mode', this.isDarkTheme);
  21. },
  22. };
  23. }
  24. const themeSwitcher = createThemeSwitcher();
  25. Vue.component('theme-switch', {
  26. props: [],
  27. template: `{{template "component/themeSwitchTemplate"}}`,
  28. data: () => ({ themeSwitcher }),
  29. });
  30. </script>
  31. {{end}}