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:
main.ts
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:
UseTheme
<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:
The following API details are for the useTheme composable.
Functions
createThemeContext
(_options?: ThemeContextOptions) => ContextTrinity<R>Creates a new theme context trinity.
Options
Properties
selectedId
ComputedRef<any>selectedIndex
ComputedRef<number>selectedItem
ComputedRef<E>selectedValue
ComputedRef<E["value"]>colors
ComputedRef<Record<string, Colors>>A computed reference to the resolved colors of the current theme.
Methods
Was this page helpful?
