Components/AutoSizer

AutoSizer

A container that animates to fit its content's size.

VerifiedSince 0.3.4

Usage

Height Only

Syncs only the height, so the surrounding layout keeps its width; wrap the state change in action().

Loading demo...

Width in a Flex Container

As a flex item, don't stretch it with flex: 1 or width: 100%, or its width stops following the content.

Loading demo...

Width Only

Syncing only the width shrinks it to its content, which suits changing button labels.

Loading demo...

Number Transition

With TxTextMorph, the width follows as the digit count changes.

Loading demo...

Text Transform

Width and height both follow; the default outerClass="overflow-hidden" clips the blur's overflow.

Loading demo...

Best Practices

  • Sync only the height for content areas (tabs, accordions, dropdowns) and only the width for buttons, labels, and numbers.
  • Keep outerClass="overflow-hidden" when content animates with blur, scale, or FLIP.
  • Wrap explicit state changes in action() or flip() rather than changing state and calling refresh(), which loses the before/after snapshot the transition needs.
  • Set observeTarget="both" only when the wrapper and the content resize independently.
  • Keep rounding="ceil" for text to avoid sub-pixel clipping; use floor only when a parent needs tighter bounds.

API Reference

Props

PropTypeDefaultDescription
asstringdivOuter element tag.
innerAsstringdivInner element tag.
widthbooleantrueSyncs the width.
heightbooleantrueSyncs the height.
inlineboolean-Shrinks to the content width; when omitted, on only in width-only mode.
durationMsnumber200Transition duration in ms.
easingstringeaseTransition timing function.
outerClassstringoverflow-hiddenOuter class, applied before forwarded classes.
innerClassstring-Inner class.
rounding'none' | 'round' | 'floor' | 'ceil'ceilHow measured values are rounded.
immediatebooleantrueMeasures right after mount.
rafBatchbooleantrueBatches measurements with rAF.
observeTarget'inner' | 'outer' | 'both'innerElement watched for size changes.

Slots

SlotDescription
defaultMeasured content, rendered in the inner wrapper.

Exposed Methods

NameTypeDescription
refresh()() => Promise<void>Re-measures.
flip(action)(action: () => void | Promise<void>) => Promise<void>Runs action inside a size FLIP transition.
action(fn, options?)(fn: (el: HTMLElement) => void | Promise<void>, options?: AutoSizerActionOptions | detect) => Promise<any>Runs a change on the inner or outer element with a transition; returns before/after snapshots and changedKeys.
size{ width: number; height: number } | nullThe last measured size.
focus()() => voidFocuses the outer wrapper when it is focusable.
outerElHTMLElement | nullThe outer wrapper element.

Overview

  • Forwarded attrs land on the outer wrapper, merged with outerClass and the sizing styles; the inner wrapper holds the measured content with display: flow-root.
  • It re-measures whenever the content's size changes, including image loads and async rendering.
  • flip() and action() pause automatic measurement while they run and re-measure after the transition.

Technologies

  • Automatic measurement runs on ResizeObserver; explicit changes use a size FLIP.
  • Source: packages/tuffex/packages/components/src/auto-sizer/.
查看源码
packages/tuffex/packages/components/src/auto-sizer/index.ts