Try 'magnetic button'
View code

Specification

Catalogue
No. 58 of 110
Interaction
Layout Animation
Element
Accordion
Preview
Live component
Imports
react 19.3.1, @number-flow/react 0.6.2
Added

Notes

When content gets longer or shorter, the box grows to fit instead of jumping. Toggle it off to compare.

A jump moves everything under it and loses the reader's place. This glides the height while the content changes, and is the part inside the questionnaire, stepper and swipe tabs.

Height follows a ResizeObserver, 300ms cubic-bezier(.22, 1, .36, 1). Height is auto on the server, so first paint matches. Instant under reduced motion.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useId, useState, type CSSProperties } from "react";
import { AutoHeight } from "@/components/pieces/motion-helpers";
import { radioArrows } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/tabs.css";
import "./auto-height.css";

const INCLUDED = ["Kitchen: oven, hob, extractor, inside the fridge", "Bathrooms: limescale off taps, screens and tiles", "Every room: skirting, doors, light switches", "Windows: inside glass and frames", "Floors: hoovered and mopped, rugs beaten"];

function AutoHeightDemo() {
  const [full, setFull] = useState(false);
  const [glide, setGlide] = useState(true);
  const uid = useId();
  return (
    <div className="dm">
      <div className="ah">
        <div className="ah-card">
          <div className="ah-head">
            <strong>Deep clean</strong>
            <div className="tabs ah-tabs" role="radiogroup" aria-label="Detail" style={{ "--n": 2, "--i": full ? 1 : 0 } as CSSProperties} onKeyDown={radioArrows}>
              <span data-light aria-hidden="true" />
              {["Summary", "Full list"].map((l, k) => (
                <button key={l} type="button" role="radio" aria-checked={full === (k === 1)} onClick={() => setFull(k === 1)}>
                  {l}
                </button>
              ))}
            </div>
          </div>
          <AutoHeight off={!glide}>
            {full ? (
              <ul className="ah-list">
                {INCLUDED.map((l) => (
                  <li key={l}>{l}</li>
                ))}
              </ul>
            ) : (
              <p className="ah-sum">Top to bottom in one visit: kitchen, bathrooms, every room and the inside of the windows.</p>
            )}
          </AutoHeight>
        </div>
        <label className="ah-switch" htmlFor={`${uid}-g`}>
          <input id={`${uid}-g`} type="checkbox" role="switch" checked={glide} onChange={(e) => setGlide(e.target.checked)} />
          Glide the height
        </label>
      </div>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { click: '[role="radio"]', nth: 1, wait: 1600 },
  { click: '[role="radio"]', nth: 0 },
];

export default function AutoHeightEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Height that glides" sample tour={TOUR}>
      <AutoHeightDemo />
    </PieceStage>
  );
}

More like this

FAQ that opens smoothly

Layout AnimationCards and accordions

Read more that glides open

Layout AnimationText and code

Dialogs and sheets

Layout AnimationOverlays and toasts

Reviews that take turns

Layout AnimationCards and accordions

Form in named steps

Type, FeedbackForms and inputs

Menu that opens into link cards

Press, Layout AnimationNavigation