Class StyleSheetResolve
Constructors
constructor
- new StyleSheetResolve(
styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>,
options?: {
excludeSelectorParts?: Iterable<RegExp>;
includeCSSLayers?: Iterable<RegExp>;
includeSelectorPartSet?: Set<string>;
},
): StyleSheetResolve Parameters
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
The stylesheet element to parse or an existing parsed stylesheet Map.
Optional
options: {
excludeSelectorParts?: Iterable<RegExp>;
includeCSSLayers?: Iterable<RegExp>;
includeSelectorPartSet?: Set<string>;
}Options for parsing stylesheet.
Optional
excludeSelectorParts?: Iterable<RegExp>A list of RegExp instance used to exclude CSS selector parts from parsed stylesheet data.
Optional
includeCSSLayers?: Iterable<RegExp>A list of RegExp instance used to specifically include in parsing for specific allowed CSS layers if present in the stylesheet.
Optional
includeSelectorPartSet?: Set<string>A Set of strings to exactly match selector parts to include in parsed stylesheet data.
Returns StyleSheetResolve
- styleSheetOrMap: CSSStyleSheet | Map<string, { [key: string]: string }>
Accessors
frozen
- get frozen(): boolean
Returns boolean
Current frozen state; when true no more modifications are possible.
size
- get size(): number
Returns number
Returns the size / count of selector properties tracked.
Methods
clone
- clone(): StyleSheetResolve
Clones this instance returning a new
StyleSheetResolve
instance with a copy of the data.Returns StyleSheetResolve
Cloned instance.
entries
- entries(): MapIterator<[string, { [key: string]: string }]>
Entries iterator of selector / style properties objects.
Returns MapIterator<[string, { [key: string]: string }]>
Tracked CSS selector key / value iterator.
freeze
get
- get(
selector: string | Iterable<string>,
opts?: {
camelCase?: boolean;
depth?: number;
resolve?: string | Iterable<string>;
},
): { [key: string]: string } Gets all properties associated with the given selector(s). You may combine multiple selectors for a combined result. You may also provide additional selectors as the
resolve
option to substitute any CSS variables in the target selector(s).Parameters
- selector: string | Iterable<string>
A selector or array of selectors to retrieve.
Optional
opts: { camelCase?: boolean; depth?: number; resolve?: string | Iterable<string> }Options.
Optional
camelCase?: booleanWhen true, property keys will be in camel case.
Optional
depth?: numberResolution depth for CSS variable substitution. By default, the depth is the length of the provided
resolve
selectors, but you may opt to provide a specific depth even with multiple resolution selectors.Optional
resolve?: string | Iterable<string>Additional selectors as CSS variable resolution sources.
Returns { [key: string]: string }
Style properties object.
- selector: string | Iterable<string>
getProperty
- getProperty(
selector: string | string[],
property: string,
options?: { depth?: number; resolve?: string | string[] },
): string Gets a specific property value from the given
selector
andproperty
key. Try and use a direct selector match otherwise all keys are iterated to find a selector string that includesselector
.Parameters
- selector: string | string[]
Selector to find.
- property: string
Specific property to locate.
Optional
options: { depth?: number; resolve?: string | string[] }Options.
Optional
depth?: numberResolution depth for CSS variable substitution. By default, the depth is the length of the provided
resolve
selectors, but you may opt to provide a specific depth even with multiple resolution selectors.Optional
resolve?: string | string[]Additional selectors as CSS variable resolution sources.
Returns string
Style property value.
- selector: string | string[]
has
keys
- keys(): MapIterator<string>
Returns MapIterator<string>
Tracked CSS selector keys iterator.
merge
- merge(
source: StyleSheetResolve,
options?: { exactMatch?: boolean; strategy?: "override" | "preserve" },
): void Merges styles from another StyleSheetResolve instance into this one.
Parameters
- source: StyleSheetResolve
Another instance to merge from.
Optional
options: { exactMatch?: boolean; strategy?: "override" | "preserve" }Options.
Optional
exactMatch?: booleanOnly merge if selector part keys match exactly.
Optional
strategy?: "override" | "preserve"By default, the source overrides existing values. You may also provide a
preserve
strategy which only merges property keys that do not exist already.
Returns void
- source: StyleSheetResolve
Dynamically parses and indexes a
CSSStyleSheet
at runtime, exposing a selector-to-style mapping by individual selector parts. CSS variable resolution is also possible and this enables the ability to flatten and resolve complex nestedvar(--...)
chains defined across multiple selectors and layers.When retrieving specific selector styles via StyleSheetResolve.get and StyleSheetResolve.getProperty it is possible to provide additional parent selectors that may define scoped CSS variables. These parent variable definitions will be substituted in the target selector data allowing specific element scoping of CSS variables to be flattened.
Core features:
@layer
blocks.excludeSelectorParts
option.includeCSSLayers
option.includeSelectorPartSet
option..get()
and.getProperty()
.TODO: There are a few improvements to make including supporting CSS variable fallback resolution.
Example