use-element-size
shadcn-hooks/use-element-sizeFreeHook
A hook to track an element's width and height
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-element-size.jsonSource
1import { useState } from 'react'2import { useEffectWithTarget } from '@/registry/hooks/use-effect-with-target'3import { getTargetElement } from '@/registry/lib/create-effect-with-target'4import type { BasicTarget } from '@/registry/lib/create-effect-with-target'56export interface ElementSize {7 width: number8 height: number9}1011export interface UseElementSizeOptions {12 /**13 * ResizeObserver box model to use for measuring.14 *15 * @default 'content-box'16 */17 box?: ResizeObserverBoxOptions18}1920const defaultInitialSize: ElementSize = {21 width: 0,22 height: 0,23}2425type ResizeObserverBoxSize =26 | ResizeObserverSize27 | ReadonlyArray<ResizeObserverSize>2829function toBoxSizeArray(30 boxSize: ResizeObserverBoxSize | undefined,31): readonly ResizeObserverSize[] {32 if (!boxSize) {33 return []34 }3536 return Array.isArray(boxSize)37 ? boxSize38 : ([boxSize] as readonly ResizeObserverSize[])39}4041function getSizeFromEntry(42 entry: ResizeObserverEntry,43 box: ResizeObserverBoxOptions,44): ElementSize {45 const boxSize =46 box === 'border-box'47 ? entry.borderBoxSize48 : box === 'device-pixel-content-box'49 ? entry.devicePixelContentBoxSize50 : entry.contentBoxSize5152 const sizes = toBoxSizeArray(boxSize)5354 if (sizes.length > 0) {55 return {56 width: sizes.reduce((sum, size) => sum + size.inlineSize, 0),57 height: sizes.reduce((sum, size) => sum + size.blockSize, 0),58 }59 }6061 return {62 width: entry.contentRect.width,63 height: entry.contentRect.height,64 }65}6667function isSvgElement(element: Element): element is SVGElement {68 return typeof SVGElement !== 'undefined' && element instanceof SVGElement69}7071export function useElementSize(72 target: BasicTarget<Element>,73 initialSize: ElementSize = defaultInitialSize,74 options: UseElementSizeOptions = {},75): ElementSize {76 const { box = 'content-box' } = options77 const [size, setSize] = useState<ElementSize>(initialSize)7879 const updateSize = (nextSize: ElementSize) => {80 setSize((currentSize) => {81 if (82 currentSize.width === nextSize.width &&83 currentSize.height === nextSize.height84 ) {85 return currentSize86 }8788 return nextSize89 })90 }9192 useEffectWithTarget(93 () => {94 const element = getTargetElement(target)9596 if (!element) {97 updateSize({ width: 0, height: 0 })98 return99 }100101 updateSize(initialSize)102103 if (typeof ResizeObserver === 'undefined') {104// … truncated
What it pulls in
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
