Try 'magnetic button'
View code

Specification

Catalogue
No. 72 of 110
Interaction
Press, Layout Animation
Element
Picker, Form
Preview
Live component
Imports
react 19.3.1, @number-flow/react 0.6.2, framer-motion 12.40.0
Added

Notes

Cards to choose one from, each with a title, a short note and a price. One ring slides from the chosen card to the next, and a card that cannot be chosen says why ("Fully booked until 14 January").

Choosing a session length, a delivery speed or a plan on a booking or checkout page. The sliding ring makes the change of choice easy to follow, and the reason on an unavailable card saves a question to the client.

The ring travels between cards on a 0.3s spring with no bounce, measured from the cards' own layout so the stage's scale never throws it off. The cards are a radio group: arrow keys move and choose, skipping the unavailable card, and only the chosen card is in the tab order. Each card is at least 76px tall. Instant under reduced motion.

The sessions and prices are sample content.

Code

React and plain CSS
"use client";

import { useId, useLayoutEffect, useRef, useState } from "react";
import { motion } from "framer-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { physicalSpring } from "@/components/pieces/kit/physical-spring";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { radioArrows } from "@/components/pieces/shared";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./option-cards.css";

const OPTIONS = [
  { id: "30", title: "30 minutes", note: "A check-in or one problem", price: "£35" },
  { id: "60", title: "60 minutes", note: "Most first sessions", price: "£60" },
  { id: "90", title: "90 minutes", note: "Assessment and a plan", price: "£85" },
  { id: "home", title: "Home visit", note: "Fully booked until 14 January", price: "£110", disabled: true },
] as const;

type Box = { x: number; y: number; w: number; h: number };
const GLIDE = physicalSpring(0.3);

function OptionCardsDemo() {
  const reduce = useSafeReducedMotion();
  const uid = useId();
  const [sel, setSel] = useState("60");
  const cards = useRef<Record<string, HTMLButtonElement | null>>({});
  const [ring, setRing] = useState<Box | null>(null);

  // One ring travels between the cards, measured in layout pixels (offsets ignore the stage's scale).
  useLayoutEffect(() => {
    const el = cards.current[sel];
    if (!el) return;
    const read = () => setRing({ x: el.offsetLeft, y: el.offsetTop, w: el.offsetWidth, h: el.offsetHeight });
    read();
    const ro = new ResizeObserver(read);
    ro.observe(el);
    return () => ro.disconnect();
  }, [sel]);

  const chosen = OPTIONS.find((o) => o.id === sel);

  return (
    <div className="dm oc">
      <p id={`${uid}-l`} className="oc-label">
        Session length
      </p>
      <div className="oc-grid" role="radiogroup" aria-labelledby={`${uid}-l`} onKeyDown={radioArrows}>
        {ring && (
          <motion.span
            className="oc-ring"
            aria-hidden="true"
            initial={false}
            animate={{ x: ring.x, y: ring.y, width: ring.w, height: ring.h }}
            transition={reduce ? { duration: 0 } : GLIDE}
          />
        )}
        {OPTIONS.map((o) => {
          const on = o.id === sel;
          const off = "disabled" in o && o.disabled;
          return (
            <button
              key={o.id}
              ref={(el) => {
                cards.current[o.id] = el;
              }}
              type="button"
              role="radio"
              aria-checked={on}
              tabIndex={on ? 0 : -1}
              disabled={off || undefined}
              className="oc-card"
              onClick={() => setSel(o.id)}
            >
              <span className="oc-top">
                <span className="oc-title">{o.title}</span>
                <span className="oc-price">{o.price}</span>
              </span>
              <span className="oc-note">{o.note}</span>
              <span className="oc-dot" aria-hidden="true" />
            </button>
          );
        })}
      </div>
      <p className="oc-sum" aria-live="polite">
        {chosen ? `${chosen.title}, ${chosen.price}. Next, pick a day.` : ""}
      </p>
    </div>
  );
}

// What a hovered card shows: the ring moving to the second card, then the third.
const TOUR: Tour = [
  { click: ".oc-card", nth: 0, wait: 700 },
  { click: ".oc-card", nth: 2, wait: 900 },
];

export default function OptionCardsEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Option cards with a sliding ring" tour={TOUR} sample>
      <OptionCardsDemo />
    </PieceStage>
  );
}

More like this

Weekly hours editor

Press, Layout AnimationPickers and booking

Date range with nightly prices

PressPickers and booking

Seat map with a list view

PressPickers and booking

Price range over the prices it filters

DragForms and inputs

Billing toggle with rolling price

Press, Layout AnimationTabs

Button that grows into its form

Press, TypeButtons and toggles