Skip to content
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

Merged
merged 5 commits into from
Aug 31, 2023

Conversation

exside
Copy link
Contributor

@exside exside commented Aug 24, 2023

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 array

both 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:

		...,
		runtime: {
			observer: {
				target: () => document.querySelector('.your-area-of-the-site-to-watch-for-mutations'),
				attributeFilter: ['class'], // only watch for changes in class attribute not all attributes
			},
		},
		...

the attributeFilter option for observe 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 } : {}),

@netlify
Copy link

netlify bot commented Aug 24, 2023

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 28db3fd
🔍 Latest deploy log https://app.netlify.com/sites/unocss/deploys/64ec19abbbc3ab0008d28913
😎 Deploy Preview https://deploy-preview-3030--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -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);
Copy link
Collaborator

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

Copy link
Contributor Author

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 😬

@exside exside requested a review from chu121su12 August 25, 2023 19:26
@ydcjeff ydcjeff changed the title Allow runtime mutation observer optimization feat(runtime): allow mutation observer optimization Aug 26, 2023
Copy link
Member

@ydcjeff ydcjeff left a 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.

@antfu antfu merged commit 4f4148e into unocss:main Aug 31, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants