Skip to content

useCssVar

Category
Export Size
796 B
Last Changed
8 months ago

Manipulate CSS variables

Demo

Sample text, #7fa998
Sample text, --color: #7fa998

Usage

ts
import { 
useCssVar
} from '@vueuse/core'
import {
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
('el')
const
color1
=
useCssVar
('--color',
el
)
const
elv
=
useTemplateRef
('elv')
const
key
=
ref
('--color')
const
colorVal
=
useCssVar
(
key
,
elv
)
const
someEl
=
useTemplateRef
('someEl')
const
color2
=
useCssVar
('--color',
someEl
, {
initialValue
: '#eee' })

Type Declarations

ts
export interface UseCssVarOptions extends ConfigurableWindow {
  
initialValue
?: string
/** * Use MutationObserver to monitor variable changes * @default false */
observe
?: boolean
} /** * Manipulate CSS variables. * * @see https://vueuse.org/useCssVar * @param prop * @param target * @param options */ export declare function
useCssVar
(
prop
:
MaybeRefOrGetter
<string | null | undefined>,
target
?:
MaybeElementRef
,
options
?: UseCssVarOptions,
):
ShallowRef
<string | undefined, string | undefined>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
Robin
丶远方
Antério Vieira
SerKo
babu-ch
Fernando Fernández
Waleed Khaled
木荣
JD Solanki
btea
Alex Kozack

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
d680f - fix: update variable when initial (#4641)
v12.6.0 on
d5dd8 - fix: new Behavior (#4500)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.2 on
d0523 - fix: Replace getComputedStyle with direct style property access (#4166)
v11.0.0-beta.2 on
fe19c - feat(useCssVars): remove property on null/undefined (#3821)
v10.2.0 on
9d34a - fix: observe changes based on element class list (#3131)

Released under the MIT License.

Black Friday Super Early Bird
Get 60% off + 9 premium Vue mastery bonuses
The most generous offer we’ve ever made
Buy Now
TIME LIMITED OFFER