visulima css-style-inject

Inject style tag to document head.


[![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
---

Daniel Bannert's open source work is supported by the community on GitHub Sponsors

--- ## Install ```bash npm install @visulima/css-style-inject ``` ```bash yarn add @visulima/css-style-inject ``` ```bash pnpm add @visulima/css-style-inject ``` ## Example ```javascript import { cssStyleInject } from "@visulima/css-style-inject"; const css = ` body { margin: 0; } `; cssStyleInject(css, options); ``` ### Usage with Next.js (SSR) If using a library that uses `@visulima/css-style-inject` for CSS modules in Next.js, you need to inject styles during SSR, here's an example: ```jsx // file: pages/_document.js import React from "react"; import { SSR_INJECT_ID } from "@visulima/css-style-inject"; const SSRInjectStyles = () => { if (!globalThis[SSR_INJECT_ID]) return null; return ( <> {globalThis[SSR_INJECT_ID].map((module) => ( // React escapes text children, so `` would // turn `>` into `>` and break the CSS. Use dangerouslySetInnerHTML // to emit the CSS verbatim. // // SECURITY: the CSS is injected as raw HTML. Only do this with CSS you // trust, and guard against a literal `` sequence in the CSS // (e.g. reject/escape it) — otherwise it closes the tag early and // becomes an HTML-injection vector.