-
-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(runtime): allow mutation observer optimization #3030
Conversation
✅ Deploy Preview for unocss ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
packages/runtime/src/index.ts
Outdated
@@ -251,13 +251,14 @@ export default function init(inlineConfig: RuntimeOptions = {}) { | |||
function observe() { | |||
if (observing) | |||
return | |||
const target = html() || defaultDocument.body | |||
if (!target) | |||
const target = () => runtimeOptions.observer?.target ? runtimeOptions.observer.target() : (html() || defaultDocument.body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we don't need target()
as function?
Also please add the new prop types definition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the target()
as a function, now a regular const
Tried to add the type defs, hope it's ok 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally LTGM.
Need to wait antfu for final review.
This is probably a more advanced usage config, but as it doesn't affect default behaviour I think it's a good addition with minimal bundle size impact.
The runtime's MutationObserver is basically watching the entire DOM for all mutations on all nodes and attributes, that's probably fine for most, but I would prefer if I could fine tune Uno CSS runtime so it does the least amount of work necessary, therefore I propose an addition to the runtime options, which allow granular control over:
a) what area of the DOM is monitored by returning a node to watch (including its child nodes) with
target
b) which attributes to watch for mutations by providing an
attributeFilter
arrayboth config options are of course optional, default behaviour stays exactly as it is now and are summarized under
observer
So now we could do this:
the attributeFilter option forobserve
can only be included if defined, we can't just specify an empty array[]
as default, then the observer will watch no attribute at all, therefore this slightly ugly construct:...(runtimeOptions.observer?.attributeFilter ? { attributeFilter: runtimeOptions.observer.attributeFilter } : {}),