Wraps a CSSStyleSheet to provide convenient methods for styling and 'unstyling' elements and manipulating the CSSStyleSheet.

Example

import { StyleSheet } from 'deleight/css';
const style = new StyleSheet(css);

Constructors

Properties

Methods

Constructors

  • Creates a new instance of StyleSheet to wrap the given CSSStyleSheet.

    Parameters

    • cssStyleSheet: CSSStyleSheet

      The wrapped CSSStyleSheet

    Returns StyleSheet

Properties

css: CSSStyleSheet

The wrapped CSS stylesheet.

Methods

  • Styles the elements with the wrapped CSSStylesheet using the adoptedStyleSheets property of Document and ShadowRoot objects.

    If an element is neither a document nor a shadow root, an open shadow root is created for it and then the root is styled.

    Type Parameters

    • T extends Element | Document | ShadowRoot | DocumentFragment

    Parameters

    • Rest ...elements: T[]

      The elements, document fragments, shadow roots or documents to add style

    Returns void

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(css);
    style.add(...document.body.children)
  • Deletes the first style rule matching the given query. This uses the selectFirst function.

    Parameters

    • p: string

      The starting text of the style rule to delete.

    Returns void

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(cssStyleSheet);
    style.delete('div');
  • Deletes all css rules matching the given query.

    Parameters

    • p: string

      The starting text of the style rules to delete.

    Returns void

    Example

    import { StyleSheet, createStyle } from 'deleight/css'
    const css = 'p {border: none;}, a {text-decoration: none;}'
    const sheet = new StyleSheet(createStyle(css));
    sheet.deleteAll('a, p'); // sheet.css.cssRules === [];
  • Returns the first style rule matching the given query (or undefined if there is none). This uses the selectFirst function.

    Parameters

    • p: string

      The starting text of the style rule to get

    Returns CSSRule

    The selected style rule

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(cssStyleSheet);
    style.get('div');
    // CSSRule{...}
  • Returns all the style rules matching the specified query using the selectAll function.

    Parameters

    • p: string

      The starting text of the style rules to get.

    Returns Iterable<CSSRule>

    The selected dtyle rules

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(cssStyleSheet);
    [...style.getAll('p')];
  • Removes the wrapped stylesheet from the given documents, shadow roots or elements. For elements, the stylesheet is removed from their shadowRoot property.

    Type Parameters

    • T extends Element | Document | ShadowRoot | DocumentFragment

    Parameters

    • Rest ...elements: T[]

      The elements, document fragments, shadow roots or documents to add unstyle

    Returns void

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(css);
    style.add(...document.body.children)
    style.remove(document.body.children[0], document.body.children[5])
  • Replaces the cssText of the first style rule matching the given query with the given value. This uses the selectFirst function.

    Parameters

    • p: string

      The starting text of the style rule to update.

    • value: string | CSSRule

      The new value to overwrite existing CSS text with

    Returns string

    Example

    import { StyleSheet } from 'deleight/css';
    const style = new StyleSheet(cssStyleSheet);
    style.set('div', 'div {border: none;}');
  • Assigns the given values to the css rules matching the query (in the order they are specified in the query).

    Parameters

    • p: string

      The starting text of the style rules to update.

    • Rest ...values: (string | CSSRule)[]

      The values to replace matching rules with.

    Returns void

    Example

    import { StyleSheet, createStyle } from 'deleight/css'
    const css = 'p {border: none;}, a {text-decoration: none;}'
    const sheet = new StyleSheet(createStyle(css));
    sheet.setAll('a, p', 'a {color: white;}', 'p {color: black}');
    // sheet.css.cssRules.map(r => r.cssText) === ['p {color: black}', 'a {color: white;}''];