Welcome to mirror list, hosted at ThFree Co, Russian Federation.

themeSwitch.html « component « xui « html « web - github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ca080d927abf583805a4d1b3705e6dd020fe13f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{{define "component/themeSwitchTemplate"}}
<template>
  <a-switch size="small" :default-checked="themeSwitcher.isDarkTheme"
            @change="themeSwitcher.toggleTheme()">
  </a-switch>
</template>
{{end}}

{{define "component/themeSwitcher"}}
<script>
  function createThemeSwitcher() {
    const isDarkTheme = localStorage.getItem('dark-mode') === 'true';
    const theme = isDarkTheme ? 'dark' : 'light';
    document.querySelector('body').setAttribute('class', theme)
    return {
      isDarkTheme,
      get currentTheme() {
        return this.isDarkTheme ? 'dark' : 'light';
      },
      toggleTheme() {
        this.isDarkTheme = !this.isDarkTheme;
        localStorage.setItem('dark-mode', this.isDarkTheme);
        document.querySelector('body').setAttribute('class', this.isDarkTheme ? 'dark' : 'light')
        document.getElementById('message').className = themeSwitcher.currentTheme;
      },
    };
  }

  const themeSwitcher = createThemeSwitcher();

  Vue.component('theme-switch', {
    props: [],
    template: `{{template "component/themeSwitchTemplate"}}`,
    data: () => ({ themeSwitcher }),
    mounted() {
      this.$message.config({getContainer: () => document.getElementById('message')});
      document.getElementById('message').className = themeSwitcher.currentTheme;
    }
  });
</script>
{{end}}