kubetail-org / sentineljs

Detect new DOM nodes using CSS selectors (650 bytes)
MIT License
1.13k stars 51 forks source link

Mutation Observer? #7

Closed timwis closed 6 years ago

timwis commented 6 years ago

Hey, clever library! I was looking for something like this to facilitate sharing components between JS SPAs and WordPress sites.

  1. May I suggest a paragraph in the readme that explains how it works and whether there are any tradeoffs? I figured it out from the source but it would have been great to see a sentence or two summarizing it.

  2. Did you consider using a mutation observer to detect new elements instead of animate events? Looks like this has slightly more browser support but mutation observer is pretty good.

amorey commented 6 years ago

Thanks for the suggestion! Adding more details on how it works sounds like a good idea.

I tried implementing this with Mutation Observers but was unable to make it work. As far as I can tell the way to implement this with Mutation Observers is to add an observer to the document element with childList=true, subtree=true and iterate (recursively) through every new node added to the DOM. This might work for sites that aren't very dynamic but in general the @keyframes method should be more performant. Of course I'd like to be proven wrong because I do believe we need built-in browser support for detecting new nodes at arbitrary points in the DOM tree in a performant way!

amorey commented 6 years ago

Let me know if this is helpful: https://github.com/muicss/sentineljs/blob/master/README.md#introduction

timwis commented 6 years ago

Looks great, thanks!