Overview
MutationObserver composable for detecting DOM changes. Wraps the MutationObserver API with lifecycle management.Features
- MutationObserver API wrapper
- Pause/resume/stop functionality
- Automatic cleanup on unmount
- SSR-safe (checks SUPPORTS_MUTATION_OBSERVER)
- Hydration-aware
- Configurable observation options
Function Signature
Parameters
The element to observe.
The callback to execute when a mutation is observed.
Call the callback immediately with synthetic data.
Stop observing after first mutation.
Observe additions and removals of child nodes.
Observe changes to attributes.
Observe changes to character data.
Observe mutations in descendants.
Record the previous value of changed attributes.
Record the previous value of changed character data.
Array of attribute names to observe.
Return Value
Whether the observer is currently active.
Whether the observer is currently paused.
Pause observation.
Resume observation.
Stop observation and clean up.
Examples
Observe child list changes
Observe attribute changes
Observe specific attributes
Observe entire subtree
Pause and resume
Once mode
Track DOM changes in a list
Mutation Record Properties
The type of mutation.
The node affected by the mutation.
Nodes added to the tree.
Nodes removed from the tree.
The previous sibling of added or removed nodes.
The next sibling of added or removed nodes.
The name of the changed attribute.
The namespace of the changed attribute.
The previous value (requires
attributeOldValue or characterDataOldValue).Use Cases
- Dynamic content tracking: Monitor when content is added or removed
- Attribute monitoring: Track changes to class names, data attributes, etc.
- DOM validation: Ensure DOM structure meets requirements
- Performance monitoring: Detect excessive DOM mutations
- Debug tools: Log DOM changes during development
- Rich text editors: Track content changes in contentEditable elements
Notes
- Automatically stops observing when
once: trueand a mutation occurs - Hydration-aware: defers setup until client-side hydration completes
- SSR-safe: returns valid API with
isActive: falsewhen MutationObserver is not supported immediateoption provides synthetic data on first mount- Default observes
childListonly; explicitly enable other options as needed