Skip to content
Browser

useStyleTag

Dynamically inject and remove a <style> tag in document.head. The tag is appended on mount and removed on unmount. Supports reactive CSS updates via css$ and optional manual lifecycle management.

useStyleTag
Not injected

Dynamically inject and remove CSS styles in document.head.

Styled by injected CSS
import {
const useStyleTag: (css: MaybeObservable<string>, options?: DeepMaybeObservable<UseStyleTagOptions>) => UseStyleTagReturn
useStyleTag
} from "@usels/web";
function
function Component(): JSX.Element
Component
() {
const {
const isLoaded$: ReadonlyObservable<boolean>
isLoaded$
} =
function useStyleTag(css: MaybeObservable<string>, options?: DeepMaybeObservable<UseStyleTagOptions>): UseStyleTagReturn

Framework-agnostic dynamic <style> tag injector. Appends a style element into document.head and keeps its textContent in sync with css$.

@paramcss - Initial CSS content (MaybeObservable<string> — kept in sync with css$).

@paramoptions - Mount-time-only options (id, media, immediate, manual, nonce, document).

useStyleTag
("body { background: #f0f0f0; }");
return <
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>{
const isLoaded$: ReadonlyObservable<boolean>
isLoaded$
.
ImmutableObservableBase<boolean>.get(trackingType?: TrackingType | GetOptions): {}
get
() ? "Styles injected" : "Loading..."}</
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>;
}

Update css$ at any time to change the injected styles without re-mounting.

import { useStyleTag } from "@usels/web";
function Component() {
const { css$, isLoaded$ } = useStyleTag(".box { color: red; }");
return (
<div>
<button onClick={() => css$.set(".box { color: blue; }")}>Change color</button>
<div className="box">Styled box</div>
</div>
);
}

If you already have an Observable holding your CSS string, pass it directly.

import { observable } from "@usels/core";
import { useStyleTag } from "@usels/web";
const theme$ = observable("body { background: white; }");
function Component() {
useStyleTag(theme$);
// Updating theme$ automatically updates the injected style tag
}

Set manual: true to take full control of when the style tag is injected and removed.

import { useStyleTag } from "@usels/web";
function Component() {
const { load, unload, isLoaded$ } = useStyleTag(".box { color: red; }", {
manual: true,
});
return (
<div>
<button onClick={load}>Inject</button>
<button onClick={unload}>Remove</button>
<p>{isLoaded$.get() ? "Injected" : "Not injected"}</p>
</div>
);
}
import { useStyleTag } from "@usels/web";
useStyleTag(".box { color: red; }", {
id: "my-style", // custom id for the style element
media: "print", // CSS media query
nonce: "abc123", // Content Security Policy nonce
immediate: true, // default: true — inject on mount
manual: false, // default: false — auto lifecycle management
});
ParameterTypeDescription
cssMaybeObservable<string>- Initial CSS content (MaybeObservable<string> — kept in sync with css$).
optionsUseStyleTagOptions (optional)- Mount-time-only options (id, media, immediate, manual, nonce, document).
OptionTypeDefaultDescription
idstringauto-generatedStyle tag id
mediastring-CSS media query
immediatebooleantrueLoad the style tag immediately on mount
manualbooleanfalseManual mode — load/unload are not called automatically on mount/unmount
noncestring-Nonce attribute for Content Security Policy
documentDocument-Custom document instance (useful for iframes or testing)

UseStyleTagReturn

NameTypeDescription
idstring-
css$ObservablePrimitive<string>CSS content — update to change injected styles dynamically
isLoaded$ReadonlyObservable<boolean>-
load() => void-
unload() => void-

View on GitHub