Skip to content

useElementSize

A composable for reactive element width and height using ResizeObserver. Useful for layout calculations, charts, responsive containers, and dynamic sizing based on element dimensions.

Basic Usage

vue
<template>
  <div ref="target">Content</div>
  <p>Width: {{ width }}px, Height: {{ height }}px</p>
</template>

<script setup>
import { ref } from "vue";
import { useElementSize } from "@baklavue/composables";

const target = ref<HTMLElement | null>(null);
const { width, height } = useElementSize(target);
</script>

Options

vue
<script setup>
import { ref } from "vue";
import { useElementSize } from "@baklavue/composables";

const target = ref<HTMLElement | null>(null);
const { width, height } = useElementSize(target, {
  initialWidth: 0,
  initialHeight: 0,
});
</script>
OptionTypeDefaultDescription
initialWidthnumber0Initial width when no element is observed
initialHeightnumber0Initial height when no element is observed

With Charts

vue
<template>
  <div ref="chartContainer" class="chart-wrapper">
    <canvas :width="width" :height="height" />
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useElementSize } from "@baklavue/composables";

const chartContainer = ref<HTMLElement | null>(null);
const { width, height } = useElementSize(chartContainer);
</script>

API

Parameters

  • targetRef<Element | null | undefined> — Ref to the element to observe
  • options — Optional UseElementSizeOptions

Return Value

PropertyTypeDescription
widthRef<number>Element content width
heightRef<number>Element content height

TypeScript Support

typescript
import {
  useElementSize,
  type UseElementSizeOptions,
} from "@baklavue/composables";

const target = ref<HTMLElement | null>(null);
const { width, height } = useElementSize(target);