Card ring

Cards set on a turning ring. The one in front faces you; its neighbours have gone round the back, mirrored, with their colour wrapping away toward the edges.

Voice warmth
for every reply

25%

FormalFriendly

Tone settings

Drafts kept
for each post

12

NoneAll

Version history

Seats in use
on this plan

8

010

Team and billing

Storage used
this month

64%

EmptyFull

Files and media

Render hours
left this week

72

0100

Render engine 2.0

Options

Screen

Code

import { useLayoutEffect, useRef, useState } from "react";
import "./card-ring.css";

const places = { 0: "front", 1: "right", [-1]: "left" };
const strips = 36;
const kept = 0.5;
const reach = 0.72;

const bend = (card, side) => {
  if ((card.dataset.bend || "") === side) return;
  card.querySelectorAll(".dh-ring-wing").forEach((wing) => wing.remove());
  if (!side) return card.removeAttribute("data-bend");
  card.dataset.bend = side;
  const face = card.querySelector(".dh-ring-face");
  const wide = card.offsetWidth;
  const tall = card.offsetHeight;
  const start = Math.round(wide * kept);
  const taken = wide - start;
  const out = taken + Math.round(wide * reach);
  const power = out / taken;
  const most = 1.75;
  const source = (x) => start + taken * (1 - Math.pow(1 - x / out, power));
  const growth = (x) => 1 + (most - 1) * Math.pow(Math.max(0, (x - taken) / (out - taken)), 1.5);

  const wing = document.createElement("div");
  wing.className = "dh-ring-wing";
  wing.dataset.side = side;
  wing.setAttribute("aria-hidden", "true");
  wing.style.left = start + "px";
  wing.style.width = out + "px";
  wing.style.top = -(most - 1) * tall / 2 + "px";
  wing.style.height = tall * most + "px";
  const rim = document.createElement("i");
  const body = document.createElement("div");
  const edge = (inset) => {
    const top = [];
    const bottom = [];
    for (let step = 0; step <= 24; step++) {
      const x = step / 24 * out;
      const half = growth(x) / most * 50 - inset * Math.max(0, (x - taken) / (out - taken));
      top.push((x / out * 100).toFixed(2) + "% " + (50 - half).toFixed(2) + "%");
      bottom.unshift((x / out * 100).toFixed(2) + "% " + (50 + half).toFixed(2) + "%");
    }
    return "polygon(" + top.concat(bottom).join(", ") + ")";
  };
  rim.style.clipPath = edge(0);
  body.style.clipPath = edge(1.2);

  for (let index = 0; index < strips * 2 + 1; index++) {
    const under = index > strips;
    const place = under ? index - strips - 1 : index - 0.5;
    const left = Math.max(0, Math.round(place / strips * out));
    const right = Math.min(out, Math.round((place + 1) / strips * out));
    const begin = source(left);
    const stretch = (right - left) / (source(right) - begin);
    const grow = growth((left + right) / 2);
    const until = source(right) + 2 / stretch;
    const copy = face.cloneNode(true);
    copy.style.inset = "auto";
    copy.style.left = left + "px";
    copy.style.top = (most - 1) * tall / 2 + "px";
    copy.style.width = wide + "px";
    copy.style.height = tall + "px";
    copy.style.transform =
      "translateX(" + -begin * stretch + "px) scale(" + stretch + ", " + grow + ")" +
      (side === "start" ? " translateX(" + wide + "px) scaleX(-1)" : "");
    copy.style.clipPath =
      side === "start"
        ? "inset(0 " + begin + "px 0 " + Math.max(0, wide - until) + "px)"
        : "inset(0 " + Math.max(0, wide - until) + "px 0 " + begin + "px)";
    const past = (grow - 1) / (most - 1);
    if (past > 0.01) copy.style.filter = "blur(" + (past * 7).toFixed(1) + "px)";
    if (under) body.append(copy);
    else body.prepend(copy);
  }
  wing.append(rim, body);
  card.append(wing);
};

export function CardRing({ cards, label = "Cards" }) {
  const [front, setFront] = useState(0);
  const from = useRef(null);
  const ring = useRef(null);
  const turn = (by) => setFront((now) => (now + by + cards.length) % cards.length);

  useLayoutEffect(() => {
    const fit = () =>
      ring.current.querySelectorAll(".dh-ring-card").forEach((card) => {
        bend(card, "");
        bend(card, card.dataset.at === "left" ? "end" : card.dataset.at === "right" ? "start" : "");
      });
    fit();
    const watcher = new ResizeObserver(fit);
    watcher.observe(ring.current);
    return () => watcher.disconnect();
  }, [front, cards]);

  return (
    <section className="dh-ring-frame">
      <div
        className="dh-ring"
        ref={ring}
        tabIndex={0}
        role="group"
        aria-roledescription="carousel"
        aria-label={label}
        onKeyDown={(event) => {
          if (event.key === "ArrowLeft") turn(-1);
          if (event.key === "ArrowRight") turn(1);
        }}
        onPointerDown={(event) => (from.current = event.clientX)}
        onPointerUp={(event) => {
          if (from.current === null) return;
          const moved = event.clientX - from.current;
          from.current = null;
          if (Math.abs(moved) > 40) turn(moved < 0 ? 1 : -1);
        }}
      >
        {cards.map((card, index) => {
          let away = (index - front + cards.length) % cards.length;
          if (away > cards.length / 2) away -= cards.length;
          const at = places[away] || (away > 0 ? "far-right" : "far-left");
          return (
            <article
              className="dh-ring-card"
              key={card.title}
              data-tone={card.tone}
              data-at={at}
              aria-hidden={away !== 0}
              style={{ "--dh-ring-value": card.at + "%" }}
              onClick={() => (at === "left" ? turn(-1) : at === "right" ? turn(1) : null)}
            >
              <div className="dh-ring-face">
                <h3>{card.title}</h3>
                <div className="dh-ring-figure">
                  <strong>
                    {card.value}
                    {card.unit && <sup>{card.unit}</sup>}
                  </strong>
                  <div className="dh-ring-meter">
                    <i />
                  </div>
                  <p>
                    <span>{card.low}</span>
                    <span>{card.high}</span>
                  </p>
                </div>
                <footer>{card.foot}</footer>
              </div>
            </article>
          );
        })}
      </div>
      <div className="dh-ring-steps">
        <button type="button" aria-label="Previous card" onClick={() => turn(-1)}>
          <svg viewBox="0 0 16 16" aria-hidden="true">
            <path d="M9.5 3.5 5 8l4.5 4.5" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </button>
        <button type="button" aria-label="Next card" onClick={() => turn(1)}>
          <svg viewBox="0 0 16 16" aria-hidden="true">
            <path d="M6.5 3.5 11 8l-4.5 4.5" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </button>
      </div>
    </section>
  );
}

Voice warmth
for every reply

25%

FormalFriendly

Tone settings

Drafts kept
for each post

12

NoneAll

Version history

Seats in use
on this plan

8

010

Team and billing

Storage used
this month

64%

EmptyFull

Files and media

Render hours
left this week

72

0100

Render engine 2.0