Skip to content

Observer Directives

Observer directives help you observe DOM changes and element resizing.

v-resize

Observe element resize using ResizeObserver.

Basic Usage

vue
<template>
  <div v-resize="handleResize">Resize me</div>
</template>

<script setup>
function handleResize(entry) {
  console.log('New size:', entry.contentRect.width, entry.contentRect.height)
}
</script>

API

OptionTypeDefaultDescription
handlerFunction-Resize event handler (required)
box'content-box' | 'border-box''content-box'Box model to observe
debouncenumber0Debounce time in milliseconds

Resize Info

The handler receives a ResizeObserverEntry with:

PropertyTypeDescription
contentRectDOMRectReadOnlyContent rectangle
borderBoxSizeArrayBorder box size
contentBoxSizeArrayContent box size

v-mutation

Observe DOM mutations using MutationObserver.

Basic Usage

vue
<template>
  <div v-mutation="handleMutation">
    Observe my changes
  </div>
</template>

<script setup>
function handleMutation(mutations, observer) {
  mutations.forEach(mutation => {
    console.log('Type:', mutation.type)
    console.log('Target:', mutation.target)
  })
}
</script>

API

OptionTypeDefaultDescription
handlerFunction-Callback when mutations occur (required)
attributesbooleanfalseObserve attribute changes
attributeFilterstring[]-Specific attributes to observe
childListbooleantrueObserve child node changes
subtreebooleanfalseObserve all descendants
characterDatabooleanfalseObserve text content changes

Mutation Types

TypeDescription
attributesAttribute was added, removed, or changed
childListChild nodes were added or removed
characterDataText content was changed

Examples

Observe Child Changes

vue
<template>
  <div v-mutation="{
    handler: handleChange,
    childList: true,
    subtree: true
  }">
    <div v-for="item in items" :key="item.id">{{ item.name }}</div>
  </div>
</template>

Observe Attributes

vue
<template>
  <div v-mutation="{
    handler: handleAttributeChange,
    attributes: true,
    attributeFilter: ['class', 'style']
  }">
    Watch class and style changes
  </div>
</template>

Released under the MIT License.