Vue.js plugin that detects DOM element resizing using a ResizeObserver component
npx @tessl/cli install tessl/npm-vue-resize@1.0.0Vue Resize is a Vue.js plugin that detects DOM element resizing through a ResizeObserver component. It provides a cross-browser solution for monitoring parent element size changes, using object element-based detection with special handling for Internet Explorer compatibility.
npm install vue-resizeimport VueResize from 'vue-resize';For individual component import:
import { ResizeObserver } from 'vue-resize';CSS import (required):
import 'vue-resize/dist/vue-resize.css';import Vue from 'vue';
import VueResize from 'vue-resize';
import 'vue-resize/dist/vue-resize.css';
// Install as plugin (registers components globally)
Vue.use(VueResize);
// Use in template - component monitors parent element
export default {
template: `
<div class="container" style="position: relative;">
<h1>Resizable Content</h1>
<resize-observer @notify="handleResize" />
</div>
`,
methods: {
handleResize({ width, height }) {
console.log('Parent container resized:', width, height);
}
}
};Vue Resize uses object element-based resize detection to provide broad browser compatibility:
Global registration of resize detection components.
/**
* Vue plugin installation function
* @param Vue - Vue constructor
*/
function install(Vue: any): void;
/**
* Default plugin export with install method and version
*/
interface VueResizePlugin {
install: (Vue: any) => void;
version: string;
}Vue component that detects parent element resizing and emits notifications. The component itself is invisible and positioned absolutely within its parent to monitor dimension changes.
/**
* ResizeObserver Vue component
* Monitors parent element size changes and emits 'notify' events
*/
interface ResizeObserverComponent {
name: 'ResizeObserver';
props: {
/** Whether to emit size notification immediately on mount */
emitOnMount: {
type: Boolean;
default: false;
};
/** Whether to ignore width changes when detecting resize */
ignoreWidth: {
type: Boolean;
default: false;
};
/** Whether to ignore height changes when detecting resize */
ignoreHeight: {
type: Boolean;
default: false;
};
};
/** Emitted when parent element is resized */
$emit(event: 'notify', payload: ResizeNotification): void;
// Lifecycle methods
mounted(): void;
beforeDestroy(): void;
// Internal methods
methods: {
/** Compare current size with previous and emit if changed */
compareAndNotify(): void;
/** Emit current parent element size */
emitSize(): void;
/** Add resize event listeners to the object element */
addResizeHandlers(): void;
/** Remove resize event listeners and clean up */
removeResizeHandlers(): void;
};
}
/**
* Resize notification payload containing parent element dimensions
*/
interface ResizeNotification {
width: number;
height: number;
}Usage Examples:
// Basic resize detection
<template>
<div class="resizable-container">
<resize-observer @notify="onResize" />
<p>This container is being monitored for size changes.</p>
</div>
</template>
<script>
export default {
methods: {
onResize({ width, height }) {
console.log(`Container size: ${width}x${height}`);
}
}
};
</script>
<style scoped>
.resizable-container {
position: relative; /* Required: parent must be positioned */
width: 100%;
height: 200px;
border: 1px solid #ccc;
}
</style>// Advanced configuration
<template>
<div class="monitored-element">
<resize-observer
:emit-on-mount="true"
:ignore-height="false"
:ignore-width="false"
@notify="handleElementResize"
/>
<div class="content">Dynamic content here...</div>
</div>
</template>
<script>
export default {
methods: {
handleElementResize(dimensions) {
// Handle both width and height changes
this.updateLayout(dimensions);
},
updateLayout({ width, height }) {
// Responsive layout adjustments
if (width < 600) {
this.isMobile = true;
}
}
}
};
</script>Import and register ResizeObserver component individually.
/**
* ResizeObserver component for individual import
*/
const ResizeObserver: ResizeObserverComponent;The ResizeObserver component includes internal CSS for invisible operation.
/**
* Component CSS classes and styling
*/
.resize-observer {
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 100%;
height: 100%;
border: none;
background-color: transparent;
pointer-events: none;
display: block;
overflow: hidden;
opacity: 0;
}
.resize-observer >>> object {
display: block;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
pointer-events: none;
z-index: -1;
}Usage Example:
import Vue from 'vue';
import { ResizeObserver } from 'vue-resize';
import 'vue-resize/dist/vue-resize.css';
// Register individual component
Vue.component('resize-observer', ResizeObserver);
// Or with PascalCase name
Vue.component('ResizeObserver', ResizeObserver);import Vue from 'vue';
import VueResize from 'vue-resize';
import 'vue-resize/dist/vue-resize.css';
Vue.use(VueResize);
// Registers both 'resize-observer' and 'ResizeObserver' components globallyNote: The plugin auto-installs when Vue is globally available (in browser environments). Manual installation is only needed in module environments.
import Vue from 'vue';
import { ResizeObserver } from 'vue-resize';
import 'vue-resize/dist/vue-resize.css';
Vue.component('resize-observer', ResizeObserver);<link rel="stylesheet" href="vue-resize/dist/vue-resize.css"/>
<script src="vue.js"></script>
<script src="vue-resize/dist/vue-resize.min.js"></script>
<script>
// Auto-installs when Vue is globally available
// Or install manually: Vue.use(VueResize);
// Access component: VueResize.ResizeObserver
</script>about:blank data for resize detection/**
* Main plugin export
*/
interface VueResizePlugin {
install: (Vue: any) => void;
version: string;
}
/**
* Resize event notification data
*/
interface ResizeNotification {
width: number;
height: number;
}
/**
* ResizeObserver component props
*/
interface ResizeObserverProps {
emitOnMount: boolean;
ignoreWidth: boolean;
ignoreHeight: boolean;
}