use-css-var
Read and write a CSS custom property reactively.
| Package | @vizejs/composable/use-css-var |
| Own the source | vize lib pull composable:use-css-var |
| Runtime exports | useCssVar |
| Gzip budget | 2048 B |
Minimal setup
Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).
vp install @vizejs/composable
Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.
import { useCssVar } from "@vizejs/composable/use-css-var";
Try it
Theme a card with a CSS variable
Read and write a CSS custom property on one element from script.
What to observe: Choose Blue accent: the border and the printed value change to #3b82f6. Other elements keep their own value.
Pass the element ref as the target; without one the variable is set on :root. initialValue applies before the element mounts.
Compare the initial and interacted states


Copy the example
Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.
<script setup lang="ts">
import { useTemplateRef } from "vue";
import { useCssVar } from "@vizejs/composable/use-css-var";
const card = useTemplateRef<HTMLElement>("card");
const { value: accent } = useCssVar("--accent", card, { initialValue: "#42b883" });
const colors = [
{ label: "Green", value: "#42b883" },
{ label: "Blue", value: "#3b82f6" },
{ label: "Orange", value: "#f97316" },
];
</script>
<template>
<div class="composable-example">
<p>Theme one card by changing a CSS custom property from script.</p>
<div class="example-actions">
<button
v-for="color in colors"
:key="color.label"
type="button"
@click="() => (accent = color.value)"
>
{{ color.label }} accent
</button>
</div>
<article ref="card" class="accent-card">
Accent: <output aria-live="polite">{{ accent }}</output>
</article>
</div>
</template>
<style scoped>
.accent-card {
border: 3px solid var(--accent);
padding: 12px;
}
</style>
Runtime contract
| Utility | Category | Stability | SSR | Hydration | Cleanup | Targets | Host globals | Uses |
|---|---|---|---|---|---|---|---|---|
useCssVar |
dom | experimental | deterministic-fallback | caller-managed | reactive-scope | web, desktop | window |
— |
API
useCssVar
Read and write a CSS custom property reactively. The value is read from the target's computed style whenever the reactive name, target, or host changes (and, with observe, when its style/class attribute changes). Assigning value writes an inline style.setProperty; assigning "" removes the inline declaration. Values read from the element are never written back, so a stylesheet-provided variable is not frozen into inline style. Server rendering: nothing is read or written; value holds initialValue. Keep server markup and the first client render identical by rendering from initialValue until mounted. The observer and watchers are released with the owning reactive scope.
function useCssVar( name: MaybeRefOrGetter<string>, target?: MaybeRefOrGetter<CssVarTarget | null | undefined>, options: UseCssVarOptions = {}, ): CssVarControls
const accent = useCssVar("--accent", rootRef, { initialValue: "#0af" });
accent.value.value = "#f60";
Types
CssVarStyle
Style declaration subset used by useCssVar.
| Member | Type | Description |
|---|---|---|
getPropertyValue |
(name: string) => string |
Read a property value. |
setProperty |
(name: string, value: string | null) => void |
Write a property value. |
removeProperty |
(name: string) => string |
Remove a property. |
CssVarTarget
Element subset used by useCssVar.
| Member | Type | Description |
|---|---|---|
style |
CssVarStyle |
Inline style declaration. |
CssVarObserver
Minimal MutationObserver instance.
| Member | Type | Description |
|---|---|---|
observe |
(target: CssVarTarget, options: { attributes: true; attributeFilter: string[] }) => void |
Start observing target. |
disconnect |
() => void |
Stop observing. |
CssVarHost
Capabilities used by useCssVar.
| Member | Type | Description |
|---|---|---|
getComputedStyle |
(target: CssVarTarget) => Pick<CssVarStyle, "getPropertyValue"> |
Resolved (computed) style of a target. |
documentElement |
CssVarTarget |
Target used when none is given (the document root element). |
createObserver? |
(callback: () => void) => CssVarObserver |
Observer factory used when observe is enabled. |
UseCssVarOptions
Options for useCssVar.
| Member | Type | Description |
|---|---|---|
initialValue? |
string |
Value used while the variable is unset and during server rendering. |
observe? |
boolean |
Re-read the variable when the target's style or class attribute changes (MutationObserver). |
host? |
MaybeRefOrGetter<CssVarHost | null | undefined> |
Style capability for alternate runtimes and tests. |
CssVarControls
Reactive state returned by useCssVar.
| Member | Type | Description |
|---|---|---|
value |
Ref<string> |
Writable variable value; assignments are written as inline style. |
supported |
ComputedRef<boolean> |
Whether a style capability and target are attached. |