1
0

themeSwitch.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
  13. if (isUltra) {
  14. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  15. }
  16. const theme = isDarkTheme ? 'dark' : 'light';
  17. document.querySelector('body').setAttribute('class', theme);
  18. return {
  19. isDarkTheme,
  20. isUltra,
  21. get currentTheme() {
  22. return this.isDarkTheme ? 'dark' : 'light';
  23. },
  24. toggleTheme() {
  25. this.isDarkTheme = !this.isDarkTheme;
  26. localStorage.setItem('dark-mode', this.isDarkTheme);
  27. document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
  28. document.getElementById('message').className = themeSwitcher.currentTheme;
  29. },
  30. toggleUltra() {
  31. this.isUltra = !this.isUltra;
  32. if (this.isUltra) {
  33. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  34. } else {
  35. document.documentElement.removeAttribute('data-theme');
  36. }
  37. localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
  38. }
  39. };
  40. }
  41. const themeSwitcher = createThemeSwitcher();
  42. Vue.component('theme-switch', {
  43. props: [],
  44. template: `{{template "component/themeSwitchTemplate"}}`,
  45. data: () => ({
  46. themeSwitcher
  47. }),
  48. mounted() {
  49. this.$message.config({
  50. getContainer: () => document.getElementById('message')
  51. });
  52. document.getElementById('message').className = themeSwitcher.currentTheme;
  53. }
  54. });
  55. </script>
  56. {{end}}