useStyle
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
Signature TypeScript complète extraite du fichier du composable.
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.
Interfaces consommées
API
Paramètres
Paramètres d'entrée acceptés par ce composable.
- stylesrequisComputedRef
- 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.
- uniqMaybeRefOrGetter<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).
- namestringgetCurrentInstanceName()
- Component name prefix for the generated id. Defaults to the current Vue instance name.
API
Valeurs de retour
Propriétés et méthodes exposées par l'objet retourné par le composable.
- 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
Exemples
Extraits de code prêts à coller.
Per-instance scoped keyframes
Raw CSS injection with useStyleTag