apps/mantine.dev/src/pages/hooks/use-element-size.mdx
import { UseElementSizeDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useElementSize);
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.
import { useElementSize } from '@mantine/hooks';
const { ref, width, height } = useElementSize();
interface UseElementSizeReturnValue {
ref: React.RefObject<HTMLElement>;
width: number;
height: number;
}
function useElementSize<T extends HTMLElement = any>(): UseElementSizeReturnValue;