useMutationObserver
A composable for detecting DOM changes using the Mutation Observer API with automatic cleanup.
Usage
The useMutationObserver composable wraps the Mutation Observer API to detect changes to the DOM tree. It’s useful for monitoring attribute changes, child element modifications, and character data updates.
UseMutationObserver
<script setup lang="ts">
import { useMutationObserver } from '@vuetify/v0'
import { ref, useTemplateRef } from 'vue'
const target = useTemplateRef('target')
const mutationCount = ref(0)
useMutationObserver(target, (mutations) => {
mutationCount.value += mutations.length
mutations.forEach(mutation => {
console.log('Type:', mutation.type)
console.log('Added nodes:', mutation.addedNodes)
console.log('Removed nodes:', mutation.removedNodes)
})
}, {
childList: true,
attributes: true,
attributeOldValue: true
})
</script>
<template>
<div>
<div ref="target">
<p>Mutations detected: {{ mutationCount }}</p>
</div>
</div>
</template>Architecture
useMutationObserver wraps the native MutationObserver API with Vue reactivity:
The following API details are for the useMutationObserver composable.
Functions
useMutationObserver
(target: MaybeRef<Element>, callback: (entries: MutationObserverRecord[]) => void, options?: UseMutationObserverOptions) => UseMutationObserverReturnA composable that uses the Mutation Observer API to detect changes in the DOM.
Options
immediate
booleanonce
booleanchildList
booleanattributes
booleancharacterData
booleansubtree
booleanattributeOldValue
booleancharacterDataOldValue
booleanattributeFilter
string[]Properties
isActive
Readonly<Ref<boolean, boolean>>Whether the observer is currently active (created and observing)
Methods
Was this page helpful?
