Try 'magnetic button'
View code

Number 96: Currency swap that trades places

Specification

Catalogue
No. 96 of 110
Interaction
Press, Layout Animation
Element
Input, Button
Preview
Live component
Imports
react 19.3.1
Added

Notes

What you send on top, what they receive underneath, and a round button on the seam between them. Press it and the two currencies trade places: the top side arrives from below, the bottom from above, the button turns half a turn, and the received amount is worked out again at the rate after a small fee.

The button is ringed in the page colour so it looks cut into the seam rather than laid on it, and it keeps turning the same way, so every press reads as one more swap rather than a toggle back. The labels stay put (You send, They receive); only the currencies move.

The rate and fee are sample figures.

Code

React and plain CSS
"use client";

import { useEffect, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./asset-swap.module.css";

type Currency = { code: string; symbol: string; hue: number; perGbp: number };

const GBP: Currency = { code: "GBP", symbol: "£", hue: 6, perGbp: 1 };
const EUR: Currency = { code: "EUR", symbol: "€", hue: 1, perGbp: 1.1702 };
/** What the visitor sends, in the currency on top. */
const AMOUNT = 250;
/** The provider's margin, so the two sides never quite match. */
const FEE = 0.0062;
const DEMO_EVERY = 1600;

const money = (n: number) => n.toLocaleString("en-GB", { minimumFractionDigits: 2, maximumFractionDigits: 2 });

function Side({ label, currency, amount, hint, moved }: { label: string; currency: Currency; amount: number; hint: string; moved: boolean }) {
  return (
    <div className={styles.side} data-moved={moved || undefined}>
      <span className={styles.label}>{label}</span>
      <span className={styles.amount}>{money(amount)}</span>
      <span className={styles.hint}>{hint}</span>
      <span className={styles.badge} style={{ "--hue": `var(--cat-${currency.hue}, var(--accent, currentColor))` } as CSSProperties}>
        <span className={styles.mark} aria-hidden="true">
          {currency.symbol}
        </span>
        {currency.code}
      </span>
    </div>
  );
}

export default function AssetSwap({ active, context }: LivePreviewProps) {
  const card = context === "card";
  const [flipped, setFlipped] = useState(false);
  const [turns, setTurns] = useState(0);
  const demo = card && active;

  const [running, setRunning] = useState(demo);
  if (running !== demo) {
    setRunning(demo);
    setFlipped(false);
  }

  useEffect(() => {
    if (!demo) return;
    const id = window.setInterval(() => {
      setFlipped((f) => !f);
      setTurns((t) => t + 1);
    }, DEMO_EVERY);
    return () => window.clearInterval(id);
  }, [demo]);

  const pay = flipped ? EUR : GBP;
  const get = flipped ? GBP : EUR;
  const received = AMOUNT * (get.perGbp / pay.perGbp) * (1 - FEE);
  const rate = `1 ${pay.code} = ${(get.perGbp / pay.perGbp).toFixed(4)} ${get.code}`;

  return (
    <div className={styles.area}>
      <div className={styles.pair} data-flipped={flipped || undefined}>
        <div className={styles.slot} data-pos="top">
          <Side key={turns} label="You send" currency={pay} amount={AMOUNT} hint={rate} moved={turns > 0} />
        </div>
        <div className={styles.slot} data-pos="bottom">
          <Side key={turns} label="They receive" currency={get} amount={received} hint={`After a ${(FEE * 100).toFixed(2)}% fee`} moved={turns > 0} />
        </div>
        <button
          type="button"
          className={styles.swap}
          style={{ "--turns": turns } as CSSProperties}
          tabIndex={card ? -1 : 0}
          aria-label={`Swap: send ${get.code}, receive ${pay.code}`}
          onClick={() => {
            setFlipped((f) => !f);
            setTurns((t) => t + 1);
          }}
        >
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <path d="M12 5v14M6 13l6 6 6-6" />
          </svg>
        </button>
      </div>
      <p className="visually-hidden" role="status">
        {`Sending ${money(AMOUNT)} ${pay.code}, they receive ${money(received)} ${get.code}.`}
      </p>
    </div>
  );
}

More like this

Drops that merge like goo

Press, Layout AnimationButtons and toggles

Delete that asks where you clicked

Press, FeedbackButtons and toggles

Code field that behaves as one input

Type, FeedbackForms and inputs

Bookshelf

Press, Layout AnimationMedia and galleries

Slide to confirm

Drag, FeedbackButtons and toggles

Option cards with a sliding ring

Press, Layout AnimationPickers and booking