Skip to content

Commit 40189e0

Browse files
authored
fix: Optimize Dark Mode Toggle Functionality (#35)
This PR simplifies and optimizes the dark mode toggle logic. It ensures correct functionality when the color mode preference is set to 'system'. Now, the button works seamlessly for all initial states, including 'system', and correctly toggles between 'dark' and 'light' modes.
1 parent 10d4d19 commit 40189e0

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

app/app.vue

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ watch(loggedIn, () => {
1010
}
1111
})
1212
13-
function toggleColorMode() {
14-
colorMode.preference = colorMode.preference === 'dark' ? 'light' : 'dark'
15-
}
13+
const isDarkMode = computed({
14+
get: () => colorMode.preference === 'dark',
15+
set: () =>
16+
(colorMode.preference = colorMode.value === 'dark' ? 'light' : 'dark')
17+
})
1618
1719
useHead({
1820
htmlAttrs: { lang: 'en' },
@@ -47,8 +49,12 @@ const items = [
4749
square
4850
variant="ghost"
4951
color="black"
50-
:icon="$colorMode.preference === 'dark' ? 'i-heroicons-moon' : 'i-heroicons-sun'"
51-
@click="toggleColorMode"
52+
:icon="
53+
$colorMode.preference === 'dark' || $colorMode.preference === 'system'
54+
? 'i-heroicons-moon'
55+
: 'i-heroicons-sun'
56+
"
57+
@click="isDarkMode = !isDarkMode"
5258
/>
5359
</div>
5460

0 commit comments

Comments
 (0)