Skip to content

Commit

Permalink
feat(ui): ✨ add darkmode
Browse files Browse the repository at this point in the history
  • Loading branch information
wrzrmzx committed Mar 30, 2024
1 parent 7648160 commit 003ffe4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
33 changes: 33 additions & 0 deletions components/toolbar/slider/DarkModeSwitch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="ts" setup>
import type { Themes } from '~/composables/darkmode'
import { themePreference } from '~/composables/darkmode'
const { t } = useI18n()
function changeTheme(theme: Themes) {
themePreference.value = theme
}
</script>

<template>
<div class="flex flex-row space-x-1 text-xl">
<div
class="i-uil:desktop cursor-pointer"
:class="{ 'text-gray-400 dark:text-gray-600': themePreference !== 'system' }"
:title="t('darkmode.system')"
@click="changeTheme('system')"
/>
<div
class="i-uil:sun cursor-pointer"
:class="{ 'text-gray-400 dark:text-gray-600': themePreference !== 'light' }"
:title="t('darkmode.light')"
@click="changeTheme('light')"
/>
<div
class="i-uil:moon cursor-pointer"
:class="{ 'text-gray-400 dark:text-gray-600': themePreference !== 'dark' }"
:title="t('darkmode.dark')"
@click="changeTheme('dark')"
/>
</div>
</template>
2 changes: 2 additions & 0 deletions components/toolbar/slider/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ watchEffect(() => {
<ToolbarLogo v-model:show-slider="show" class="flex-grow-0" />

<ToolbarSliderGeneralLinks />

<ToolbarSliderDarkModeSwitch />
</div>
<PMask v-model:show="show" />
</div>
Expand Down
38 changes: 38 additions & 0 deletions composables/darkmode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const themes = ['system', 'light', 'dark'] as const
export type Themes = typeof themes[number]

export const themePreference = useLocalStorage<Themes>('theme_preference', 'system', {
listenToStorageChanges: true,
serializer: {
read(raw): Themes {
return themes.includes(raw as any) ? (raw as Themes) : 'system'
},
write(value): string {
return value
},
},
})

export const isDark = computed(() =>
themePreference.value === 'system' ? usePreferredDark().value : themePreference.value === 'dark',
)

export function applyTheme() {
// Attention: only client has 'document' object
if (process.client) {
const html = document.documentElement
const metaTag = document.querySelector('meta[name="theme-color"]')
watchEffect(() => {
if (isDark.value) {
html.classList.add('dark')
if (metaTag)
metaTag.setAttribute('content', '#1f2937')
}
else {
html.classList.remove('dark')
if (metaTag)
metaTag.setAttribute('content', '#ffffff')
}
})
}
}
6 changes: 6 additions & 0 deletions plugins/darkmode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// plugins/theme.ts
import { applyTheme } from '~/composables/darkmode'

export default defineNuxtPlugin(() => {
applyTheme()
})

0 comments on commit 003ffe4

Please sign in to comment.