Skip to content
On this page

useElementSize

Category
Export Size
1.27 kB
Last Changed
3 weeks ago

Reactive size of an HTML element. ResizeObserver MDN

Demo

Resize the box to see changes

Usage

html
<template>
  <div ref="el">
    Height: {{ height }}
    Width: {{ width }}
  </div>
</template>

<script>
import { ref } from 'vue'
import { useElementSize } from '@vueuse/core'

export default {
  setup() {
    const el = ref(null)
    const { width, height } = useElementSize(el)

    return {
      el,
      width,
      height,
    }
  }
}
</script>
<template>
  <div ref="el">
    Height: {{ height }}
    Width: {{ width }}
  </div>
</template>

<script>
import { ref } from 'vue'
import { useElementSize } from '@vueuse/core'

export default {
  setup() {
    const el = ref(null)
    const { width, height } = useElementSize(el)

    return {
      el,
      width,
      height,
    }
  }
}
</script>

Component Usage

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

html
<UseElementSize v-slot="{ width, height }">
  Width: {{ width }}
  Height: {{ height }}
</UseElementSize>
<UseElementSize v-slot="{ width, height }">
  Width: {{ width }}
  Height: {{ height }}
</UseElementSize>

Directive Usage

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

html
<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-size="onResize" />
  <!-- with options -->
  <textarea v-element-size="[onResize, {width:100,height:100}, {'box':'content-box'} ]" />
</template>
<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-size="onResize" />
  <!-- with options -->
  <textarea v-element-size="[onResize, {width:100,height:100}, {'box':'content-box'} ]" />
</template>

Type Declarations

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

Source

SourceDemoDocs

Contributors

Anthony Fu
webfansplz
wheat
仿生狮子
ByMykel
jing
vaakian X
Vasily Kuzin
vaakian X
Jelf
Shinigami
Alex Kozack
Sanxiaozhizi

Changelog

v9.12.0 on 1/29/2023
902b3 - fix: fix incorrect element size on SVG (#2661)
30475 - fix: use contentBoxSize as an array in useElementSize (#2520)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.2.0 on 9/5/2022
3c642 - feat: support box sizing (#2143)
v8.9.3 on 7/14/2022
28db2 - fix(useResizeObserver)!: rename type ResizeObserverOptions to UseResizeObserverOptions (#1862)
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.1.0 on 3/16/2022
bc1bd - fix: make directive only registered once (#1414)
v8.0.0-beta.3 on 3/8/2022
13464 - feat: directive support (#1365)
v7.6.2 on 2/13/2022
7b400 - fix: should reset value while element unmounted (#1254)

Released under the MIT License.