Loaders JS behaviour

Percent loader

A progress ring with a number counting smoothly at its heart and a stage label trading on thresholds — gathering, compressing, uploading. The cancel button genuinely cancels: the ring stays exactly where it stopped, greys out and offers a restart; at 100% the file is ready and the download button wakes.

Documentation

Real scenarios

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

A percent that tells the truth

The export card of an analytics product: the ring eases round while the number counts and the stage label trades underneath. Cancel honestly aborts — the ring greys out exactly where it stopped and offers a restart; at 100% the download wakes.

Compass · Q4 report export

Q4-report.pdf · MB / MB

Important props

Important props
Prop Type Default What it does
progress % '0 → 100' The count eases in-out on requestAnimationFrame; the number glides instead of stuttering.
stages array '[{at: 0}, {at: 35}, {at: 75}]' The stage label swaps on percent thresholds and the checklist ticks over in the same breath.
duration ms '6400' The whole export’s length; longer than a classic spinner because real work sits behind it.
cancel action 'abort' Cancel aborts the count mid-flight; the ring greys out in place until you start over.
ring geometry 'r 54 · dash 339.3' The ring’s geometry; the arc follows the percent through stroke-dashoffset, round-capped.

Snippet

<div class="ldc-wrap" data-state="working" role="progressbar" aria-valuenow="62" aria-valuemin="0" aria-valuemax="100">
<svg viewBox="0 0 128 128" aria-hidden="true">
<circle class="ldc-track" cx="64" cy="64" r="54"/>
<circle class="ldc-arc" cx="64" cy="64" r="54" style="stroke-dashoffset: 128.9"/>
</svg>
<div class="ldc-center">
<b>62%</b>
<small>Compressing…</small>
</div>
</div>
<button type="button" class="ldc-cancel">Cancel</button>

<style>
.ldc-wrap { position: relative; display: grid; place-items: center; inline-size: 9rem; aspect-ratio: 1;
            font: 600 1.5rem/1 'Inter', 'Vazirmatn', sans-serif; }
