feat: Establish core application layout, global styles, and theme mode management.
This commit is contained in:
parent
a2ce1d79a2
commit
0e095b35c5
4 changed files with 79 additions and 30 deletions
39
Frontend-Learner/composables/useThemeMode.ts
Normal file
39
Frontend-Learner/composables/useThemeMode.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { useQuasar } from 'quasar'
|
||||
|
||||
export const useThemeMode = () => {
|
||||
const $q = useQuasar()
|
||||
|
||||
// deterministic on SSR: default = light
|
||||
const isDark = useState<boolean>('theme:isDark', () => false)
|
||||
|
||||
const applyTheme = (value: boolean) => {
|
||||
if (!process.client) return
|
||||
document.documentElement.classList.toggle('dark', value)
|
||||
$q.dark.set(value)
|
||||
localStorage.setItem('theme', value ? 'dark' : 'light')
|
||||
}
|
||||
|
||||
const initTheme = () => {
|
||||
if (!process.client) return
|
||||
const saved = localStorage.getItem('theme') // 'dark' | 'light' | null
|
||||
const prefersDark = window.matchMedia?.('(prefers-color-scheme: dark)')?.matches ?? false
|
||||
|
||||
const value = saved ? saved === 'dark' : prefersDark
|
||||
isDark.value = value
|
||||
applyTheme(value)
|
||||
}
|
||||
|
||||
onMounted(initTheme)
|
||||
|
||||
watch(isDark, (v) => applyTheme(v))
|
||||
|
||||
const toggle = () => {
|
||||
isDark.value = !isDark.value
|
||||
}
|
||||
|
||||
const set = (v: boolean) => {
|
||||
isDark.value = v
|
||||
}
|
||||
|
||||
return { isDark, toggle, set }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue