qsa-observer
Given an array of selectors, handles any matching element that was wither connected, or disconnected, from an observed element, or document.
Differently from MutationObserver
- each element is granted to be handled, whenever it's observed via one, or more, selectors
- all observed elements will pass through
handle(element, selector)
utility, per each matching selector - elements injected through
innerHTML
, or created offline, will be handled too once live
Example
import QSAO from 'qsa-observer';
const query = ['div'];
const root = document;
const {
flush,
observer,
parse
} = QSAO({
query,
root,
handle(element, connected, index) {
const event = connected ? 'connected' : 'disconnected';
console.log(element, event, query[index]);
}
});
const observeMore = selector => {
if (!query.includes(selector)) {
query.push(selector);
parse(root.querySelectorAll(selector));
}
};
setTimeout(observeMore, 1000, 'p');