useTheme
The useTheme composable provides comprehensive theme management capabilities, allowing you to register multiple themes, switch between them dynamically, and automatically generate CSS custom properties. Built on createSingle for single-theme selection and createTokens for design token alias resolution.
Installation
Install the Theme plugin in your app’s entry point:
import { createApp } from 'vue'
import { createThemePlugin } from '@vuetify/v0'
import App from './App.vue'
const app = createApp(App)
app.use(
createThemePlugin({
default: 'light',
themes: {
light: {
dark: false,
colors: {
primary: '#3b82f6',
},
},
dark: {
dark: true,
colors: {
primary: '#675496',
},
},
},
})
)
app.mount('#app')Usage
Once the plugin is installed, use the useTheme composable in any component:
<script setup lang="ts">
import { useTheme } from '@vuetify/v0'
const theme = useTheme()
function toggleTheme() {
theme.cycle(['light', 'dark'])
}
</script>
<template>
<div>
<h1>Current Theme: {{ theme.selectedId }}</h1>
<p>Dark mode: {{ theme.isDark ? 'enabled' : 'disabled' }}</p>
<button @click="toggleTheme">Toggle Theme</button>
</div>
</template>Architecture
useTheme extends createSingle for theme selection and createTokens for color resolution:
Reactivity
Theme selection and computed colors are reactive. Switching themes automatically updates CSS variables.
| Property | Reactive | Notes |
|---|---|---|
selectedId | Current theme ID | |
selectedItem | Current theme ticket | |
selectedValue | Current theme colors | |
selectedIndex | Index in registry | |
colors | Resolved colors with aliases | |
isDark | Current theme is dark |
Functions
createThemeContext
<_E>(_options?: ThemePluginOptions | undefined) => ContextTrinity<_E>createThemePlugin
(_options?: ThemePluginOptions | undefined) => PluginuseTheme
<_E>(namespace?: string) => _EOptions
target
string | HTMLElement | null | undefinedThe target element or selector to apply theme classes to.
Properties
selectedValues
ComputedRef<Set<E["value"] extends Ref<infer U, infer U> ? U : E["value"]>>Computed Set of selected ticket values
selectedId
ComputedRef<ID | undefined>selectedIndex
ComputedRef<number>selectedItem
ComputedRef<E | undefined>selectedValue
ComputedRef<E["value"] | undefined>colors
ComputedRef<Record<string, Colors>>A computed reference to the resolved colors of the current theme.
Methods
move
(id: ID, toIndex: number) => E | undefinedSeek for a ticket based on direction and optional predicate
seek
(direction?: "first" | "last", from?: number, predicate?: (ticket) => boolean) => E | undefinedon
<K extends Extensible<RegistryEventName>>(event: K, cb: EventHandler<E, K>) => voidListen for registry events
off
<K extends Extensible<RegistryEventName>>(event: K, cb: EventHandler<E, K>) => voidStop listening for registry events
emit
<K extends Extensible<RegistryEventName>>(event: K, data: EventPayload<E, K>) => voidEmit an event with data
batch
<R>(fn: () => R) => RExecute operations in a batch, deferring cache invalidation and event emission until complete