Skip to content

Commit dcb5a55

Browse files
committed
feat(theme): adds dark/light theme switch
1 parent aba46ea commit dcb5a55

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

src/components/AppView.vue

+19-33
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ const store = useRouterStore();
1818
<HelloWorld :msg="i18n('app_name')" />
1919

2020
<nav>
21-
<RouterLink :to="`${store.baseName}/`">Home</RouterLink>
22-
<RouterLink :to="`${store.baseName}/about`">About</RouterLink>
21+
<NButtonGroup class="buttons">
22+
<RouterLink v-slot="{ href, navigate, isActive }" :to="`${ store.baseName }/`" custom>
23+
<NButton round :class="{ active: isActive }" :href="href" @click="navigate">Home</NButton>
24+
</RouterLink>
25+
<RouterLink v-slot="{ href, navigate, isActive }" :to="`${store.baseName}/about`" custom>
26+
<NButton round :class="{ active: isActive }" :href="href" @click="navigate">About</NButton>
27+
</RouterLink>
28+
</NButtonGroup>
2329
</nav>
24-
25-
<NButtonGroup class="buttons">
26-
<RouterLink :to="`${store.baseName}/`"><NButton>Home</NButton></RouterLink>
27-
<NButton ghost> One More </NButton>
28-
<RouterLink :to="`${store.baseName}/about`"><NButton>About/NButton></NButton></RouterLink>
29-
</NButtonGroup>
3030
</div>
3131
</header>
3232
<main>
@@ -40,9 +40,20 @@ header {
4040
flex-direction: column;
4141
justify-content: center;
4242
43+
nav {
44+
margin-top: 2rem;
45+
font-size: 12px;
46+
text-align: center;
47+
}
48+
4349
.buttons {
4450
display: flex;
4551
justify-content: center;
52+
53+
.active {
54+
color: var(--n-text-color-pressed);
55+
background-color: var(--n-color-pressed);
56+
}
4657
}
4758
}
4859
@@ -54,29 +65,4 @@ main {
5465
display: block;
5566
margin: 2rem auto;
5667
}
57-
58-
nav {
59-
margin-top: 2rem;
60-
font-size: 12px;
61-
text-align: center;
62-
63-
& a {
64-
display: inline-block;
65-
padding: 0 1rem;
66-
border-left: 1px solid var(--color-border);
67-
68-
&.router-link-exact-active {
69-
color: var(--color-text);
70-
71-
&:hover,
72-
&:focus {
73-
background-color: transparent;
74-
}
75-
}
76-
77-
&:first-of-type {
78-
border: 0;
79-
}
80-
}
81-
}
8268
</style>

src/components/web/AppWeb.ce.vue

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
<script setup lang="ts">
2+
import { darkTheme, lightTheme, NConfigProvider } from 'naive-ui';
3+
4+
import { computed } from 'vue';
5+
6+
import { watchPreferDark } from '~/utils';
27
import { lazyComponent } from '~/utils/lazy.utils';
38
49
const AppView = lazyComponent(() => import('~/components/AppView.vue'));
10+
11+
const isDark = watchPreferDark();
12+
const theme = computed(() => (isDark.value ? darkTheme : lightTheme));
513
</script>
614

715
<template>
816
<div id="trakt-extension-root">
9-
<AppView />
17+
<NConfigProvider :theme="theme">
18+
<AppView />
19+
</NConfigProvider>
1020
</div>
1121
</template>
1222

src/utils/window.utils.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
export { isDarkTheme } from '@dvcol/web-extension-utils/lib/common/utils/window.utils';
1+
import { isDarkTheme } from '@dvcol/web-extension-utils/lib/common/utils/window.utils';
2+
import { onUnmounted, ref } from 'vue';
3+
4+
export { isDarkTheme };
5+
6+
export const watchMedia = (query: string) => {
7+
const media = window.matchMedia(query);
8+
const matchMedia = ref(media.matches);
9+
10+
const listener = (_change: MediaQueryListEvent) => {
11+
matchMedia.value = _change.matches;
12+
console.info('value change', matchMedia.value);
13+
};
14+
media.addEventListener('change', listener);
15+
onUnmounted(() => media.removeEventListener('change', listener));
16+
return matchMedia;
17+
};
18+
19+
export const watchPreferDark = () => watchMedia('(prefers-color-scheme: dark');

0 commit comments

Comments
 (0)