Skip to content

useElementSize

Category
Export Size
897 B
Last Changed
4 days ago

Reactive size of an HTML element. ResizeObserver MDN

Demo

Resize the box to see changes

Usage

vue
<script setup lang="ts">
import { 
useElementSize
} from '@vueuse/core'
import {
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
('el')
const {
width
,
height
} =
useElementSize
(
el
)
</script> <template> <
div
ref
="
el
">
Height: {{
height
}}
Width: {{
width
}}
</
div
>
</template>

Component Usage

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

vue
<template>
  <UseElementSize v-slot="{ 
width
,
height
}">
Width: {{
width
}} Height: {{
height
}}
</UseElementSize> </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 { 
vElementSize
} from '@vueuse/components'
function
onResize
({
width
,
height
}: {
width
: number,
height
: number }) {
console
.
log
(
width
,
height
)
} </script> <template> <
textarea
v-element-si
ze="
onResize
" />
<!-- with options --> <
textarea
v-element-si
ze="
[
onResize
, {
width
: 100,
height
: 100 }, {
box
: 'content-box' }]
" />
</template>

Type Declarations

ts
export interface ElementSize {
  
width
: number
height
: number
} /** * Reactive size of an HTML element. * * @see https://vueuse.org/useElementSize */ export declare function
useElementSize
(
target
:
MaybeComputedElementRef
,
initialSize
?: ElementSize,
options
?:
UseResizeObserverOptions
,
): {
width
:
ShallowRef
<number, number>
height
:
ShallowRef
<number, number>
stop
: () => void
} export type
UseElementSizeReturn
=
ReturnType
<typeof
useElementSize
>

Source

SourceDemoDocs

Contributors

NoiseFan

Changelog

No recent changes

Released under the MIT License.