Components/AutoSizer

AutoSizer

Automatically follows content width and height changes with resize and FLIP transitions.

VerifiedSince 0.3.4

Built on ResizeObserver. When images/async rendering change size, it re-measures automatically.

Usage

AutoSizer height

Loading demo...

Width Follow (Inside Flex Containers)

When AutoSizer is a flex item and the parent stretches it with flex: 1 or width: 100%, it may look like it is not following content.

AutoSizer width in flex

Loading demo...

AutoSizer width

Loading demo...

Number Transition + Width Follow (TextMorph)

AutoSizer + TextMorph

Loading demo...

Smooth Text Transform + Width Follow (TextTransformer)

This example enables width + height follow and sets outer overflow-hidden to clip blur overflow.

AutoSizer + TextTransformer

Loading demo...

Best Practices

  • Use height=true and width=false for tab panels, accordions, and dropdown bodies where the surrounding layout should keep its width.
  • Use width=true and height=false for buttons, inline labels, and number transitions; this mode becomes shrink-to-content by default.
  • Keep outerClass="overflow-hidden" when content animates with blur, scale, or FLIP so intermediate frames do not bleed outside the wrapper.
  • Wrap explicit state mutations in action() or flip() instead of toggling state and calling refresh() manually; this preserves the before/after snapshot used by the transition.
  • Set observeTarget="both" only when both wrapper and content can resize independently; observing only inner avoids unnecessary refresh work for ordinary content changes.
  • Use rounding="ceil" for text and button labels to avoid clipped sub-pixel content; switch to floor only when a parent layout requires tighter bounds.

API Reference

Props

PropTypeDefaultDescription
asstringdivRoot element tag used for the measured outer wrapper.
innerAsstringdivInner element tag that wraps slot content.
widthbooleantrueEnables width measurement and style application.
heightbooleantrueEnables height measurement and style application.
inlineboolean-Forces inline sizing; when omitted, width-only mode uses inline layout automatically.
durationMsnumber200Resize / FLIP transition duration in milliseconds.
easingstringeaseCSS timing function used for resize / FLIP transitions.
outerClassstringoverflow-hiddenClass applied to the outer wrapper before forwarded attrs classes.
innerClassstring-Class applied to the inner content wrapper.
rounding'none' | 'round' | 'floor' | 'ceil'ceilRounding strategy for measured width and height values.
immediatebooleantrueMeasures immediately after mount when enabled.
rafBatchbooleantrueBatches resize measurement through requestAnimationFrame when available.
observeTarget'inner' | 'outer' | 'both'innerElement target watched for automatic resize changes.

Slots

SlotDescription
defaultMeasured content rendered inside the inner wrapper.

Events

TxAutoSizer does not emit component-level Vue events. Use the exposed action(), flip(), or refresh() methods when a parent needs to coordinate explicit mutations.

Expose

NameTypeDescription
refresh()() => Promise<void>Manually remeasures the configured target.
flip(action)(action: () => void | Promise<void>) => Promise<void>Runs an action while auto-resize is paused, then performs a size FLIP transition and remeasures.
action(fn, options?)(fn: (el: HTMLElement) => void | Promise<void>, options?: AutoSizerActionOptions | detect) => Promise<any>Runs a mutation against the inner or outer element and returns before / after snapshots with changed keys.
size{ width: number; height: number } | nullLatest measured size from the auto-resize utility.
focus()() => voidFocuses the outer wrapper when it is focusable.
outerElHTMLElement | nullOuter wrapper element for advanced integration.

Overview

  • The outer wrapper receives forwarded attrs, merged class/style, and auto-resize styles; the inner wrapper owns the measured content with display: flow-root.
  • Width-only mode uses inline layout automatically unless inline=false is set explicitly.
  • flip(action) pauses auto-resize while the action runs, performs a size FLIP transition, then re-enables measurement.
  • action(fn, options?) snapshots the selected target before and after mutation and returns detected change keys.
  • observeTarget, rounding, immediate, rafBatch, durationMs, and easing are forwarded to the underlying resize/FLIP utilities.

Technologies

  • Reviewed against packages/tuffex/packages/components/src/auto-sizer/src/types.ts, TxAutoSizer.vue, and auto-sizer.test.ts.
  • The public contract is mostly exposed methods, not emitted events; docs should keep examples focused on action() / flip() coordination.
  • observeTarget="both" is intentionally advanced; ordinary content transitions should observe inner only.
  • Component source: packages/tuffex/packages/components/src/auto-sizer/src/TxAutoSizer.vue.
  • Types: packages/tuffex/packages/components/src/auto-sizer/src/types.ts.
  • Verified coverage: packages/tuffex/packages/components/src/auto-sizer/__tests__/auto-sizer.test.ts verifies wrapper / inner tag rendering, forwarded sizing options, automatic width-only inline layout, exposed refresh / focus / outerEl, and action() snapshot detection.
查看源码
packages/tuffex/packages/components/src/auto-sizer/index.ts