Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/auto-height.jsonFirst 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.
"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.jsonDependencies
motion lucide-reactThe 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.
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.
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>
);
}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.
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
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | — |
| deps | DependencyList | [] |
| animate | TargetAndTransition | — |
| transition | Transition | component spring or tween |
| asChild | boolean | false |
| className | string | — |
useAutoHeight
| Prop | Type | Default |
|---|---|---|
| deps | DependencyList | [] |
| includeParentBox | boolean | true |
| includeSelfBox | boolean | false |