Skip to content

Overflow Detection

hookoverflowresize-observerdetection

Detects whether an element's content overflows its container using ResizeObserver. Returns a ref to attach and a reactive boolean — no polling, no manual recalculation.

tsx
const { ref, isOverflowing } = useOverflowDetection("horizontal");return (  <div ref={ref} className="truncate">    {text}    {isOverflowing && <span></span>}  </div>);

Installation

$pnpm exec dgadd add ui/overflow-detection
[Installs to]src/hooks/use-overflow-detection.ts[Item]ui/overflow-detection

dgadd is not public on npm yet. Until the first release, pack @diffgazer/add from the repository and install that tarball into this app, which is what puts dgadd on pnpm exec.

UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS once packages are available.

Parameters

NameTypeDefaultDescription
direction"horizontal" | "vertical" | "both"Which axis to check for overflow. "horizontal" compares scrollWidth > clientWidth, "vertical" compares scrollHeight > clientHeight, "both" checks either. Defaults to "horizontal".

Returns

{ ref: RefCallback<T>; isOverflowing: boolean }A callback ref to attach to the observed element, and a boolean that updates reactively when overflow state changes.
NameTypeDefaultDescription
refrequiredRefCallback<T>Attach to the element whose overflow you want to detect. Conditional mounts and replacement nodes are observed when the callback receives them. The element must have constrained dimensions (width/height).
isOverflowingrequiredbooleanTrue when content exceeds container bounds in the specified direction.

Examples

Basic Detection

Preview

Notes

Common Use Cases

Conditionally showing tooltips on truncated text, toggling expand/collapse buttons, applying visual indicators when content is clipped.

Requirement

The ref'd element must have constrained dimensions. For horizontal detection, the element needs a width constraint (e.g. truncate, max-w-*, fixed width). For vertical, it needs a height constraint.

Source

Highlighted source loads after this disclosure opens. Browse the source repository.