.ldc-wrap svg { position: absolute; inset: 0; }
.ldc-center { position: relative; display: grid; gap: .3rem; justify-items: center; }
.ldc-center small { font-size: .62rem; color: #78716c; }
.ldc-track, .ldc-arc { fill: none; }
.ldc-track { stroke: #e7e5e4; stroke-width: 7; }
.ldc-arc { stroke: #0f766e; stroke-width: 7; stroke-linecap: round;
           stroke-dasharray: 339.3; transform: rotate(-90deg); transform-origin: 50% 50%; }
.ldc-wrap[data-state='cancelled'] .ldc-arc { stroke: #a8a29e; }
.ldc-cancel { margin-block-start: 1rem; padding: .55rem 1.2rem; border: 1px solid #e7e5e4;
              border-radius: 999px; background: none; font: 600 .8rem/1 inherit; cursor: pointer; }
</style>
// app/Pages/Analytics.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 ExportRing from '@/components/ExportRing';

export default function Analytics() {
  return (
    <NabuXUIProvider locale="en" linkComponent={Link}>
      <main className="nx-page">
        <ExportRing workMs={6400} />
      </main>
    </NabuXUIProvider>
  );
}
// ExportRing.jsx — percent loader with live stages and a cancel that cancels
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';

const C = 339.3; // 2π × r54
const STAGES = [
  { at: 0, label: 'Gathering data' },
  { at: 35, label: 'Compressing' },
  { at: 75, label: 'Uploading' },
];

export default function ExportRing({ workMs = 6400 }) {
  const [state, setState] = useState('idle'); // idle | working | cancelled | done
  const [p, setP] = useState(0);
  const raf = useRef(0);

  // Unmount aborts the frame loop — nothing counts on behind the page.
  useEffect(() => () => cancelAnimationFrame(raf.current), []);

  const run = () => {
    if (state === 'working') return;
    setState('working');
    setP(0);
    const t0 = performance.now();
    const tick = (now) => {
      const t = Math.min(1, (now - t0) / workMs);
      const eased = t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
      setP(eased * 100);
      if (t >= 1) { setState('done'); return; }
      raf.current = requestAnimationFrame(tick);
    };
    raf.current = requestAnimationFrame(tick);
  };

  const cancel = () => {
    if (state !== 'working') return;
    cancelAnimationFrame(raf.current);
    setState('cancelled');
  };

  const stageIdx = Math.max(0, STAGES.findLastIndex((s) => p >= s.at));

  return (
    <>
      <style>{`
        .ldc-card { display: grid; gap: 1.2rem; justify-items: center; padding: 1.4rem;
                    border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
                    max-inline-size: 21rem; }
        .ldc-wrap { position: relative; display: grid; place-items: center; inline-size: 9rem; aspect-ratio: 1;
                    font: 600 1.5rem/1 inherit; }
        .ldc-wrap svg { position: absolute; inset: 0; }
        .ldc-center { position: relative; display: grid; gap: .3rem; justify-items: center; }
        .ldc-center small { font-size: .62rem; color: #78716c; }
        .ldc-track, .ldc-arc { fill: none; }
        .ldc-track { stroke: #e7e5e4; stroke-width: 7; }
        .ldc-arc { stroke: #0f766e; stroke-width: 7; stroke-linecap: round; stroke-dasharray: 339.3;
                   transform: rotate(-90deg); transform-origin: 50% 50%; }
        .ldc-wrap[data-state='cancelled'] .ldc-arc { stroke: #a8a29e; }
        .ldc-stages { display: grid; gap: .4rem; justify-items: start; font-size: .8rem; }
        .ldc-stage { display: flex; gap: .5rem; align-items: center; color: #78716c; }
        .ldc-stage[data-s='done'] { color: #0f766e; }
        .ldc-stage[data-s='active'] { color: #1c1917; font-weight: 600; }
        .ldc-stage i { inline-size: .55rem; aspect-ratio: 1; border-radius: 50%; background: currentColor; }
        .ldc-foot { display: flex; gap: .7rem; }
        .ldc-btn { block-size: 2.4rem; padding-inline: 1.15rem; border: none; border-radius: 999px;
                   font: 600 .84rem/1 inherit; cursor: pointer; }
        .ldc-btn[data-kind='cancel'] { border: 1px solid #e7e5e4; background: none; color: #1c1917; }
        .ldc-btn[data-kind='go'] { background: #0f766e; color: #fff; }
        .ldc-btn[data-kind='idle'] { background: #0f766e; color: #fff; }
      `}</style>
      <div className="ldc-card">
        <div className="ldc-wrap" data-state={state} role="progressbar"
             aria-valuenow={Math.round(p)} aria-valuemin={0} aria-valuemax={100">
          <svg viewBox="0 0 128 128" aria-hidden="true">
            <circle className="ldc-track" cx="64" cy="64" r="54" />
            <circle className="ldc-arc" cx="64" cy="64" r="54"
                    style={{ strokeDashoffset: C * (1 - p / 100) }} />
          </svg>
          <div className="ldc-center">
            <b>{Math.round(p)}%</b>
            <small aria-live="polite">
              {state === 'done' ? 'Ready' : state === 'cancelled' ? 'Cancelled' : `${STAGES[stageIdx].label}…`}
            </small>
          </div>
        </div>
        <div className="ldc-stages" aria-hidden="true">
          {STAGES.map((s, i) => (
            <span key={s.at} className="ldc-stage"
                  data-s={state === 'done' || i < stageIdx ? 'done' : i === stageIdx && state === 'working' ? 'active' : ''}>
              <i />{s.label}
            </span>
          ))}
        </div>
        <div className="ldc-foot">
          {state === 'working' && (
            <button type="button" className="ldc-btn" data-kind="cancel" onClick={cancel}>Cancel</button>
          )}
          {(state === 'idle' || state === 'cancelled') && (
            <button type="button" className="ldc-btn" data-kind="idle" onClick={run}>Start export</button>
          )}
          {state === 'cancelled' && (
            <span style={{ alignSelf: 'center', fontSize: '.75rem', color: '#78716c' }}>
              Stopped at {Math.round(p)}%
            </span>
          )}
          {state === 'done' && (
            <>
              <button type="button" className="ldc-btn" data-kind="go">Download PDF</button>
              <button type="button" className="ldc-btn" data-kind="cancel" onClick={run}>Export again</button>
            </>
          )}
        </div>
      </div>
    </>
  );
}
<!-- ExportRing.vue — percent loader with live stages and a cancel that cancels -->
<script setup lang="ts">
import { computed, onBeforeUnmount, ref } from 'vue';
import '@nabuxai/ui-core/css';

const props = defineProps<{ workMs?: number }>();
const C = 339.3; // 2π × r54
const stages = [
  { at: 0, label: 'Gathering data' },
  { at: 35, label: 'Compressing' },
  { at: 75, label: 'Uploading' },
];

const state = ref<'idle' | 'working' | 'cancelled' | 'done'>('idle');
const p = ref(0);
let raf = 0;

const stageIdx = computed(() => Math.max(0, stages.reduce((acc, s, i) => (p.value >= s.at ? i : acc), 0)));
const stage = (i: number) =>
  state.value === 'done' || i < stageIdx.value ? 'done'
  : i === stageIdx.value && state.value === 'working' ? 'active' : '';

const run = () => {
  if (state.value === 'working') return;
  state.value = 'working';
  p.value = 0;
  const t0 = performance.now();
  const tick = (now: number) => {
    const t = Math.min(1, (now - t0) / (props.workMs ?? 6400));
    const eased = t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
    p.value = eased * 100;
    if (t >= 1) { state.value = 'done'; return; }
    raf = requestAnimationFrame(tick);
  };
  raf = requestAnimationFrame(tick);
};

const cancel = () => {
  if (state.value !== 'working') return;
  cancelAnimationFrame(raf);
  state.value = 'cancelled';
};

// Unmount aborts the frame loop — nothing counts on behind the page.
onBeforeUnmount(() => cancelAnimationFrame(raf));
</script>

<template>
  <div class="ldc-card">
    <div class="ldc-wrap" :data-state="state" role="progressbar"
         :aria-valuenow="Math.round(p)" aria-valuemin="0" aria-valuemax="100">
      <svg viewBox="0 0 128 128" aria-hidden="true">
        <circle class="ldc-track" cx="64" cy="64" r="54" />
        <circle class="ldc-arc" cx="64" cy="64" r="54" :style="{ strokeDashoffset: C * (1 - p / 100) }" />
      </svg>
      <div class="ldc-center">
        <b>{{ Math.round(p) }}%</b>
        <small aria-live="polite">
          {{ state === 'done' ? 'Ready' : state === 'cancelled' ? 'Cancelled' : `${stages[stageIdx].label}…` }}
        </small>
      </div>
    </div>
    <div class="ldc-stages" aria-hidden="true">
      <span v-for="(s, i) in stages" :key="s.at" class="ldc-stage" :data-s="stage(i)"><i />{{ s.label }}</span>
    </div>
    <div class="ldc-foot">
      <button v-if="state === 'working'" type="button" class="ldc-btn" data-kind="cancel" @click="cancel">Cancel</button>
      <button v-if="state === 'idle' || state === 'cancelled'" type="button" class="ldc-btn" data-kind="idle" @click="run">
        Start export
      </button>
      <span v-if="state === 'cancelled'" class="ldc-stopped">Stopped at {{ Math.round(p) }}%</span>
      <template v-if="state === 'done'">
        <button type="button" class="ldc-btn" data-kind="go">Download PDF</button>
        <button type="button" class="ldc-btn" data-kind="cancel" @click="run">Export again</button>
      </template>
    </div>
  </div>
</template>

<style scoped>
.ldc-card { display: grid; gap: 1.2rem; justify-items: center; padding: 1.4rem;
            border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
            max-inline-size: 21rem; }
.ldc-wrap { position: relative; display: grid; place-items: center; inline-size: 9rem; aspect-ratio: 1;
            font: 600 1.5rem/1 inherit; }
.ldc-wrap svg { position: absolute; inset: 0; }
.ldc-center { position: relative; display: grid; gap: .3rem; justify-items: center; }
.ldc-center small { font-size: .62rem; color: #78716c; }
.ldc-track, .ldc-arc { fill: none; }
.ldc-track { stroke: #e7e5e4; stroke-width: 7; }
.ldc-arc { stroke: #0f766e; stroke-width: 7; stroke-linecap: round; stroke-dasharray: 339.3;
           transform: rotate(-90deg); transform-origin: 50% 50%; }
.ldc-wrap[data-state='cancelled'] .ldc-arc { stroke: #a8a29e; }
.ldc-stages { display: grid; gap: .4rem; justify-items: start; font-size: .8rem; }
.ldc-stage { display: flex; gap: .5rem; align-items: center; color: #78716c; }
.ldc-stage[data-s='done'] { color: #0f766e; }
.ldc-stage[data-s='active'] { color: #1c1917; font-weight: 600; }
.ldc-stage i { inline-size: .55rem; aspect-ratio: 1; border-radius: 50%; background: currentColor; }
.ldc-foot { display: flex; gap: .7rem; }
.ldc-stopped { align-self: center; font-size: .75rem; color: #78716c; }
.ldc-btn { block-size: 2.4rem; padding-inline: 1.15rem; border: none; border-radius: 999px;
           font: 600 .84rem/1 inherit; cursor: pointer; }
.ldc-btn[data-kind='cancel'] { border: 1px solid #e7e5e4; background: none; color: #1c1917; }
.ldc-btn[data-kind='go'], .ldc-btn[data-kind='idle'] { background: #0f766e; color: #fff; }
</style>
<!-- ExportRing.svelte — percent loader with live stages and a cancel that cancels -->
<script lang="ts">
  import { onDestroy } from 'svelte';
  import '@nabuxai/ui-core/css';

  let { workMs = 6400 } = $props();
  const C = 339.3; // 2π × r54
  const stages = [
    { at: 0, label: 'Gathering data' },
    { at: 35, label: 'Compressing' },
    { at: 75, label: 'Uploading' },
  ];

  let state = $state<'idle' | 'working' | 'cancelled' | 'done'>('idle');
  let p = $state(0);
  let raf = 0;

  const stageIdx = $derived(Math.max(0, stages.reduce((acc, s, i) => (p >= s.at ? i : acc), 0)));
  const stage = (i: number) =>
    state === 'done' || i < stageIdx ? 'done'
    : i === stageIdx && state === 'working' ? 'active' : '';

  const run = () => {
    if (state === 'working') return;
    state = 'working';
    p = 0;
    const t0 = performance.now();
    const tick = (now: number) => {
      const t = Math.min(1, (now - t0) / workMs);
      const eased = t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
      p = eased * 100;
      if (t >= 1) { state = 'done'; return; }
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
  };

  const cancel = () => {
    if (state !== 'working') return;
    cancelAnimationFrame(raf);
    state = 'cancelled';
  };

  // Unmount aborts the frame loop — nothing counts on behind the page.
  onDestroy(() => cancelAnimationFrame(raf));
</script>

<div class="ldc-card">
  <div class="ldc-wrap" data-state={state} role="progressbar"
       aria-valuenow={Math.round(p)} aria-valuemin="0" aria-valuemax="100">
    <svg viewBox="0 0 128 128" aria-hidden="true">
      <circle class="ldc-track" cx="64" cy="64" r="54" />
      <circle class="ldc-arc" cx="64" cy="64" r="54" style={`stroke-dashoffset: ${C * (1 - p / 100)}`} />
    </svg>
    <div class="ldc-center">
      <b>{Math.round(p)}%</b>
      <small aria-live="polite">
        {state === 'done' ? 'Ready' : state === 'cancelled' ? 'Cancelled' : `${stages[stageIdx].label}…`}
      </small>
    </div>
  </div>
  <div class="ldc-stages" aria-hidden="true">
    {#each stages as s, i (s.at)}
      <span class="ldc-stage" data-s={stage(i)}><i />{s.label}</span>
    {/each}
  </div>
  <div class="ldc-foot">
    {#if state === 'working'}
      <button type="button" class="ldc-btn" data-kind="cancel" onclick={cancel}>Cancel</button>
    {/if}
    {#if state === 'idle' || state === 'cancelled'}
      <button type="button" class="ldc-btn" data-kind="idle" onclick={run}>Start export</button>
    {/if}
    {#if state === 'cancelled'}
      <span class="ldc-stopped">Stopped at {Math.round(p)}%</span>
    {/if}
    {#if state === 'done'}
      <button type="button" class="ldc-btn" data-kind="go">Download PDF</button>
      <button type="button" class="ldc-btn" data-kind="cancel" onclick={run}>Export again</button>
    {/if}
  </div>
</div>

<style>
  .ldc-card { display: grid; gap: 1.2rem; justify-items: center; padding: 1.4rem;
              border: 1px solid #e7e5e4; border-radius: 1rem; font-family: 'Inter', sans-serif;
              max-inline-size: 21rem; }
  .ldc-wrap { position: relative; display: grid; place-items: center; inline-size: 9rem; aspect-ratio: 1;
              font: 600 1.5rem/1 inherit; }
  .ldc-wrap svg { position: absolute; inset: 0; }
  .ldc-center { position: relative; display: grid; gap: .3rem; justify-items: center; }
  .ldc-center small { font-size: .62rem; color: #78716c; }
  .ldc-track, .ldc-arc { fill: none; }
  .ldc-track { stroke: #e7e5e4; stroke-width: 7; }
  .ldc-arc { stroke: #0f766e; stroke-width: 7; stroke-linecap: round; stroke-dasharray: 339.3;
             transform: rotate(-90deg); transform-origin: 50% 50%; }
  .ldc-wrap[data-state='cancelled'] .ldc-arc { stroke: #a8a29e; }
  .ldc-stages { display: grid; gap: .4rem; justify-items: start; font-size: .8rem; }
  .ldc-stage { display: flex; gap: .5rem; align-items: center; color: #78716c; }
  .ldc-stage[data-s='done'] { color: #0f766e; }
  .ldc-stage[data-s='active'] { color: #1c1917; font-weight: 600; }
  .ldc-stage i { inline-size: .55rem; aspect-ratio: 1; border-radius: 50%; background: currentColor; }
  .ldc-foot { display: flex; gap: .7rem; flex-wrap: wrap; justify-content: center; }
  .ldc-stopped { align-self: center; font-size: .75rem; color: #78716c; }
  .ldc-btn { block-size: 2.4rem; padding-inline: 1.15rem; border: none; border-radius: 999px;
             font: 600 .84rem/1 inherit; cursor: pointer; }
  .ldc-btn[data-kind='cancel'] { border: 1px solid #e7e5e4; background: none; color: #1c1917; }
  .ldc-btn[data-kind='go'], .ldc-btn[data-kind='idle'] { background: #0f766e; color: #fff; }
</style>