Skip to content

Commit

Permalink
feat: add automatic theme from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
caro3801 committed Oct 4, 2024
1 parent 0a859ff commit e9ecf16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
34 changes: 28 additions & 6 deletions src/composables/useTheme.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import { reactive } from 'vue'

import themeLight from '@/assets/images/illustrations/theme-light.png'
import themeDark from '@/assets/images/illustrations/theme-dark.png'
let eventAdded = false

export function useTheme() {
const preferredTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
const LOCAL_STORAGE_KEY = 'data-bs-theme'
const theme = localStorage.getItem(LOCAL_STORAGE_KEY) || preferredTheme
const AUTOMATIC = 'automatic'
function getTheme() {
return localStorage.getItem(LOCAL_STORAGE_KEY) ?? AUTOMATIC
}
function getAutomaticTheme() {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}
if (!eventAdded) {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (event) => {
if (getTheme() === AUTOMATIC) {
setTheme(AUTOMATIC)
}
})
eventAdded = true
}
function setTheme(theme) {
document.body?.setAttribute('data-bs-theme', theme)
document.body?.setAttribute('data-bs-theme', theme === AUTOMATIC ? getAutomaticTheme() : theme)
localStorage.setItem(LOCAL_STORAGE_KEY, theme)
}
const themes = [
const themes = reactive([
{
icon: 'paint-roller',
name: 'automatic',
label: `Automatic`,
thumbnail: ''
},
{
icon: 'sun',
name: 'light',
Expand All @@ -22,6 +44,6 @@ export function useTheme() {
label: 'Dark mode',
thumbnail: themeDark
}
]
return { theme, setTheme, preferredTheme, themes }
])
return { getTheme, setTheme, themes }
}
5 changes: 2 additions & 3 deletions src/core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,8 @@ class Core extends Behaviors {
}

loadTheme() {
const { theme, setTheme } = useTheme()
console.log(theme)
setTheme(theme)
const { getTheme, setTheme } = useTheme()
setTheme(getTheme())
}
/**
* Append the given title to the page title
Expand Down
7 changes: 3 additions & 4 deletions src/views/Settings/SettingsView/SettingsViewAppearance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import SettingsViewLayout from '@/views/Settings/SettingsView/SettingsViewLayout
import { useTheme } from '@/composables/useTheme'
defineOptions({ name: 'SettingsViewAppearance' })
const { theme, setTheme, themes } = useTheme()
const { getTheme, setTheme, themes } = useTheme()
const { t } = useI18n()
const infoLabel = computed(() => t('settings.appearance.info'))
const selectedTheme = ref(theme)
const options = ref(themes)
const selectedTheme = ref(getTheme())
watch(
() => selectedTheme.value,
Expand All @@ -24,6 +23,6 @@ watch(

<template>
<settings-view-layout info-name="appearance" :info-label="infoLabel">
<settings-appearance-radio-group v-model="selectedTheme" :options="options" />
<settings-appearance-radio-group v-model="selectedTheme" :options="themes" />
</settings-view-layout>
</template>

0 comments on commit e9ecf16

Please sign in to comment.