Back to Mantine

Use Element Size

apps/mantine.dev/src/pages/hooks/use-element-size.mdx

9.3.0943 B
Original Source

import { UseElementSizeDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.useElementSize);

Usage

<Demo data={UseElementSizeDemos.usage} />

API

use-element-size is a simpler version of the use-resize-observer hook. The hook returns a ref object that should be passed to the observed element, and the element's height and width. On the first render (as well as during SSR), or when no element is being observed, the width and height properties are equal to 0.

tsx
import { useElementSize } from '@mantine/hooks';

const { ref, width, height } = useElementSize();

Definition

tsx
interface UseElementSizeReturnValue {
  ref: React.RefObject<HTMLElement>;
  width: number;
  height: number;
}

function useElementSize<T extends HTMLElement = any>(): UseElementSizeReturnValue;