themeSwitch.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. {{define "component/themeSwitchTemplate"}}
  2. <template>
  3. <a-switch :default-checked="themeSwitcher.isDarkTheme"
  4. checked-children="☀"
  5. un-checked-children="🌙"
  6. @change="themeSwitcher.toggleTheme()">
  7. </a-switch>
  8. </template>
  9. {{end}}
  10. {{define "component/themeSwitcher"}}
  11. <script>
  12. const colors = {
  13. dark: {
  14. bg: "#242c3a",
  15. text: "hsla(0,0%,100%,.65)"
  16. },
  17. light: {
  18. bg: '#f0f2f5',
  19. text: "rgba(0, 0, 0, 0.7)",
  20. }
  21. }
  22. function createThemeSwitcher() {
  23. const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
  24. const theme = isDarkTheme ? 'dark' : 'light';
  25. return {
  26. isDarkTheme,
  27. bgStyle: `background: ${colors[theme].bg};`,
  28. textStyle: `color: ${colors[theme].text};`,
  29. darkClass: isDarkTheme ? 'ant-dark' : '',
  30. darkCardClass: isDarkTheme ? 'ant-card-dark' : '',
  31. darkDrawerClass: isDarkTheme ? 'ant-drawer-dark' : '',
  32. get currentTheme() {
  33. return this.isDarkTheme ? 'dark' : 'light';
  34. },
  35. toggleTheme() {
  36. this.isDarkTheme = !this.isDarkTheme;
  37. this.theme = this.isDarkTheme ? 'dark' : 'light';
  38. localStorage.setItem('dark-mode', this.isDarkTheme);
  39. this.bgStyle = `background: ${colors[this.theme].bg};`;
  40. this.textStyle = `color: ${colors[this.theme].text};`;
  41. this.darkClass = this.isDarkTheme ? 'ant-dark' : '';
  42. this.darkCardClass = this.isDarkTheme ? 'ant-card-dark' : '';
  43. this.darkDrawerClass = this.isDarkTheme ? 'ant-drawer-dark' : '';
  44. },
  45. };
  46. }
  47. const themeSwitcher = createThemeSwitcher();
  48. Vue.component('theme-switch', {
  49. props: [],
  50. template: `{{template "component/themeSwitchTemplate"}}`,
  51. data: () => ({ themeSwitcher }),
  52. });
  53. </script>
  54. {{end}}