add-css-constructed
v1.1.1
Published
add a CSS to document or specified target using constructed stylesheet with fallback
Readme
add-css-constructed
add a CSS to document or specified target using constructed stylesheet
Features
- Supports modern
adoptedStyleSheetsAPI - Works in both document and shadow DOM
- Zero dependencies
- TypeScript support included
- Universal compatibility (fallback to traditional style elements)
Installation
npm i add-css-constructedUsage
ES Module
import { addCSS } from 'add-css-constructed';
// Add CSS to document
const style = addCSS('body { color: red; }');
// Add CSS to shadow root
const shadowRoot = element.attachShadow({ mode: 'open' });
addCSS('div { padding: 10px; }', shadowRoot);CommonJS
const addCSS = require('add-css-constructed');
addCSS('.container { margin: 0 auto; }');Browser (UMD)
<script src="https://unpkg.com/add-css-constructed"></script>
<script>
addCSS('body { font-family: sans-serif; }');
</script>API
addCSS(css_code, target?)
Adds CSS styles to the specified target.
Parameters:
css_code(string): CSS code to injecttarget(Document|ShadowRoot|null): Target element, defaults to document
Returns:
CSSStyleSheet|HTMLStyleElement: The created stylesheet or style element
Throws:
Error: When no DOM is detected and no target provided
removeCSS(stylesheet, target?)
Removes CSS style sheet from document or specified target.
Returns:
boolean: True if the removal operation is successful
hasDOM()
Checks if DOM environment is available.
Returns:
boolean: True if DOM is available
isAdoptedStyleSheetsSupported()
Checks if adoptedStyleSheets API is supported.
Returns:
boolean: True if adoptedStyleSheets is supported
Browser Support
- Modern browsers with
adoptedStyleSheetssupport - All browsers with traditional
<style>element fallback
License
Unlicense - Free for any use.
