summaryrefslogtreecommitdiff
path: root/src/components/_sila/Global/ThemeButton.vue
blob: 687e0b19c05007e829ce5db12ba1697953575d07 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template>
  <b-nav-item @click="toggleTheme">
    <icon-sun v-if="userTheme !== 'dark-theme'" />
    <icon-moon v-if="userTheme === 'dark-theme'" />
  </b-nav-item>
</template>

<script>
import IconSun from '@carbon/icons-vue/es/sun/20';
import IconMoon from '@carbon/icons-vue/es/moon/20';

export default {
  name: 'ThemeButton',
  components: {
    IconSun,
    IconMoon,
  },
  data() {
    return {
      userTheme: 'light-theme',
    };
  },
  mounted() {
    const initUserTheme = this.getTheme();
    this.setTheme(initUserTheme);
  },
  methods: {
    toggleTheme() {
      const activeTheme = localStorage.getItem('user-theme');
      if (activeTheme === 'light-theme') {
        this.setTheme('dark-theme');
      } else {
        this.setTheme('light-theme');
      }
    },

    getTheme() {
      return localStorage.getItem('user-theme');
    },

    setTheme(theme) {
      localStorage.setItem('user-theme', theme);
      this.$root.$emit('change-theme');
      this.userTheme = theme;
      document.documentElement.className = theme;
    },
  },
};
</script>

<style lang="scss">
.dark-theme {
  body,
  .app-content {
    background-color: $surface-primary-dark !important;
    color: $text-primary-dark;
  }
}
</style>