themeSwitch.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-menu :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
  4. <a-sub-menu>
  5. <span slot="title">
  6. <a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
  7. <span>Theme</span>
  8. </span>
  9. <a-menu-item id="change-theme" class="ant-menu-theme-switch" @mousedown="themeSwitcher.animationsOff()"> Dark <a-switch style="margin-left: 2px;" size="small" :default-checked="themeSwitcher.isDarkTheme" @change="themeSwitcher.toggleTheme()"></a-switch>
  10. </a-menu-item>
  11. <a-menu-item id="change-theme-ultra" v-if="themeSwitcher.isDarkTheme" class="ant-menu-theme-switch" @mousedown="themeSwitcher.animationsOffUltra()"> Ultra <a-checkbox style="margin-left: 2px;" :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()"></a-checkbox>
  12. </a-menu-item>
  13. </a-sub-menu>
  14. </a-menu>
  15. </template>
  16. {{end}}
  17. {{define "component/themeSwitchTemplateLogin"}}
  18. <template>
  19. <a-menu @mousedown="themeSwitcher.animationsOff()" id="change-theme" :theme="themeSwitcher.currentTheme" mode="inline" selected-keys="">
  20. <a-menu-item mode="inline" class="ant-menu-theme-switch">
  21. <a-icon type="bulb" :theme="themeSwitcher.isDarkTheme ? 'filled' : 'outlined'"></a-icon>
  22. <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme" @change="themeSwitcher.toggleTheme()"></a-switch>
  23. <template v-if="themeSwitcher.isDarkTheme">
  24. <a-checkbox style="margin-left: 1rem; vertical-align: middle;" :checked="themeSwitcher.isUltra" @click="themeSwitcher.toggleUltra()">Ultra</a-checkbox>
  25. </template>
  26. </a-menu-item>
  27. </a-menu>
  28. </template>
  29. {{end}}
  30. {{define "component/themeSwitcher"}}
  31. <script>
  32. function createThemeSwitcher() {
  33. const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
  34. const isUltra = localStorage.getItem('isUltraDarkThemeEnabled') === 'true';
  35. if (isUltra) {
  36. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  37. }
  38. const theme = isDarkTheme ? 'dark' : 'light';
  39. document.querySelector('body').setAttribute('class', theme);
  40. return {
  41. animationsOff() {
  42. document.documentElement.setAttribute('data-theme-animations', 'off');
  43. const themeAnimations = document.querySelector('#change-theme');
  44. themeAnimations.addEventListener('mouseleave', () => {
  45. document.documentElement.removeAttribute('data-theme-animations');
  46. });
  47. themeAnimations.addEventListener('touchend', () => {
  48. document.documentElement.removeAttribute('data-theme-animations');
  49. });
  50. },
  51. animationsOffUltra() {
  52. document.documentElement.setAttribute('data-theme-animations', 'off');
  53. const themeAnimationsUltra = document.querySelector('#change-theme-ultra');
  54. themeAnimationsUltra.addEventListener('mouseleave', () => {
  55. document.documentElement.removeAttribute('data-theme-animations');
  56. });
  57. themeAnimationsUltra.addEventListener('touchend', () => {
  58. document.documentElement.removeAttribute('data-theme-animations');
  59. });
  60. },
  61. isDarkTheme,
  62. isUltra,
  63. get currentTheme() {
  64. return this.isDarkTheme ? 'dark' : 'light';
  65. },
  66. toggleTheme() {
  67. this.isDarkTheme = !this.isDarkTheme;
  68. localStorage.setItem('dark-mode', this.isDarkTheme);
  69. document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light');
  70. document.getElementById('message').className = themeSwitcher.currentTheme;
  71. },
  72. toggleUltra() {
  73. this.isUltra = !this.isUltra;
  74. if (this.isUltra) {
  75. document.documentElement.setAttribute('data-theme', 'ultra-dark');
  76. } else {
  77. document.documentElement.removeAttribute('data-theme');
  78. }
  79. localStorage.setItem('isUltraDarkThemeEnabled', this.isUltra.toString());
  80. }
  81. };
  82. }
  83. const themeSwitcher = createThemeSwitcher();
  84. Vue.component('theme-switch', {
  85. props: [],
  86. template: `{{template "component/themeSwitchTemplate"}}`,
  87. data: () => ({
  88. themeSwitcher
  89. }),
  90. mounted() {
  91. this.$message.config({
  92. getContainer: () => document.getElementById('message')
  93. });
  94. document.getElementById('message').className = themeSwitcher.currentTheme;
  95. }
  96. });
  97. Vue.component('theme-switch-login', {
  98. props: [],
  99. template: `{{template "component/themeSwitchTemplateLogin"}}`,
  100. data: () => ({
  101. themeSwitcher
  102. }),
  103. mounted() {
  104. this.$message.config({
  105. getContainer: () => document.getElementById('message')
  106. });
  107. document.getElementById('message').className = themeSwitcher.currentTheme;
  108. }
  109. });
  110. </script>
  111. {{end}}