Vue API Reference
Complete API reference for @tokiforge/vue package.
provideTheme()
Provide theme context to child components.
function provideTheme<T extends DesignTokens = DesignTokens>(
config: ThemeConfig,
options?: ThemeInitOptions
): ThemeContext<T>Parameters:
config: ThemeConfig- Theme configurationoptions?: ThemeInitOptions- Initialization options
Returns: ThemeContext<T> - Theme context object
Example:
<script setup>
import { provideTheme } from '@tokiforge/vue';
const { theme, tokens, setTheme, availableThemes } = provideTheme({
themes: [
{
name: 'light',
tokens: {
color: {
primary: { value: '#3b82f6' },
background: { value: '#ffffff' },
},
},
},
{
name: 'dark',
tokens: {
color: {
primary: { value: '#60a5fa' },
background: { value: '#1f2937' },
},
},
},
],
defaultTheme: 'light',
}, {
mode: 'dynamic',
persist: true,
selector: ':root',
prefix: 'app',
});
</script>
<template>
<div>
<p>Current theme: {{ theme }}</p>
<button @click="setTheme('dark')">Switch to Dark</button>
</div>
</template>useTheme()
Access theme context in child components.
function useTheme<T extends DesignTokens = DesignTokens>(): ThemeContext<T>Returns: ThemeContext<T> - Theme context object
Throws:
- Error if called outside of theme provider
Example:
<script setup>
import { useTheme } from '@tokiforge/vue';
const { theme, tokens, setTheme, nextTheme } = useTheme();
const handleThemeToggle = async () => {
await nextTheme();
};
</script>
<template>
<button @click="handleThemeToggle">
Toggle Theme (Current: {{ theme }})
</button>
</template>ThemeContext
Returned by provideTheme() and useTheme().
Properties
theme
theme: Ref<string>Reactive reference to current theme name.
Example:
<script setup>
const { theme } = useTheme();
</script>
<template>
<p>Active theme: {{ theme }}</p>
</template>tokens
tokens: Ref<T>Reactive reference to current theme tokens.
Example:
<script setup>
const { tokens } = useTheme();
</script>
<template>
<div :style="{ color: tokens.color.primary.value }">
Styled with tokens
</div>
</template>availableThemes
availableThemes: ComputedRef<string[]>Computed ref of available theme names.
Example:
<script setup>
const { availableThemes, setTheme } = useTheme();
</script>
<template>
<select @change="setTheme($event.target.value)">
<option v-for="t in availableThemes" :key="t" :value="t">
{{ t }}
</option>
</select>
</template>Methods
setTheme()
setTheme(themeName: string): Promise<void>Switch to a different theme.
Parameters:
themeName: string- Theme to switch to
Throws:
- Error if theme doesn't exist
Example:
<script setup>
const { setTheme } = useTheme();
const switchToDark = async () => {
await setTheme('dark');
console.log('Theme switched!');
};
</script>nextTheme()
nextTheme(): Promise<void>Switch to the next theme in rotation.
Example:
<script setup>
const { nextTheme } = useTheme();
</script>
<template>
<button @click="nextTheme">Next Theme</button>
</template>generateCSS()
generateCSS(themeName?: string): stringGenerate CSS for a theme (static mode only).
Parameters:
themeName?: string- Theme name (defaults to current)
Returns: string - CSS output
Example:
<script setup>
const { generateCSS } = useTheme();
const downloadCSS = () => {
const css = generateCSS('dark');
const blob = new Blob([css], { type: 'text/css' });
// ... download logic
};
</script>ThemeInitOptions
Configuration options for theme initialization.
interface ThemeInitOptions {
selector?: string; // CSS selector (default: ':root')
prefix?: string; // Variable prefix (default: 'hf')
mode?: 'dynamic' | 'static'; // Mode (default: 'dynamic')
persist?: boolean; // LocalStorage persistence (default: true)
watchSystemTheme?: boolean; // Watch system theme (default: false)
bodyClassPrefix?: string; // Class prefix for static mode (default: 'theme')
defaultTheme?: string; // Override default theme
}mode Options
Dynamic Mode (Recommended)
<script setup>
provideTheme(config, {
mode: 'dynamic',
selector: ':root',
prefix: 'app',
});
</script>Features:
- CSS variables injected at runtime
- Theme switching without page reload
- Full token access in components
- Smooth transitions
Best for: SPAs, complex applications, frequent theme changes
Static Mode
<script setup>
provideTheme(config, {
mode: 'static',
bodyClassPrefix: 'theme',
});
</script>Features:
- Class-based theme switching
- Pre-generated CSS classes
- Smaller runtime overhead
Best for: Static sites, simple applications
Composable Patterns
Theme Switcher
<script setup>
import { useTheme } from '@tokiforge/vue';
const { theme, availableThemes, setTheme } = useTheme();
</script>
<template>
<div class="theme-switcher">
<button
v-for="t in availableThemes"
:key="t"
:class="{ active: theme === t }"
@click="setTheme(t)"
>
{{ t }}
</button>
</div>
</template>System Theme Sync
<script setup>
import { provideTheme } from '@tokiforge/vue';
import { watch } from 'vue';
const config = { /* ... */ };
const { runtime } = provideTheme(config, {
watchSystemTheme: true, // Auto-sync with system
});
</script>Lazy Loading Themes
<script setup>
import { provideTheme } from '@tokiforge/vue';
const config = {
themes: [
{
name: 'light',
tokens: lightTokens, // Loaded immediately
},
{
name: 'dark',
tokens: async () => {
const { default: darkTokens } = await import('./themes/dark');
return darkTokens;
},
},
],
};
provideTheme(config);
</script>Accessing Tokens in Script
<script setup>
import { useTheme, computed } from 'vue';
const { tokens } = useTheme();
const primaryColor = computed(() => tokens.value.color.primary.value);
console.log(primaryColor.value); // '#3b82f6'
</script>TypeScript Support
Full TypeScript support with generic types:
import { provideTheme, useTheme } from '@tokiforge/vue';
interface MyTokens {
color: {
primary: { value: string };
background: { value: string };
};
spacing: {
sm: { value: string };
md: { value: string };
};
}
// Type-safe theme context
const { tokens } = provideTheme<MyTokens>(config);
// Auto-complete and type checking
tokens.value.color.primary.value; // ✅ string
tokens.value.invalid; // ❌ Type errorSSR Support
Nuxt 3
<!-- app.vue -->
<script setup>
import { provideTheme } from '@tokiforge/vue';
import { useCookie } from '#app';
const themeCookie = useCookie('theme');
provideTheme(config, {
defaultTheme: themeCookie.value || 'light',
});
</script>Vue SSR
// server.ts
app.get('*', (req, res) => {
const themeCookie = req.cookies.theme || 'light';
const html = renderToString(app, {
theme: themeCookie,
});
res.send(html);
});