/** * Based on CSS Tricks tutorial. * @see https://css-tricks.com/how-to-get-all-custom-properties-on-a-page-in-javascript/ * * The names of the custom properties are collected from the CSSOM, but their values are resolved * via the computed style of a matching element. * The CSSOM is only a reliable source for the names: declarations can be nested inside grouping * rules (`@layer`, `@media`, `@supports`, `@container`), and their document order does not * represent the cascade anymore, e.g. unlayered declarations win over layered ones. * * If the CSSOM does not provide any name, then the names can optionally be read from the computed * style of the element as well, see the `useComputedStyleFallback` option. */ type CustomPropertyEntry = [string, string]; interface getLocalCssStyleRulesProps { cssRuleType?: "CSSStyleRule"; /** * Selector the rule needs to use, e.g. `:root`. * A rule matches if the selector is part of its selector list, e.g. `:root, :host`. */ selectorText?: string; } interface getLocalCssStyleRulePropertiesProps extends getLocalCssStyleRulesProps { propertyType?: "all" | "normal" | "custom"; } interface getCustomPropertiesProps extends getLocalCssStyleRulesProps { filterName?: (name: string) => boolean; removeDashPrefix?: boolean; returnObject?: boolean; /** * Read the property names from the computed style of the matching element if the CSSOM does not * provide any name, e.g. because the declarations are part of a stylesheet that cannot be read * or that is not listed by `document.styleSheets`, like constructed and adopted stylesheets. * * Disabled by default because it changes the result set: the computed style of an element also * contains all custom properties it inherits from its ancestors, e.g. everything defined for * `:root`, and it does not tell which rule declared them. */ useComputedStyleFallback?: boolean; } export default class CssCustomProperties { getterDefaultProps: getCustomPropertiesProps; customprops: CustomPropertyEntry[] | Record; constructor(props?: getCustomPropertiesProps); customProperties: (props?: getCustomPropertiesProps) => CustomPropertyEntry[] | Record; static listLocalStylesheets: () => CSSStyleSheet[]; /** Rules of a stylesheet are not readable if it was loaded from another origin. */ static readCssRules: (stylesheet: CSSStyleSheet) => CSSRuleList | undefined; static listLocalCssRules: () => CSSRule[]; static isCssStyleRule: (rule: CSSRule) => rule is CSSStyleRule; static matchesSelectorText: (rule: CSSStyleRule, selectorText: string) => boolean; static listLocalCssStyleRules: (filter?: getLocalCssStyleRulesProps) => CSSStyleRule[]; /** * Return the property names of a style declaration. * The declaration is not iterated directly because it is not always an iterable object, e.g. * the declarations of style rules are not iterable in test environments using jsdom. */ static listStyleDeclarationPropertyNames: (style: CSSStyleDeclaration) => string[]; static listLocalCssStyleRuleProperties: (filter?: getLocalCssStyleRulePropertiesProps) => string[][]; /** * Return the element the values of custom properties can be read from. * `:root` and `html` are mapped to the root element of the document, for any other selector the * first matching element is used. * If nothing matches and the selector consists of class names only, then a temporary hidden * element is created; the second item of the returned tuple removes it again. */ static targetElement: (selectorText?: string) => [Element | undefined, (() => void) | undefined]; /** * Return the names of all custom properties that apply to an element, they are read from its * computed style. * Inherited custom properties are part of the computed style, so the returned list also contains * the names of custom properties that were declared for one of the ancestors of the element. */ static listElementCustomPropertyNames: (element: Element) => string[]; /** * Resolve the values of custom properties as they are applied to an element. * Properties without a value are removed, they do not apply to the element, e.g. because they * are only defined inside a currently not matching `@media` rule. */ static resolveCustomPropertyValues: (element: Element, propertyNames: string[]) => CustomPropertyEntry[]; static listCustomProperties: (props?: getCustomPropertiesProps) => CustomPropertyEntry[] | Record; } export {};