Monitor element size changes with v-resize directive
Use Case: Track element dimensions in real-time using ResizeObserver API.
Hint: Drag the bottom-right corner to resize the box!
// Resize observer with v-resize directive <div v-resize="onResize" class="resizable-box"> {{ width }}px × {{ height }}px </div> methods: { onResize(entries, $ctx) { const entry = entries[0]; // ContentRect provides content dimensions this.width = Math.round(entry.contentRect.width); this.height = Math.round(entry.contentRect.height); // BorderBoxSize provides full element dimensions if (entry.borderBoxSize) { const borderBox = entry.borderBoxSize[0]; this.borderBoxWidth = Math.round(borderBox.inlineSize); this.borderBoxHeight = Math.round(borderBox.blockSize); } // Access element through $ctx console.log('Element:', $ctx.element); console.log('VNode:', $ctx.vnode); } }
Use Case: Configure ResizeObserver with custom options using :options.resize attribute.
Hint: This example observes border-box dimensions instead of content-box!
// Custom box model with :options.resize <div v-resize="onBorderBoxResize" :options.resize="resizeOptions"> Border-box measurement </div> data: { resizeOptions: { box: 'border-box' } }, methods: { onBorderBoxResize(entries, $ctx) { const entry = entries[0]; if (entry.borderBoxSize) { const borderBox = entry.borderBoxSize[0]; this.borderWidth = Math.round(borderBox.inlineSize); this.borderHeight = Math.round(borderBox.blockSize); } } }
// Handler signature onResize(entries: ResizeObserverEntry[], $ctx: LifecycleContext) { // entries[0].contentRect - content dimensions // entries[0].borderBoxSize - border box dimensions // entries[0].contentBoxSize - content box dimensions // $ctx.element - DOM element // $ctx.vnode - VNode instance // $ctx.userData - Proxy-free storage } // Options format (ResizeObserverOptions) { box: 'content-box' // 'content-box' | 'border-box' | 'device-pixel-content-box' }