fn

useStyleTag

v0.0.14testeddemo

Inject a reactive <style> tag into the document <head>. The CSS is a writable ref — assigning to it updates the live stylesheet. Multiple instances sharing an id reuse a single element via reference counting, and everything is SSR-safe.

Examples

ts
const { css, isLoaded } = useStyleTag('body { color: red }');
css.value = 'body { color: blue }';
ts
// Manual control
const { load, unload } = useStyleTag('.a { color: red }', { manual: true });
load();
unload();

Demo

Loading demo…

Signature

ts
export function useStyleTag(
  css: MaybeRefOrGetter<string>,
  options: UseStyleTagOptions ={ ... }

Parameters

ParameterTypeDescription
cssMaybeRefOrGetter<string>Reactive CSS source for the tag
options?UseStyleTagOptionsOptions

Returns

UseStyleTagReturn{ id, css, load, unload, isLoaded }
PropertyTypeDescription
idstringDOM id of the injected <style> tag.
cssShallowRef<string>Reactive, writable CSS text content of the tag.
load() => voidInject the <style> tag into <head> (or reuse an existing one).
unload() => voidRemove the <style> tag from <head> when no other instance references it.
isLoadedReadonly<ShallowRef<boolean>>Whether the tag is currently loaded.