Composables e utilitários

useTheme

Composable / módulo

Lê e aplica o tema (`light`, `dark` ou `system`) num elemento via `data-theme`, com persistência opcional no localStorage.

Importação
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.

Exemplo
<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

NomeTipoDescrição
themeRef<'light' | 'dark' | 'system'>Preferência escolhida pelo usuário.
resolvedThemeComputedRef<'light' | 'dark'>Tema efetivo, resolvendo `system` com prefers-color-scheme.
setTheme(t: 'light' | 'dark' | 'system') => voidAplica o tema no alvo e persiste se houver storageKey.
toggleTheme() => voidAlterna 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

VTheme

União 'light' | 'dark' | 'system'.

ts
export type VTheme = 'light' | 'dark' | 'system'
UseThemeOptions

target, storageKey e default controlam onde e como o tema é persistido.

ts
export interface UseThemeOptions {
  
  target?: HTMLElement | (() => HTMLElement | null)
  
  storageKey?: string | false
  
  default?: VTheme
}
UseThemeReturn

theme, resolvedTheme, setTheme e toggleTheme.

ts
export interface UseThemeReturn {
  theme: Ref<VTheme>
  resolvedTheme: ComputedRef<'light' | 'dark'>
  setTheme: (t: VTheme) => void
  toggleTheme: () => void
}
useTheme

Sincroniza a preferência de tema com data-theme e, opcionalmente, com o localStorage.

ts
useTheme(options: UseThemeOptions = {}): UseThemeReturn

Fonte: src/composables/useTheme.ts