Vue Resize Observer Directive

2.0.16 · active · verified Sun Apr 19

Vue Resize Observer is a lightweight plugin that provides a Vue directive, `v-resize`, to easily detect and react to resize events on any DOM element within a Vue component. It leverages the native `ResizeObserver` browser API for efficient size change detection without the performance overhead of traditional window resize listeners or polling. The package supports both Vue 2 (version `2.0.16` is the latest stable for Vue 2) and Vue 3, with separate installation instructions (`vue-resize-observer@next` for Vue 3). While not explicitly stated, the `@next` tag indicates active development for Vue 3 compatibility, suggesting a potentially faster release cadence for the Vue 3 branch. Its primary differentiator is the direct integration of the `ResizeObserver` API into a straightforward Vue directive, simplifying responsive layouts and element-specific size management.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to globally install `vue-resize-observer` in a Vue 3 application and use the `v-resize` directive on a resizable HTML element to listen for and display its width and height changes.

import { createApp, defineComponent, ref, onMounted } from 'vue';
import App from './App.vue';
import VueResizeObserver from 'vue-resize-observer';

// main.ts or app.ts
const app = createApp(App);
app.use(VueResizeObserver); // Globally install the v-resize directive
app.mount('#app');

// App.vue (or any other component)
<template>
  <div
    class="resizable-box"
    v-resize="onBoxResize"
    style="width: 50%; min-width: 200px; height: 200px; border: 2px solid #3498db; background-color: #ecf0f1; resize: both; overflow: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: sans-serif; box-sizing: border-box; padding: 10px;"
  >
    <p>Resize this element!</p>
    <p>Width: {{ width }}px</p>
    <p>Height: {{ height }}px</p>
  </div>
  <p style="margin-top: 20px; font-family: sans-serif; color: #555;">Drag the bottom-right corner to change its size.</p>
</template>

<script lang="ts">
export default defineComponent({
  name: 'ResizeDemo',
  setup() {
    const width = ref(0);
    const height = ref(0);

    const onBoxResize = (entry: ResizeObserverEntry) => {
      // The entry object provides details about the resize event
      width.value = Math.floor(entry.contentRect.width);
      height.value = Math.floor(entry.contentRect.height);
      console.log(`Box resized: W: ${width.value}, H: ${height.value}`);
    };

    // Initial size capture on mount (ResizeObserver will also trigger on first render)
    onMounted(() => {
      // You might need a ref to the element if you want its initial size
      // directly, but v-resize will also call onBoxResize on first render.
    });

    return {
      width,
      height,
      onBoxResize,
    };
  },
});
</script>

<style>
/* No specific styles needed beyond inline ones for quickstart */
</style>

view raw JSON →