Custom element lifecycle callbacks
A custom <x-ell> watches its rows attribute and updates its line clamp. Change the property, or detach and reconnect the same element to see its lifecycle.
Only attributes listed in observedAttributes trigger attributeChangedCallback. Adoption into another document is included in the code but is not triggered by these controls.
MutationObserver: collect changes, then react
Watch attributes, text-node edits, and child replacements. Several synchronous mutations can arrive in one callback. This example observes a plain element so it stays independent of the custom element above.
Editing Text.data produces characterData; assigning textContent replaces children and produces childList. The callback renders the owning element because a text mutation’s target is a Text node.
Mutation events Legacy · simulation
Legacy mutation events ran synchronously. They are obsolete and should not be used for new implementations. This explicitly labeled simulation uses a custom event to illustrate removal timing, alongside a real MutationObserver.
The custom “demo:before-remove” event is manually dispatched; it is not native DOMNodeRemoved detection. The observer receives an empty NodeList for additions and a NodeList containing the removed node.
Object.defineProperty: a setter with a side effect
A setter can render immediately when you assign element.rows. It does not observe arbitrary DOM changes: setting the attribute directly bypasses the setter. Try both routes.
Accessor descriptors use get and set. Do not include writable with an accessor descriptor; “writeable” from the original snippet is not a recognized descriptor key.
CSS animation as a selector-based signal
New matching nodes receive a short animation. A delegated animationend listener detects its completion. This is an indirect signal, not a general-purpose observer: it provides no mutation records and cannot reliably report arbitrary removals.
This imperceptible animation changes no layout. Removing a node before its animation completes can prevent animationend from firing.