aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/DynamicTheme.vue
blob: 2ccb47b2d5afc243e6ddaec3cc8d8544f3868469 (plain) (blame)
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
<template>
  <DynamicStyle>
    /* light / dark theme switch based on system pref if available */ body #app
    {
    {{ getVars(themes.light) }}
    } @media (prefers-color-scheme: light), (prefers-color-scheme:
    no-preference) { body #app {
    {{ getVars(themes.light) }}
    } } @media (prefers-color-scheme: dark) { body #app { } } /* light / dark
    theme override base on user choice. */ body #app.is-dark {
    {{ getVars(themes.dark) }}
    } body #app.is-light {
    {{ getVars(themes.light) }}
    }
  </DynamicStyle>
</template>

<script>
export default {
  name: "DynamicTheme",
  props: {
    themes: Object,
  },
  methods: {
    getVars: function (theme) {
      let vars = [];
      for (const themeVars in theme) {
        let value = `${theme[themeVars]}`;
        if (!value) {
          value = "inital";
        } else if (themeVars == "background-image") {
          value = `url(${theme[themeVars]})`;
        }
        vars.push(`--${themeVars}: ${value}`);
      }
      return vars.join(";");
    },
  },
};
</script>