Skip to content

useResizeObserver

Category
Export Size
500 B
Last Changed
5 months ago

Reports changes to the dimensions of an Element's content or the border-box

Demo

Resize the box to see changes

Usage

vue
<script setup lang="ts">
import { 
useResizeObserver
} from '@vueuse/core'
import {
ref
,
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
('el')
const
text
=
ref
('')
useResizeObserver
(
el
, (
entries
) => {
const
entry
=
entries
[0]
const {
width
,
height
} =
entry
.
contentRect
text
.
value
= `width: ${
width
}, height: ${
height
}`
}) </script> <template> <
div
ref
="
el
">
{{
text
}}
</
div
>
</template>

Directive Usage

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

vue
<script setup lang="ts">
import { 
vResizeObserver
} from '@vueuse/components'
const
text
=
ref
('')
function
onResizeObserver
(
entries
) {
const [
entry
] =
entries
const {
width
,
height
} =
entry
.contentRect
text
.
value
= `width: ${
width
}, height: ${
height
}`
} </script> <template> <
div
v-resize-observ
er="
onResizeObserver
">
{{
text
}}
</
div
>
</template>

ResizeObserver MDN

Type Declarations

Show Type Declarations
ts
export interface ResizeObserverSize {
  readonly 
inlineSize
: number
readonly
blockSize
: number
} export interface ResizeObserverEntry { readonly
target
: Element
readonly
contentRect
: DOMRectReadOnly
readonly
borderBoxSize
:
ReadonlyArray
<ResizeObserverSize>
readonly
contentBoxSize
:
ReadonlyArray
<ResizeObserverSize>
readonly
devicePixelContentBoxSize
:
ReadonlyArray
<ResizeObserverSize>
} export type
ResizeObserverCallback
= (
entries
:
ReadonlyArray
<ResizeObserverEntry>,
observer
:
ResizeObserver
,
) => void export interface UseResizeObserverOptions extends ConfigurableWindow { /** * Sets which box model the observer will observe changes to. Possible values * are `content-box` (the default), `border-box` and `device-pixel-content-box`. * * @default 'content-box' */
box
?:
ResizeObserverBoxOptions
} declare class
ResizeObserver
{
constructor(
callback
:
ResizeObserverCallback
)
disconnect
(): void
observe
(
target
: Element,
options
?: UseResizeObserverOptions): void
unobserve
(
target
: Element): void
} /** * Reports changes to the dimensions of an Element's content or the border-box * * @see https://vueuse.org/useResizeObserver * @param target * @param callback * @param options */ export declare function
useResizeObserver
(
target
:
|
MaybeComputedElementRef
|
MaybeComputedElementRef
[]
|
MaybeRefOrGetter
<
MaybeElement
[]>,
callback
:
ResizeObserverCallback
,
options
?: UseResizeObserverOptions,
): {
isSupported
:
ComputedRef
<boolean>
stop
: () => void
} export type
UseResizeObserverReturn
=
ReturnType
<typeof
useResizeObserver
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
青椒肉丝
IlyaL
Jelf
Hongkun
远方os
birdxiao
acyza
ByMykel
vaakian X
karma
Shinigami
Alex Kozack
Jacob Clevenger
Sanxiaozhizi
Antério Vieira
zhong666

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.1.0 on
044dd - fix: update type (#4218)
v11.0.0-beta.2 on
d4310 - feat: added vResizeObserver directive (#4008)
1c124 - fix: fix target is Ref Array (#4005)
v10.8.0 on
48421 - fix: do not watch deep to avoid cicular calls (#3753)
v10.0.0-beta.2 on
3e187 - feat: support element list (#2841)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access