Skip to content
This repository was archived by the owner on Jan 3, 2026. It is now read-only.

Commit 4ab8a5c

Browse files
committed
feat: auto adapt to prefered color schema
1 parent f98ae86 commit 4ab8a5c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/App.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
<script setup lang="ts">
22
import { RouterView } from 'vue-router';
3+
import { darkTheme } from 'naive-ui';
4+
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
5+
import type { BuiltInGlobalTheme } from 'naive-ui/es/themes/interface';
6+
7+
const usingTheme = ref<BuiltInGlobalTheme|null>(null);
8+
9+
const isSystemDarkTheme = computed(
10+
() => !!window.matchMedia('(prefers-color-scheme: dark)')?.matches
11+
);
12+
13+
const updateTheme = () => {
14+
usingTheme.value = isSystemDarkTheme.value ? darkTheme : null;
15+
};
16+
17+
onMounted(() => {
18+
updateTheme();
19+
window.matchMedia('(prefers-color-scheme: dark)')
20+
.addEventListener('change', updateTheme);
21+
});
22+
23+
onBeforeUnmount(() => {
24+
window.matchMedia('(prefers-color-scheme: dark)')
25+
.removeEventListener('change', updateTheme);
26+
});
327
</script>
428

529
<template>
6-
<n-config-provider class="size-full !bg-transparent">
30+
<n-config-provider :theme="usingTheme" class="size-full !bg-transparent">
731
<RouterView />
832
</n-config-provider>
933
</template>

0 commit comments

Comments
 (0)