Skip to main content
Vuetify0 is now in alpha!
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useResizeObserver

A composable for detecting element size changes using the Resize Observer API with automatic cleanup.

Usage

The useResizeObserver composable wraps the Resize Observer API to detect when an element’s dimensions change. It’s useful for responsive components, charts, virtualized lists, and aspect ratio maintenance.

Tip

Why wrap ResizeObserver? The native ResizeObserver has no awareness of Vue’s effectScope lifecycle. If you create one inside a composable, it won’t automatically disconnect when the scope is disposed. useResizeObserver integrates onScopeDispose for automatic cleanup, defers creation until after hydration for SSR safety, and adds reactive target tracking — things the native API can’t do on its own.

vue
<script setup lang="ts">
  import { useResizeObserver } from '@vuetify/v0'
  import { ref, useTemplateRef } from 'vue'

  const container = useTemplateRef('container')
  const width = ref(0)
  const height = ref(0)

  useResizeObserver(container, (entries) => {
    const entry = entries[0]
    width.value = entry.contentRect.width
    height.value = entry.contentRect.height
  })
</script>

<template>
  <div ref="container" class="resizable">
    <p>Width: {{ width }}px</p>
    <p>Height: {{ height }}px</p>
  </div>
</template>

Architecture

useResizeObserver wraps the native ResizeObserver API with Vue reactivity:

Resize Observer Hierarchy

Use controls to zoom and pan. Click outside or press Escape to close.

Resize Observer Hierarchy

Options

OptionTypeDefaultNotes
immediatebooleanfalseFire the callback immediately on mount before any resize
oncebooleanfalseStop observing after the first callback fires
box'content-box' | 'border-box''content-box'Which box model to observe

Reactivity

Property/MethodReactiveNotes
isActiveComputed from observer ref
isPausedShallowRef, readonly
targetAccepts MaybeRef, watched for changes
pause()Temporarily stop observing without disconnecting
resume()Resume after pause()
stop()Disconnect the observer permanently

useElementSize

Property/MethodReactiveNotes
widthShallowRef
heightShallowRef

Examples

Responsive Grid

A manually resizable container that adapts from 1 to 3 columns based on its own width, with live dimension readout.

0 x 0
Analytics
Users
Revenue
Orders
Messages
Settings

Drag the right edge to resize. Columns: 1

API Reference

The following API details are for the useResizeObserver composable.

Functions

useResizeObserver

(target: MaybeElementRef, callback: (entries: ResizeObserverEntry[]) => void, options?: ResizeObserverOptions) => UseResizeObserverReturn

A composable that uses the Resize Observer API to detect when an element's size changes.

useElementSize

(target: MaybeElementRef) => UseElementSizeReturn

A convenience composable that uses the Resize Observer API to track an element's size.

Options

immediate

boolean | undefined

once

boolean | undefined

box

"content-box" | "border-box" | undefined

Properties

isActive

Readonly<Ref<boolean, boolean>>

Whether the observer is currently active (created and observing)

isPaused

Readonly<Ref<boolean, boolean>>

Whether the observer is currently paused

Methods

pause

() => void

Pause observation (disconnects observer but keeps it alive)

resume

() => void

Resume observation

stop

() => void

Stop observation and clean up (destroys observer)

Was this page helpful?

© 2016-1970 Vuetify, LLC
Ctrl+/