useStyle

Commons
stable

Injects a dynamic <style> tag into document.head scoped to a generated element id, allowing components to apply per-instance CSS that cannot be expressed as inline styles (e.g. pseudo-element rules, :hover selectors, @keyframes). Also exposes the lower-level useStyleTag for raw CSS string injection. Both composables manage their own lifecycle: they load on mount and unload on scope dispose.

API

Signature

Full TypeScript signature sourced from the composable file.

typescript

useStyle targets the injected styles with a #id selector, so only the specific DOM element with that id is affected — it is component-scoped without the overhead of a Shadow DOM. The id must be applied to the element via :id="id" in the template.

Consumed interfaces

IStyleTagOptions

API

Parameters

Input parameters accepted by this composable.

styles
required
ComputedRef
A ComputedRef returning a CSS value array or object. Accepts the same formats as Vue :style bindings (array of objects, strings, or mixed). useStyle normalises this to a flat `#id { prop: value; … }` string and feeds it to useStyleTag.
uniq
MaybeRefOrGetter<string | undefined>
Optional fixed element id. When omitted, a unique id is generated from the component name and a counter (e.g. origam-card-3). Provide a fixed id when multiple instances must share the same <style> tag (e.g. global animation keyframes).
name
string
getCurrentInstanceName()
Component name prefix for the generated id. Defaults to the current Vue instance name.

API

Return values

Properties and methods exposed by the composable return object.

id
The element id used as the CSS scope selector (#id). Apply this id to the root element of the component so the injected styles target only this instance.
css
The current generated CSS string. Reactive — updates whenever the styles ComputedRef changes.
load / unload
Manually control style tag injection. Normally called automatically on mount / scope dispose. Call load() to re-inject after an explicit unload().
isLoaded
True when the <style> tag is currently present in document.head.

Usage

Examples

Ready-to-paste usage snippets.

Per-instance scoped keyframes

vue

Raw CSS injection with useStyleTag

vue