Back to EffectsYou are on Auto Height.

Auto Height

A container whose height glides to fit whatever is inside.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
auto-height.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/effects/auto-height.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/auto-height.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
auto-height.tsx
"use client";

import * as React from "react";
import {
  motion,
  type HTMLMotionProps,
  type TargetAndTransition,
  type Transition,
} from "motion/react";
import { cn } from "@/lib/utils";
import { Slot, useReduceMotion, useTempoTransition } from "../motion";
import { heightSpring, mergeRefs } from "./shared";

export type UseAutoHeightOptions = {
  includeParentBox?: boolean;
  includeSelfBox?: boolean;
};

export function useAutoHeight<T extends HTMLElement = HTMLDivElement>(
  deps: React.DependencyList = [],
  options: UseAutoHeightOptions = {},
) {
  const { includeParentBox = true, includeSelfBox = false } = options;
  const ref = React.useRef<T>(null);
  const [height, setHeight] = React.useState(0);

  const measure = React.useCallback(() => {
    const el = ref.current;
    if (!el) return 0;

    const base = el.getBoundingClientRect().height;
    let extra = 0;

    if (includeParentBox && el.parentElement) {
      const box = el.parentElement.getBoundingClientRect();
      const inner = Array.from(el.parentElement.children).reduce(
        (total, child) => total + child.getBoundingClientRect().height,
        0,
      );
      extra = Math.max(0, box.height - inner);
    }

    if (includeSelfBox) {
      const style = getComputedStyle(el);
      extra +=
        (parseFloat(style.paddingTop) || 0) +
        (parseFloat(style.paddingBottom) || 0) +
        (parseFloat(style.borderTopWidth) || 0) +
        (parseFloat(style.borderBottomWidth) || 0);
    }

    const dpr = typeof window === "undefined" ? 1 : window.devicePixelRatio || 1;
    return Math.ceil((base + extra) * dpr) / dpr;
  }, [includeParentBox, includeSelfBox]);

  React.useLayoutEffect(() => {
    const el = ref.current;
    if (!el) return;

    setHeight(measure());

    let frame = 0;
    const observer = new ResizeObserver(() => {
      cancelAnimationFrame(frame);
      frame = requestAnimationFrame(() => setHeight(measure()));
    });

    observer.observe(el);
    if (includeParentBox && el.parentElement) observer.observe(el.parentElement);

    return () => {
      cancelAnimationFrame(frame);
      observer.disconnect();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, deps);

  React.useLayoutEffect(() => {
    if (height !== 0) return;
    const next = measure();
    if (next !== 0) setHeight(next);
  }, [height, measure]);

  return { ref, height } as const;
}

export type AutoHeightProps = Omit<HTMLMotionProps<"div">, "animate"> & {
  children: React.ReactNode;
  deps?: React.DependencyList;
  animate?: TargetAndTransition;
  transition?: Transition;
  asChild?: boolean;
  wrapperClassName?: string;
};

export function AutoHeight({
  children,
  deps = [],
  animate,
  transition,
  asChild = false,
  wrapperClassName,
  className,
  style,
  ...props
}: AutoHeightProps) {
  const reduce = useReduceMotion();
  const spring = useTempoTransition(transition ?? heightSpring);
  const { ref, height } = useAutoHeight<HTMLDivElement>(deps);
  const Component = (asChild ? Slot : motion.div) as React.ElementType;

  return (
    <Component
      data-slot="auto-height"
      style={{ overflow: "hidden", ...style }}
      animate={{ height, ...animate }}
      transition={reduce ? { duration: 0 } : spring}
      className={className}
      {...props}
    >
      <div ref={ref} className={cn("w-full", wrapperClassName)}>
        {children}
      </div>
    </Component>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/auto-height.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

auto-height.tsx
import { AutoHeight } from "@/components/prequelui/effects/auto-height";

export default function Demo() {
  const [open, setOpen] = useState(false);
  return (
    <AutoHeight deps={[open]} className="w-[320px] overflow-hidden rounded-xl border">
      <p className="p-4 text-[13px] font-medium">Deployment summary</p>
      {open && <p className="px-4 pb-4 text-[13px] text-muted-foreground">…</p>}
    </AutoHeight>
  );
}
import { AutoHeight } from "@/components/prequelui/auto-height";

export function Variant() {
  return (
    <AutoHeight tempo={1.2}>
      …
    </AutoHeight>
  );
}
03

Animation

The container measures its child instead of guessing at a height. Every time the content, the font metrics or the parent box changes, a ResizeObserver reports the new size and the outer motion element springs to it with overflow hidden.

  • Height spring: stiffness 300, damping 30, bounce 0, restDelta 0.01.
  • The measured height is rounded to the device pixel ratio so text stays crisp.
  • The observer watches the child and the parent box, not only the root.
  • Reduced motion applies the new height instantly.
spring 300 / 30ResizeObserverDPR roundedinstant under reduced motion
04

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

AutoHeight

PropTypeDefault
childrenReactNode—
depsDependencyList[]
animateTargetAndTransition—
transitionTransitioncomponent spring or tween
asChildbooleanfalse
classNamestring—

useAutoHeight

PropTypeDefault
depsDependencyList[]
includeParentBoxbooleantrue
includeSelfBoxbooleanfalse

Next component

Blur

An entrance that resolves from blur to sharp.