Skip to content
On this page

useElementBounding

Category
Last Changed
20 days ago

Reactive bounding box of an HTML element

Demo

Resize the box to see changes

Usage

<template>
  <div ref="el" />
</template>

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

export default {
  setup() {
    const el = ref(null)
    const { x, y, top, right, bottom, left, width, height } = useElementBounding(el)

    return {
      el,
      /* ... */
    }
  }
})
</script>
<template>
  <div ref="el" />
</template>

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

export default {
  setup() {
    const el = ref(null)
    const { x, y, top, right, bottom, left, width, height } = useElementBounding(el)

    return {
      el,
      /* ... */
    }
  }
})
</script>

Component

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.
<UseElementBounding v-slot="{ width, height }">
  Width: {{ width }}
  Height: {{ height }}
</UseElementBounding>
<UseElementBounding v-slot="{ width, height }">
  Width: {{ width }}
  Height: {{ height }}
</UseElementBounding>

Type Declarations

/**
 * Reactive bounding box of an HTML element.
 *
 * @see https://vueuse.org/useElementBounding
 * @param target
 */
export declare function useElementBounding(target: MaybeElementRef): {
  height: Ref<number>
  bottom: Ref<number>
  left: Ref<number>
  right: Ref<number>
  top: Ref<number>
  width: Ref<number>
  x: Ref<number>
  y: Ref<number>
  update: () => void
}
export declare type UseElementBoundingReturn = ReturnType<
  typeof useElementBounding
>
/**
 * Reactive bounding box of an HTML element.
 *
 * @see https://vueuse.org/useElementBounding
 * @param target
 */
export declare function useElementBounding(target: MaybeElementRef): {
  height: Ref<number>
  bottom: Ref<number>
  left: Ref<number>
  right: Ref<number>
  top: Ref<number>
  width: Ref<number>
  x: Ref<number>
  y: Ref<number>
  update: () => void
}
export declare type UseElementBoundingReturn = ReturnType<
  typeof useElementBounding
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Jelf
wheat
webfansplz
Ciro Lo Sapio
Shinigami
Alex Kozack

Changelog

v7.6.2 on 2/13/2022
aba02 - fix(useElementBouding): should reset value while element unmounted (#1253)
v7.6.0 on 2/8/2022
e1f64 - fix: update jsdoc
v7.4.0 on 12/18/2021
d6d1d - fix: fix target is null or undefined (#1043)
v7.0.0 on 11/20/2021
1229a - fix!: use getBoundingClientRect instead of contentRect (#870)
v6.4.0 on 9/17/2021
e557f - feat: added 'as' prop to renderable components (#742)
v4.11.2 on 5/30/2021
68c7d - feat(typedef): add return typedefs (#543) (#544)
v5.0.0-beta.2 on 5/25/2021
5bede - feat: introduce components & directives (#486)
useElementBounding has loaded