Try 'magnetic button'
View code

Specification

Catalogue
No. 34 of 110
Section
Tabs
Interaction
Press, Layout Animation
Element
Tabs, Toggle, Counter
Preview
Live component
Imports
react 19.3.1, @number-flow/react 0.6.2
Added

Notes

A pill slides to the chosen option and the price rolls to its new value.

The change happens where the visitor is looking. The pill is placed by CSS alone, so it works before any script loads.

Pill 240ms, cubic-bezier(.22, 1, .36, 1). Placed with CSS mod() from --i. Digits roll with NumberFlow, tabular figures.

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 { Num, tabKey } 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 "./tab-pill.css";

function Tabs({ label, options, value, onChange }: { label: string; options: string[]; value: number; onChange: (i: number) => void }) {
  const idPrefix = useId();
  return (
    <div className="tabs" role="tablist" aria-label={label} style={{ "--n": options.length, "--i": value } as CSSProperties}>
      <span data-light aria-hidden="true" />
      {options.map((o, k) => (
        <button
          key={o}
          id={`${idPrefix}-${k}`}
          type="button"
          role="tab"
          aria-selected={value === k}
          tabIndex={value === k ? 0 : -1}
          onClick={() => onChange(k)}
          onKeyDown={(e) => {
            const n = tabKey(e.key, k, options.length);
            if (n === null) return;
            e.preventDefault();
            onChange(n);
            document.getElementById(`${idPrefix}-${n}`)?.focus();
          }}
        >
          {o}
        </button>
      ))}
    </div>
  );
}

function TabPillDemo() {
  const [yearly, setYearly] = useState(1);
  return (
    <div className="dm">
      <div className="dm-price dm-surface">
        <Tabs label="Billing" options={["Monthly", "Yearly"]} value={yearly} onChange={setYearly} />
        <div>
          <p style={{ fontWeight: 600 }}>Team</p>
          <p className="dm-amount">
            <Num value={yearly ? 24 : 29} format={{ style: "currency", currency: "GBP", maximumFractionDigits: 0 }} />
            <small>a month</small>
          </p>
        </div>
        <p className="dm-small">
          {yearly ? (
            <>
              Billed £288 a year. <span className="dm-save">Two months free.</span>
            </>
          ) : (
            "Billed monthly. Cancel any time."
          )}
        </p>
      </div>
    </div>
  );
}

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

export default function TabPillEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Billing toggle with rolling price" sample tour={TOUR}>
      <TabPillDemo />
    </PieceStage>
  );
}

More like this

Pricing period toggle

Press, Layout AnimationPricing

Estimate slider

Drag, TypeForms and inputs

Menu that opens into link cards

Press, Layout AnimationNavigation

Cookie choice that folds away

FeedbackOverlays and toasts

Bookshelf

Press, Layout AnimationMedia and galleries

Donut that changes period

Hover, PressCharts and numbers