Loaders JS behaviour

Morphing ring

A ring whose weight, break and breadth morph through keyframes while it turns; the percent inside counts up to its final tick. It completes in the refund card, and freezes red mid-way in the wallet top-up — Retry sets the very same ring turning again.

Documentation

Real scenarios

The component as it is used in a product — not a specimen grid.

A ring that never holds one shape

The morph ring trades weight for break — 3px to 12px, a long arc for tiny cracks — while a thin inner arc keeps the honest percent. In this refund card it runs to the tick; the top-up below dies at the gateway.

Refund · order #NB-2841

Amount€124.50
DestinationVisa card ··4242
ProcessorStripe · Frankfurt

The failure nobody designs for

The first charge here hits a 502: the ring turns red mid-lap, every animation pauses, the percent freezes at 58 — and Retry runs the exact same ring to green.

Nomad wallet · top-up

Amount€50.00
MethodMastercard ··7017
GatewayDublin · eu-ie-1

Important props

Important props
Prop Type Default What it does
morph keyframes '2.6s ease-in-out' Four keys: weight swings 3→12 while the dasharray trades a long arc for tiny cracks — the ring never holds one shape.
spin s '1.8' The whole group’s turn; the morph rotates with it so even the cracks keep changing direction.
progress % '0 → 100' A thin inner arc follows the percent while the number counts with it — exact reading under the morph’s deliberate chaos.
error % '58' On error the colour turns red, the animation pauses and the percent stays put until the retry.
track r '50' The grey path under both arcs; pathLength=100 keeps the percent maths painless.

Snippet

<div class="ldm-wrap" data-state="working" role="progressbar" aria-valuenow="62" aria-valuemin="0" aria-valuemax="100">
<svg viewBox="0 0 120 120" aria-hidden="true">
<circle class="ldm-track" cx="60" cy="60" r="50" pathLength="100"/>
<circle class="ldm-morph" cx="60" cy="60" r="50" pathLength="100"/>
<circle class="ldm-fill" cx="60" cy="60" r="50" pathLength="100" style="stroke-dashoffset: 38"/>
</svg>
<b>62%</b>
</div>

