Skip to content

Commit

Permalink
feat(runtime): allow mutation observer optimization (#3030)
Browse files Browse the repository at this point in the history
  • Loading branch information
exside committed Aug 31, 2023
1 parent 7d063a3 commit 4f4148e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ export interface RuntimeGenerateResult extends GenerateResult {
getStyleElements(): Map<string, HTMLStyleElement>
}

export interface RuntimeObserverConfig {
/**
* A function that returns an HTML Element for the MutationObserver to watch.
*/
target?: () => Element
/**
* An array of attribute names for the MutationObserver to limit which attributes
* are watched for mutations.
*/
attributeFilter?: Array<string>
}

export interface RuntimeOptions {
/**
* Default config of UnoCSS
Expand All @@ -30,6 +42,10 @@ export interface RuntimeOptions {
* Callback when the runtime is ready. Returning false will prevent default extraction
*/
ready?: (runtime: RuntimeContext) => false | any
/**
* Runtime MutationObserver configuration options
*/
observer?: RuntimeObserverConfig
/**
* When enabled, UnoCSS will look for the existing selectors defined in the stylesheet and bypass them.
* This is useful when using the runtime alongwith the build-time UnoCSS.
Expand Down Expand Up @@ -251,13 +267,14 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
function observe() {
if (observing)
return
const target = html() || defaultDocument.body
const target = runtimeOptions.observer?.target ? runtimeOptions.observer.target() : (html() || defaultDocument.body)
if (!target)
return
mutationObserver.observe(target, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: runtimeOptions.observer?.attributeFilter,
})
observing = true
}
Expand Down

0 comments on commit 4f4148e

Please sign in to comment.