themeSwitch.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-menu class="change-theme" :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
  4. <a-menu-item mode="inline" class="ant-menu-theme-switch">
  5. <a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
  6. <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme" @change="themeSwitcher.toggleTheme()"></a-switch>
  7. <template v-if="themeSwitcher.isDarkTheme">
  8. <a-checkbox style="margin-left: 1rem; vertical-align: middle;" :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()">Ultra</a-checkbox>
  9. </template>
  10. </a-menu-item>
  11. </a-menu>
  12. </template>
  13. {{end}}
  14. {{define "component/themeSwitcher"}}
  15. <script>
  16. function createThemeSwitcher() {
  17. const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
  18. const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
  19. if (isUltra) {
  20. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  21. }
  22. const theme = isDarkTheme ? 'dark' : 'light';
  23. document.querySelector('body').setAttribute('class', theme);
  24. return {
  25. isDarkTheme,
  26. isUltra,
  27. get currentTheme() {
  28. return this.isDarkTheme ? 'dark' : 'light';
  29. },
  30. toggleTheme() {
  31. this.isDarkTheme = !this.isDarkTheme;
  32. localStorage.setItem('dark-mode', this.isDarkTheme);
  33. document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
  34. document.getElementById('message').className = themeSwitcher.currentTheme;
  35. },
  36. toggleUltra() {
  37. this.isUltra = !this.isUltra;
  38. if (this.isUltra) {
  39. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  40. } else {
  41. document.documentElement.removeAttribute('data-theme');
  42. }
  43. localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
  44. }
  45. };
  46. }
  47. const themeSwitcher = createThemeSwitcher();
  48. Vue.component('theme-switch', {
  49. props: [],
  50. template: `{{template "component/themeSwitchTemplate"}}`,
  51. data: () => ({
  52. themeSwitcher
  53. }),
  54. mounted() {
  55. this.$message.config({
  56. getContainer: () => document.getElementById('message')
  57. });
  58. document.getElementById('message').className = themeSwitcher.currentTheme;
  59. const themeAnimations = document.querySelector('.change-theme');
  60. themeAnimations.addEventListener('mousedown', () => {
  61. document.documentElement.setAttribute('data-theme-animations', 'off');
  62. });
  63. themeAnimations.addEventListener('mouseleave', () => {
  64. document.documentElement.removeAttribute('data-theme-animations');
  65. });
  66. }
  67. });
  68. </script>
  69. {{end}}