<style>
.ldm-wrap { position: relative; display: grid; place-items: center; inline-size: 7.5rem; aspect-ratio: 1;
            color: #7c3aed; font: 700 1.3rem/1 'Inter', 'Vazirmatn', sans-serif; }
.ldm-wrap svg { position: absolute; inset: 0; }
.ldm-wrap b { position: relative; }
.ldm-track, .ldm-morph, .ldm-fill { fill: none; }
.ldm-track { stroke: #ede9fe; stroke-width: 3; }
.ldm-fill { stroke: color-mix(in oklab, currentColor 35%, transparent); stroke-width: 3;
            stroke-dasharray: 100; transform: rotate(-90deg); transform-origin: 50% 50%; }
.ldm-morph { stroke: currentColor; stroke-linecap: round;
             animation: ldm-morph 2.6s ease-in-out infinite, ldm-spin 1.8s linear infinite; }
.ldm-wrap[data-state='error'] .ldm-morph { animation-play-state: paused; }
@keyframes ldm-morph {
0%, 100% { stroke-width: 3;  stroke-dasharray: 58 42; }
28%      { stroke-width: 10; stroke-dasharray: 22 30; }
52%      { stroke-width: 5;  stroke-dasharray: 84 16; }
76%      { stroke-width: 12; stroke-dasharray: 10 26; }
}
@keyframes ldm-spin { to { rotate: 360deg; } }
</style>
// app/Pages/Wallet.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link in the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import TopUpRing from '@/components/TopUpRing';

export default function Wallet() {
  return (
    <NabuXUIProvider locale="en" linkComponent={Link}>
      <main className="nx-page">
        <TopUpRing amount={50} failAt={58} />
      </main>
    </NabuXUIProvider>
  );
}
// TopUpRing.jsx — a morphing ring that counts up, freezes red on gateway error
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';

export default function TopUpRing({ amount = 50, failAt = 58, workMs = 4200 }) {
  const [state, setState] = useState('idle'); // idle | working | error | done
  const [p, setP] = useState(0);
  const retried = useRef(false);
  const ticks = useRef<number[]>([]);

  // Unmount clears every interval — no ring spins on behind the page.
  useEffect(() => () => ticks.current.forEach(clearInterval), []);

  const run = () => {
    if (state === 'working') return;
    setState('working');
    setP(0);
    const step = 100 / (workMs / 55);
    const t = window.setInterval(() => {
      setP((prev) => {
        const next = Math.min(100, prev + step * (0.7 + Math.random() * 0.6));
        if (!retried.current && next >= failAt) {
          clearInterval(t);
          retried.current = true;
          setState('error');
          return failAt;
        }
        if (next >= 100) { clearInterval(t); setState('done'); }
        return next;
      });
    }, 55);
    ticks.current.push(t);
  };

  return (
    <>
      <style>{`
        .ldm-card { display: grid; justify-items: center; gap: 1rem; padding: 1.4rem;
                    border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
                    max-inline-size: 22rem; }
        .ldm-wrap { position: relative; display: grid; place-items: center; inline-size: 7.5rem; aspect-ratio: 1;
                    color: #7c3aed; font: 700 1.3rem/1 inherit; }
        .ldm-wrap[data-state='error'] { color: #dc2626; }
        .ldm-wrap[data-state='done'] { color: #059669; }
        .ldm-wrap svg { position: absolute; inset: 0; }
        .ldm-track, .ldm-morph, .ldm-fill { fill: none; }
        .ldm-track { stroke: #ede9fe; stroke-width: 3; }
        .ldm-fill { stroke: color-mix(in oklab, currentColor 35%, transparent); stroke-width: 3;
                    stroke-dasharray: 100; transform: rotate(-90deg); transform-origin: 50% 50%; }
        .ldm-morph { stroke: currentColor; stroke-linecap: round;
                     animation: ldm-morph 2.6s ease-in-out infinite, ldm-spin 1.8s linear infinite; }
        .ldm-wrap[data-state='done'] .ldm-morph { display: none; }
        .ldm-wrap[data-state='error'] .ldm-morph { animation-play-state: paused; }
        @keyframes ldm-morph {
          0%, 100% { stroke-width: 3;  stroke-dasharray: 58 42; }
          28%      { stroke-width: 10; stroke-dasharray: 22 30; }
          52%      { stroke-width: 5;  stroke-dasharray: 84 16; }
          76%      { stroke-width: 12; stroke-dasharray: 10 26; }
        }
        @keyframes ldm-spin { to { rotate: 360deg; } }
        .ldm-btn { block-size: 2.6rem; padding-inline: 1.2rem; border: none; border-radius: 999px;
                   background: #7c3aed; color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
        .ldm-btn:disabled { cursor: default; }
        .ldm-err { color: #dc2626; font-size: .8rem; }
        @media (prefers-reduced-motion: reduce) { .ldm-morph { animation: none; stroke-width: 6; stroke-dasharray: 58 42; } }
      `}</style>
      <div className="ldm-card">
        <div className="ldm-wrap" data-state={state} role="progressbar"
             aria-valuenow={Math.round(p)} aria-valuemin={0} aria-valuemax={100}>
          <svg viewBox="0 0 120 120" aria-hidden="true">
            <circle className="ldm-track" cx="60" cy="60" r="50" pathLength="100" />
            <circle className="ldm-morph" cx="60" cy="60" r="50" pathLength="100" />
            <circle className="ldm-fill" cx="60" cy="60" r="50" pathLength="100"
                    style={{ strokeDashoffset: 100 - Math.round(p) }} />
          </svg>
          <b>{Math.round(p)}%</b>
        </div>
        <button type="button" className="ldm-btn" onClick={run} disabled={state === 'working'}>
          {state === 'error' ? 'Retry' : `Top up $${amount}`}
        </button>
        <p className="ldm-err" role="status">
          {state === 'error' ? 'Gateway did not answer (502).' : state === 'done' ? 'Balance updated.' : ''}
        </p>
      </div>
    </>
  );
}
<!-- TopUpRing.vue — a morphing ring that counts up, freezes red on gateway error -->
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import '@nabuxai/ui-core/css';

const props = defineProps<{ amount?: number; failAt?: number; workMs?: number }>();
const state = ref<'idle' | 'working' | 'error' | 'done'>('idle');
const p = ref(0);
let retried = false;
const ticks: number[] = [];

const run = () => {
  if (state.value === 'working') return;
  state.value = 'working';
  p.value = 0;
  const step = 100 / ((props.workMs ?? 4200) / 55);
  const t = window.setInterval(() => {
    p.value = Math.min(100, p.value + step * (0.7 + Math.random() * 0.6));
    if (!retried && p.value >= (props.failAt ?? 58)) {
      clearInterval(t); retried = true; state.value = 'error'; p.value = props.failAt ?? 58;
    } else if (p.value >= 100) {
      clearInterval(t); state.value = 'done';
    }
  }, 55);
  ticks.push(t);
};

// Unmount clears every interval — no ring spins on behind the page.
onBeforeUnmount(() => ticks.forEach(clearInterval));
</script>

<template>
  <div class="ldm-card">
    <div class="ldm-wrap" :data-state="state" role="progressbar"
         :aria-valuenow="Math.round(p)" aria-valuemin="0" aria-valuemax="100">
      <svg viewBox="0 0 120 120" aria-hidden="true">
        <circle class="ldm-track" cx="60" cy="60" r="50" pathLength="100" />
        <circle class="ldm-morph" cx="60" cy="60" r="50" pathLength="100" />
        <circle class="ldm-fill" cx="60" cy="60" r="50" pathLength="100"
                :style="{ strokeDashoffset: 100 - Math.round(p) }" />
      </svg>
      <b>{{ Math.round(p) }}%</b>
    </div>
    <button type="button" class="ldm-btn" :disabled="state === 'working'" @click="run">
      {{ state === 'error' ? 'Retry' : `Top up $${props.amount ?? 50}` }}
    </button>
    <p class="ldm-err" role="status">
      {{ state === 'error' ? 'Gateway did not answer (502).' : state === 'done' ? 'Balance updated.' : '' }}
    </p>
  </div>
</template>

<style scoped>
.ldm-card { display: grid; justify-items: center; gap: 1rem; padding: 1.4rem;
            border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
            max-inline-size: 22rem; }
.ldm-wrap { position: relative; display: grid; place-items: center; inline-size: 7.5rem; aspect-ratio: 1;
            color: #7c3aed; font: 700 1.3rem/1 inherit; }
.ldm-wrap[data-state='error'] { color: #dc2626; }
.ldm-wrap[data-state='done'] { color: #059669; }
.ldm-wrap svg { position: absolute; inset: 0; }
.ldm-track, .ldm-morph, .ldm-fill { fill: none; }
.ldm-track { stroke: #ede9fe; stroke-width: 3; }
.ldm-fill { stroke: color-mix(in oklab, currentColor 35%, transparent); stroke-width: 3;
            stroke-dasharray: 100; transform: rotate(-90deg); transform-origin: 50% 50%; }
.ldm-morph { stroke: currentColor; stroke-linecap: round;
             animation: ldm-morph 2.6s ease-in-out infinite, ldm-spin 1.8s linear infinite; }
.ldm-wrap[data-state='done'] .ldm-morph { display: none; }
.ldm-wrap[data-state='error'] .ldm-morph { animation-play-state: paused; }
@keyframes ldm-morph {
  0%, 100% { stroke-width: 3;  stroke-dasharray: 58 42; }
  28%      { stroke-width: 10; stroke-dasharray: 22 30; }
  52%      { stroke-width: 5;  stroke-dasharray: 84 16; }
  76%      { stroke-width: 12; stroke-dasharray: 10 26; }
}
@keyframes ldm-spin { to { rotate: 360deg; } }
.ldm-btn { block-size: 2.6rem; padding-inline: 1.2rem; border: none; border-radius: 999px;
           background: #7c3aed; color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
.ldm-btn:disabled { cursor: default; }
.ldm-err { color: #dc2626; font-size: .8rem; }
@media (prefers-reduced-motion: reduce) { .ldm-morph { animation: none; stroke-width: 6; stroke-dasharray: 58 42; } }
</style>
<!-- TopUpRing.svelte — a morphing ring that counts up, freezes red on gateway error -->
<script lang="ts">
  import { onDestroy } from 'svelte';
  import '@nabuxai/ui-core/css';

  let { amount = 50, failAt = 58, workMs = 4200 } = $props();
  let state = $state<'idle' | 'working' | 'error' | 'done'>('idle');
  let p = $state(0);
  let retried = false;
  const ticks: number[] = [];

  const run = () => {
    if (state === 'working') return;
    state = 'working';
    p = 0;
    const step = 100 / (workMs / 55);
    const t = window.setInterval(() => {
      p = Math.min(100, p + step * (0.7 + Math.random() * 0.6));
      if (!retried && p >= failAt) {
        clearInterval(t); retried = true; state = 'error'; p = failAt;
      } else if (p >= 100) {
        clearInterval(t); state = 'done';
      }
    }, 55);
    ticks.push(t);
  };

  // Unmount clears every interval — no ring spins on behind the page.
  onDestroy(() => ticks.forEach(clearInterval));
</script>

<div class="ldm-card">
  <div class="ldm-wrap" data-state={state} role="progressbar"
       aria-valuenow={Math.round(p)} aria-valuemin="0" aria-valuemax="100">
    <svg viewBox="0 0 120 120" aria-hidden="true">
      <circle class="ldm-track" cx="60" cy="60" r="50" pathLength="100" />
      <circle class="ldm-morph" cx="60" cy="60" r="50" pathLength="100" />
      <circle class="ldm-fill" cx="60" cy="60" r="50" pathLength="100"
              style={`stroke-dashoffset: ${100 - Math.round(p)}`} />
    </svg>
    <b>{Math.round(p)}%</b>
  </div>
  <button type="button" class="ldm-btn" disabled={state === 'working'} onclick={run}>
    {state === 'error' ? 'Retry' : `Top up $${amount}`}
  </button>
  <p class="ldm-err" role="status">
    {state === 'error' ? 'Gateway did not answer (502).' : state === 'done' ? 'Balance updated.' : ''}
  </p>
</div>

<style>
  .ldm-card { display: grid; justify-items: center; gap: 1rem; padding: 1.4rem;
              border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
              max-inline-size: 22rem; }
  .ldm-wrap { position: relative; display: grid; place-items: center; inline-size: 7.5rem; aspect-ratio: 1;
              color: #7c3aed; font: 700 1.3rem/1 inherit; }
  .ldm-wrap[data-state='error'] { color: #dc2626; }
  .ldm-wrap[data-state='done'] { color: #059669; }
  .ldm-wrap svg { position: absolute; inset: 0; }
  .ldm-track, .ldm-morph, .ldm-fill { fill: none; }
  .ldm-track { stroke: #ede9fe; stroke-width: 3; }
  .ldm-fill { stroke: color-mix(in oklab, currentColor 35%, transparent); stroke-width: 3;
              stroke-dasharray: 100; transform: rotate(-90deg); transform-origin: 50% 50%; }
  .ldm-morph { stroke: currentColor; stroke-linecap: round;
               animation: ldm-morph 2.6s ease-in-out infinite, ldm-spin 1.8s linear infinite; }
  .ldm-wrap[data-state='done'] .ldm-morph { display: none; }
  .ldm-wrap[data-state='error'] .ldm-morph { animation-play-state: paused; }
  @keyframes ldm-morph {
    0%, 100% { stroke-width: 3;  stroke-dasharray: 58 42; }
    28%      { stroke-width: 10; stroke-dasharray: 22 30; }
    52%      { stroke-width: 5;  stroke-dasharray: 84 16; }
    76%      { stroke-width: 12; stroke-dasharray: 10 26; }
  }
  @keyframes ldm-spin { to { rotate: 360deg; } }
  .ldm-btn { block-size: 2.6rem; padding-inline: 1.2rem; border: none; border-radius: 999px;
             background: #7c3aed; color: #fff; font: 600 .88rem/1 inherit; cursor: pointer; }
  .ldm-btn:disabled { cursor: default; }
  .ldm-err { color: #dc2626; font-size: .8rem; }
  @media (prefers-reduced-motion: reduce) { .ldm-morph { animation: none; stroke-width: 6; stroke-dasharray: 58 42; } }
</style>