@3mo/mutation-observer
Version:
A lit controller and directive for observing mutations.
24 lines (23 loc) • 1.05 kB
JavaScript
import { AsyncDirective, directive, PartType } from '@a11d/lit';
class MutationDirective extends AsyncDirective {
constructor(partInfo) {
super(partInfo);
this.observer = new MutationObserver((...args) => { var _a; return (_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, ...args); });
if (partInfo.type !== PartType.ELEMENT) {
throw new Error('observeMutation can only be used on an element');
}
const part = partInfo;
this.element = part.element;
if (this.element instanceof HTMLSlotElement) {
this.element.addEventListener('slotchange', () => { var _a; return (_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, [], this.observer); });
}
}
render(callback, options = { childList: true }) {
this.callback = callback;
this.observer.observe(this.element, options);
}
disconnected() {
this.observer.disconnect();
}
}
export const observeMutation = directive(MutationDirective);