Skip to content

Auto Height

A box that follows the height of its content when the content changes, on a spring, instead of jumping.

$npx shadcn@latest add https://framebrick.co/r/auto-height.json

From the Compose Digi template, see it in context.

SEO package

  • Keyword and competitor audit
  • Weekly ranking report

$450 per week

Installation

$ npx shadcn@latest add https://framebrick.co/r/auto-height.json

Usage

import { AutoHeight } from "@/components/ui/auto-height";

// the box's own look goes on it; its content can change height
<AutoHeight className="rounded-xl border bg-card p-5">
  {period === "weekly" ? <WeeklyPlan /> : <MonthlyPlan />}
</AutoHeight>

Props

PropTypeDefaultDescription
classNamestring—The box: background, border, radius, padding. The height it animates includes its padding and border.
bouncenumber0.2Spring bounce: 0 settles without overshoot, 0.2 overshoots a little (Compose Digi's).
durationnumber0.4Seconds the move takes.
childrenReactNode—The content. Whatever changes its height — a swapped list, an opened panel, text loading in — moves the box.
  • It is the Compose Digi plan card: the Weekly / Monthly switch swaps the list and the price, and the card's height follows on spring S2 (bounce 0.2, 0.4 s, a slight overshoot — for example 437.6 → 515 px, peaking at 516.0).
  • It animates only when the content changes. When the width changes (a window being resized, a column re-flowing) the height follows at once, because a spring that chased a drag would lag behind it.
  • While it moves the box clips its content; at rest it clips nothing, so a focus ring or a shadow at its edge shows.
  • The server renders the box at its natural height with no inline height, so nothing is wrong without JavaScript or before hydration. The height is measured before the first paint, and the box does not move on its own then.
  • Reduced motion makes it jump: the height is animated with the imperative animate(), which <MotionConfig> does not stop, so the component reads the preference itself, in an effect and never while rendering.
  • It measures layout pixels (the observer's own figure), not getBoundingClientRect, so a scaled ancestor does not change the answer.

AI prompt

Paste into v0, Lovable, Bolt or Claude to rebuild this component in your own project.

Create a React + Tailwind CSS + Motion (motion/react) component called AutoHeight — a box that animates its height when its content changes.
- Props: className (the box's look: background, border, radius, padding), bounce (0.2), duration (0.4), children.
- A motion.div (flow-root) holding one inner div (flow-root) that carries the children. The motion.div has no inline height on the server
  and until it has been measured.
- In a layout effect: measure the inner div (offsetHeight) plus what the outer adds (its padding-top and -bottom and border-top-
  and -bottom widths, from getComputedStyle), put that in a motion value with jump(), then set a ready state so the box takes
  style={{ height: thatMotionValue }}. Observe the inner div with a ResizeObserver and use the entry's contentRect.height plus the same extra.
- When the observer reports a new height (more than 0.5px from the one it is heading for): if the inner width changed since the last
  report or prefers-reduced-motion: reduce matches (read it here), jump to it; otherwise animate(height, next, { type: "spring",
  bounce, duration }).
- Clip only while moving: overflow-hidden during a move, overflow-visible at rest (a state set on start and in onComplete).

Be the first to know

Get new templates and components first

One short email when we release a template or a free component. No spam — unsubscribe anytime.

We use your email only for Framebrick updates. Privacy policy