Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useElementBounding.md
1---2category: Elements3---45# useElementBounding67Reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element89## Usage1011```vue12<script setup lang="ts">13import { useElementBounding } from '@vueuse/core'14import { useTemplateRef } from 'vue'1516const el = useTemplateRef('el')17const { x, y, top, right, bottom, left, width, height } = useElementBounding(el)18</script>1920<template>21<div ref="el" />22</template>23```2425## Component Usage2627```vue28<template>29<UseElementBounding v-slot="{ width, height }">30Width: {{ width }} Height: {{ height }}31</UseElementBounding>32</template>33```3435## Directive Usage3637```vue38<script setup lang="ts">39import { vElementBounding } from '@vueuse/components'4041interface BoundingType {42height: number43bottom: number44left: number45right: number46top: number47width: number48x: number49y: number50}5152function onBounding({ height, bottom, left, right, top, width, x, y }: BoundingType) {53console.log(height, bottom, left, right, top, width, x, y)54}5556const options = {57reset: true,58windowResize: true,59windowScroll: true,60immediate: true,61updateTiming: 'sync',62}63</script>6465<template>66<textarea v-element-bounding="onBounding" />67<!-- with options -->68<textarea v-element-bounding="[onBounding, options]" />69</template>70```7172## Type Declarations7374```ts75export interface UseElementBoundingOptions {76/**77* Reset values to 0 on component unmounted78*79* @default true80*/81reset?: boolean82/**83* Listen to window resize event84*85* @default true86*/87windowResize?: boolean88/**89* Listen to window scroll event90*91* @default true92*/93windowScroll?: boolean94/**95* Immediately call update on component mounted96*97* @default true98*/99immediate?: boolean100/**101* Timing to recalculate the bounding box102*103* Setting to `next-frame` can be useful when using this together with something like {@link useBreakpoints}104* and therefore the layout (which influences the bounding box of the observed element) is not updated on the current tick.105*106* @default 'sync'107*/108updateTiming?: "sync" | "next-frame"109}110export interface UseElementBoundingReturn {111height: ShallowRef<number>112bottom: ShallowRef<number>113left: ShallowRef<number>114right: ShallowRef<number>115top: ShallowRef<number>116width: ShallowRef<number>117x: ShallowRef<number>118y: ShallowRef<number>119update: () => void120}121/**122* Reactive bounding box of an HTML element.123*124* @see https://vueuse.org/useElementBounding125* @param target126*/127export declare function useElementBounding(128target: MaybeComputedElementRef,129options?: UseElementBoundingOptions,130): UseElementBoundingReturn131```132