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.
npm install vue-resize-observerVerified import paths — ran on the pinned version, not inferred.
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.
For Vue 3, use `npm install --save vue-resize-observer@next`. For Vue 2, use `npm install --save vue-resize-observer`.
For Vue 3, ensure you use `app.use(Plugin)` on your application instance. E.g., `const app = createApp(App); app.use(VueResizeObserver);`. For Vue 2, use `Vue.use(Plugin)`.
For older browsers, consider a `ResizeObserver` polyfill (e.g., `resize-observer-polyfill`). For SSR, dynamically import components that use `v-resize` only on the client-side or wrap usage in `client-only` blocks. Check `typeof ResizeObserver !== 'undefined'` before use.
Ensure that any style changes or DOM manipulations within your `onResize` handler are either batched, deferred (e.g., using `requestAnimationFrame` or `Vue.nextTick`), or conditionally applied to avoid re-triggering the observer in the same frame.
Install a `ResizeObserver` polyfill for unsupported browsers or Jest. For SSR, ensure code utilizing `ResizeObserver` is executed only on the client-side using conditional rendering or dynamic imports with `ssr: false`.
For Vue 3, call `use` on your application instance: `const app = createApp(App); app.use(VueResizeObserver);`. For Vue 2, `Vue.use(VueResizeObserver)` is correct.
Ensure `app.use(VueResizeObserver)` (Vue 3) or `Vue.use(VueResizeObserver)` (Vue 2) is called before mounting your app, or register it locally in your component's `directives` option: `directives: { 'resize': VueResizeObserver }`. Check that the import path is correct.No dependency data recorded yet.