Composables e utilitários
useTheme
Lê e aplica o tema (`light`, `dark` ou `system`) num elemento via `data-theme`, com persistência opcional no localStorage.
import { useTheme } from 'vueui-nova-kit'Uso
Por padrão o alvo é document.documentElement e a chave é v-theme — a mesma usada pelo header desta documentação. Num demo isolado, passe target e storageKey: false para não alterar o tema da página. toggleTheme alterna entre claro e escuro com base no tema resolvido.
<script setup lang="ts">
import { useTheme, VButton } from 'vueui-nova-kit'
const { theme, resolvedTheme, setTheme, toggleTheme } = useTheme()
</script>
<template>
<p>Preferência: {{ theme }} · resolvido: {{ resolvedTheme }}</p>
<VButton @click="toggleTheme">Alternar tema</VButton>
</template>Retorno
| Nome | Tipo | Descrição |
|---|---|---|
theme | Ref<'light' | 'dark' | 'system'> | Preferência escolhida pelo usuário. |
resolvedTheme | ComputedRef<'light' | 'dark'> | Tema efetivo, resolvendo `system` com prefers-color-scheme. |
setTheme | (t: 'light' | 'dark' | 'system') => void | Aplica o tema no alvo e persiste se houver storageKey. |
toggleTheme | () => void | Alterna entre claro e escuro a partir do tema resolvido. |
Demo
Exemplo ao vivo usando o próprio composable ou módulo, com componentes da lib.
API
VThemeUnião 'light' | 'dark' | 'system'.
export type VTheme = 'light' | 'dark' | 'system'UseThemeOptionstarget, storageKey e default controlam onde e como o tema é persistido.
export interface UseThemeOptions {
target?: HTMLElement | (() => HTMLElement | null)
storageKey?: string | false
default?: VTheme
}UseThemeReturntheme, resolvedTheme, setTheme e toggleTheme.
export interface UseThemeReturn {
theme: Ref<VTheme>
resolvedTheme: ComputedRef<'light' | 'dark'>
setTheme: (t: VTheme) => void
toggleTheme: () => void
}useThemeSincroniza a preferência de tema com data-theme e, opcionalmente, com o localStorage.
useTheme(options: UseThemeOptions = {}): UseThemeReturn Fonte: src/composables/useTheme.ts