BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/shadcn-hooks/use-element-size

use-element-size

shadcn-hooks/use-element-size
FreeHook

A hook to track an element's width and height

Install it

npx shadcn@latest add https://shadcn-hooks.com/r/use-element-size.json

Source

registry/hooks/use-element-size.tsshadcn-hooks.com/r/use-element-size.json
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'
5
6export interface ElementSize {
7 width: number
8 height: number
9}
10
11export interface UseElementSizeOptions {
12 /**
13 * ResizeObserver box model to use for measuring.
14 *
15 * @default 'content-box'
16 */
17 box?: ResizeObserverBoxOptions
18}
19
20const defaultInitialSize: ElementSize = {
21 width: 0,
22 height: 0,
23}
24
25type ResizeObserverBoxSize =
26 | ResizeObserverSize
27 | ReadonlyArray<ResizeObserverSize>
28
29function toBoxSizeArray(
30 boxSize: ResizeObserverBoxSize | undefined,
31): readonly ResizeObserverSize[] {
32 if (!boxSize) {
33 return []
34 }
35
36 return Array.isArray(boxSize)
37 ? boxSize
38 : ([boxSize] as readonly ResizeObserverSize[])
39}
40
41function getSizeFromEntry(
42 entry: ResizeObserverEntry,
43 box: ResizeObserverBoxOptions,
44): ElementSize {
45 const boxSize =
46 box === 'border-box'
47 ? entry.borderBoxSize
48 : box === 'device-pixel-content-box'
49 ? entry.devicePixelContentBoxSize
50 : entry.contentBoxSize
51
52 const sizes = toBoxSizeArray(boxSize)
53
54 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 }
60
61 return {
62 width: entry.contentRect.width,
63 height: entry.contentRect.height,
64 }
65}
66
67function isSvgElement(element: Element): element is SVGElement {
68 return typeof SVGElement !== 'undefined' && element instanceof SVGElement
69}
70
71export function useElementSize(
72 target: BasicTarget<Element>,
73 initialSize: ElementSize = defaultInitialSize,
74 options: UseElementSizeOptions = {},
75): ElementSize {
76 const { box = 'content-box' } = options
77 const [size, setSize] = useState<ElementSize>(initialSize)
78
79 const updateSize = (nextSize: ElementSize) => {
80 setSize((currentSize) => {
81 if (
82 currentSize.width === nextSize.width &&
83 currentSize.height === nextSize.height
84 ) {
85 return currentSize
86 }
87
88 return nextSize
89 })
90 }
91
92 useEffectWithTarget(
93 () => {
94 const element = getTargetElement(target)
95
96 if (!element) {
97 updateSize({ width: 0, height: 0 })
98 return
99 }
100
101 updateSize(initialSize)
102
103 if (typeof ResizeObserver === 'undefined') {
104// … truncated

What it pulls in

Other registry items

  • @shadcnhooks/use-effect-with-target
  • @shadcnhooks/create-effect-with-target

Files it writes

  • registry/hooks/use-element-size.ts

Facts

Registry
shadcn-hooks
Type
registry:hook
Access
Free
Files written
1
Licence
the repository states none
In the index
at or before 2026-08-12
Last confirmed
today

Go to the source

shadcn-hooks.com Debbl/shadcn-hooks on GitHub Raw registry item This item as JSON

More from shadcn-hooks

All 58 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
use-active-elementuse-active-elementshadcn-hooksHooksFreeno deps
use-batteryuse-batteryshadcn-hooksHooksFreeno deps
use-booleanuse-booleanshadcn-hooksHooksFreeno deps
use-click-any-whereuse-click-any-whereshadcn-hooksHooksFreeno deps
use-click-awayuse-click-awayshadcn-hooksHooksFreeno deps
use-clipboarduse-clipboardshadcn-hooksHooksFreeno deps
use-controllable-valueuse-controllable-valueshadcn-hooksHooksFree1 dep
use-counteruse-countershadcn-hooksHooksFreeno deps

Also from Kynth Studios

Built for the same person as BlockDex

SkillWorks

Claude Code skills and plugins, scored

skillworks.kynth.studio

RuleStack

AGENTS.md gallery and agent-config comparison

rulestack.kynth.studio

ToolDrift

What the AI coding tools changed last night

tooldrift.kynth.studio

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex