Polled job card
One card covering every state a background job passes through, where an unknown total switches the bar to indeterminate with a running count rather than inventing a percentage, and a failed poll is treated as transient instead of ending the job.
Reach for it when building
- a library scan or import
- a long export or report build
- a bulk download queue
- a video transcode
- a data migration triggered from the UI
- any job whose progress is fetched rather than streamed
<div class="pjc-demo">
<section class="pjc-card" aria-labelledby="pjc-name">
<header class="pjc-head">
<h3 class="pjc-name" id="pjc-name">Scan library</h3>
<span class="pjc-pill" id="pjc-pill" data-state="idle">Not started</span>
</header>
<div class="pjc-track" id="pjc-track" role="progressbar" aria-labelledby="pjc-name" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-valuetext="Not started" aria-busy="false" data-state="idle">
<div class="pjc-fill" id="pjc-fill"></div>
</div>
<p class="pjc-line" id="pjc-line">Nothing has run yet.</p>
<p class="pjc-sr-only" id="pjc-status-announce" role="status"></p>
</section>
<div class="pjc-controls">
<button type="button" class="pjc-btn pjc-primary" id="pjc-start">Start scan</button>
<button type="button" class="pjc-btn" id="pjc-cancel" aria-disabled="true">Cancel</button>
<button type="button" class="pjc-btn" id="pjc-fail" aria-disabled="true" aria-describedby="pjc-hint">Simulate a failed poll</button>
</div>
<p class="pjc-hint" id="pjc-hint">One missed poll is shrugged off. Press it three times in a row to fail the job.</p>
</div>Colors come from shared theme tokens — --surface, --ink, --border, --accent and friends — so this CSS carries no palette
of its own. Use Runnable file to copy the tokens along with it.
.pjc-demo { display: flex; flex-direction: column; gap: 0.75rem; max-width: 480px; }
.pjc-card {
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 1rem;
border: 1px solid var(--border);
border-radius: var(--radius);
background: var(--surface);
}
.pjc-head { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; justify-content: space-between; }
.pjc-name { margin: 0; font-size: 1rem; }
/* Each state is a different shape as well as a different colour: the border
style and the word both change, so the state survives a greyscale screenshot. */
.pjc-pill {
font-size: 0.875rem;
font-weight: 600;
padding: 0.15rem 0.55rem;
border-radius: 999px;
border: 1px solid var(--border);
background: var(--surface-2);
color: var(--dim);
}
.pjc-pill[data-state='running'] { border-color: var(--accent); color: var(--accent); background: color-mix(in oklab, var(--accent) 12%, var(--surface)); }
.pjc-pill[data-state='done'] { border-color: var(--ok); color: var(--ok); background: color-mix(in oklab, var(--ok) 12%, var(--surface)); }
.pjc-pill[data-state='failed'] { border-color: var(--bad); color: var(--bad); background: color-mix(in oklab, var(--bad) 12%, var(--surface)); }
.pjc-pill[data-state='cancelled'] { border-style: dashed; border-color: var(--warn); color: var(--warn); }
/* The mark is decoration for the eye; the word already says it, so the
alternative text after the slash is empty. */
.pjc-pill[data-state='done']::before { content: '\2713\00a0'; content: '\2713\00a0' / ''; }
.pjc-pill[data-state='failed']::before { content: '\2715\00a0'; content: '\2715\00a0' / ''; }
.pjc-track {
height: 12px;
border: 1px solid var(--border-strong);
border-radius: 999px;
background: var(--surface-2);
overflow: hidden;
}
/* The script writes the percentage to --pjc-done rather than to width, so the
indeterminate rule below can still win over it. */
.pjc-fill { height: 100%; width: var(--pjc-done, 0%); background: var(--accent); transition: width 200ms linear; }
.pjc-track[data-state='done'] .pjc-fill { background: var(--ok); }
.pjc-track[data-state='failed'] .pjc-fill { background: var(--bad); }
.pjc-track[data-state='cancelled'] .pjc-fill { background: var(--dim); }
/* Indeterminate is a different drawing, not a guessed width. */
.pjc-fill[data-mode='indeterminate'] {
width: 100%;
background-image: repeating-linear-gradient(
45deg,
color-mix(in oklab, var(--accent-ink) 45%, transparent) 0 8px,
transparent 8px 16px
);
animation: pjc-drift 900ms linear infinite;
}
@keyframes pjc-drift {
from { background-position: 0 0; }
to { background-position: 22px 0; }
}
/* The stripes are the indeterminate state; the drift is only garnish. */
@media (prefers-reduced-motion: reduce) {
.pjc-fill { transition: none; }
.pjc-fill[data-mode='indeterminate'] { animation: none; }
}
/* Room for the longest message, so the buttons below never move under a
pointer when the line wraps. */
.pjc-line { margin: 0; color: var(--dim); min-height: 3.2em; }
@media (max-width: 30rem) { .pjc-line { min-height: 4.8em; } }
/* The visible line updates on every poll; a screen reader only needs the
phase changes and the failure messages, so the actual role="status" lives
here instead, off-screen. */
.pjc-sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.pjc-controls { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.pjc-btn {
font: inherit;
min-height: 44px;
padding: 0.5rem 0.9rem;
border-radius: calc(var(--radius) - 4px);
border: 1px solid var(--border-strong);
background: var(--surface-2);
color: var(--ink);
cursor: pointer;
}
.pjc-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.pjc-btn[aria-disabled='true'] { opacity: 0.55; cursor: not-allowed; }
.pjc-hint { margin: 0; font-size: 0.875rem; color: var(--dim); }/* Uneven but fixed steps: a scan discovers files in bursts, and a demo that
redraws differently on every run is impossible to reason about. */
const DISCOVERY_STEPS = [148, 212, 176, 233, 191, 260];
const POLL_INTERVAL_MS = 420;
const MISSES_BEFORE_FAILURE = 3;
const pill = document.getElementById('pjc-pill');
const fill = document.getElementById('pjc-fill');
const line = document.getElementById('pjc-line');
const track = document.getElementById('pjc-track');
const startButton = document.getElementById('pjc-start');
const cancelButton = document.getElementById('pjc-cancel');
const failButton = document.getElementById('pjc-fail');
const statusAnnounce = document.getElementById('pjc-status-announce');
let timer = null;
let found = 0;
let done = 0;
let total = 0;
let missedPolls = 0;
let queuedFailures = 0;
let discoveryStep = 0;
let lastAnnouncedWord = null;
/* aria-disabled rather than the disabled attribute: a button that disables
itself on press would drop keyboard focus to the top of the page. */
function setEnabled(button, enabled) {
button.setAttribute('aria-disabled', enabled ? 'false' : 'true');
}
function isEnabled(button) {
return button.getAttribute('aria-disabled') !== 'true';
}
/* The visible line updates on every poll; the hidden status region only
speaks when the word on the pill changes, or a failure needs to interrupt —
one announcement per meaningful change, not one per poll. */
function announce(message) {
statusAnnounce.textContent = message;
}
function setState(word, state, message) {
const running = state === 'running';
pill.textContent = word;
pill.dataset.state = state;
track.dataset.state = state;
track.setAttribute('aria-busy', running ? 'true' : 'false');
line.textContent = message;
setEnabled(startButton, !running);
setEnabled(cancelButton, running);
setEnabled(failButton, running);
if (!running) startButton.textContent = state === 'cancelled' ? 'Resume scan' : 'Start scan';
if (word !== lastAnnouncedWord) {
lastAnnouncedWord = word;
announce(word + '. ' + message);
}
}
/* The progressbar has no aria-valuenow while the total is unknown: that
absence is what "indeterminate" means to assistive technology. */
function syncProgress() {
if (total === 0) {
track.removeAttribute('aria-valuenow');
track.setAttribute('aria-valuetext', found.toLocaleString() + ' files found, total not known yet');
return;
}
track.setAttribute('aria-valuenow', String(Math.round((done / total) * 100)));
track.setAttribute(
'aria-valuetext',
done.toLocaleString() + ' of ' + total.toLocaleString() + ' files indexed'
);
}
function stop() {
if (timer !== null) {
window.clearInterval(timer);
timer = null;
}
}
function poll() {
/* A failure queued by the button is consumed here, on the next real poll,
instead of firing instantly — so it can never race the timer's own tick. */
if (queuedFailures > 0) {
queuedFailures -= 1;
missedPolls += 1;
if (missedPolls < MISSES_BEFORE_FAILURE) {
line.textContent =
'Poll ' + missedPolls + ' of ' + MISSES_BEFORE_FAILURE + ' failed. Still polling — the job is very likely fine.';
announce(line.textContent);
return;
}
stop();
delete fill.dataset.mode;
setState(
'Failed',
'failed',
'Three polls failed in a row. The library folder may have moved — check the path and start again.'
);
return;
}
/* Any poll that actually succeeds resets the streak — only misses landing
back to back are allowed to add up to a failure. */
if (missedPolls > 0) announce('Polling recovered.');
missedPolls = 0;
/* A total of zero means "not known yet", not "nothing to do". The bar goes
indeterminate and the count carries the only honest number there is. */
if (total === 0) {
found += DISCOVERY_STEPS[discoveryStep % DISCOVERY_STEPS.length];
discoveryStep += 1;
fill.dataset.mode = 'indeterminate';
setState(
'Scanning',
'running',
found.toLocaleString() + ' files found so far. No percentage until the total is known.'
);
syncProgress();
if (found > 900) {
total = found;
delete fill.dataset.mode;
}
return;
}
done = Math.min(total, done + Math.ceil(total / 14));
fill.style.setProperty('--pjc-done', (done / total) * 100 + '%');
syncProgress();
if (done >= total) {
stop();
setState('Done', 'done', total.toLocaleString() + ' files indexed. Nothing was left waiting.');
return;
}
setState('Indexing', 'running', done.toLocaleString() + ' of ' + total.toLocaleString() + ' indexed.');
}
startButton.addEventListener('click', () => {
if (!isEnabled(startButton)) return;
stop();
/* Cancelling rolled nothing back, so the next run picks up the counts it
stopped at. Done and Failed start from nothing. */
const resuming = pill.dataset.state === 'cancelled';
if (!resuming) {
found = 0;
done = 0;
total = 0;
discoveryStep = 0;
fill.style.setProperty('--pjc-done', '0%');
}
missedPolls = 0;
queuedFailures = 0;
if (total === 0) fill.dataset.mode = 'indeterminate';
if (resuming) {
setState('Resuming', 'running', 'Picking up from ' + (done || found).toLocaleString() + ' files.');
} else {
setState('Starting', 'running', 'Counting files. The total is not known yet.');
}
syncProgress();
timer = window.setInterval(poll, POLL_INTERVAL_MS);
});
cancelButton.addEventListener('click', () => {
if (!isEnabled(cancelButton)) return;
stop();
delete fill.dataset.mode;
setState(
'Cancelled',
'cancelled',
'Stopped at ' + (done || found).toLocaleString() + ' files. Nothing was rolled back — Resume picks up from here.'
);
});
/* Only queues while a job is running, so a failure can never be attributed
to an idle or already-finished job. */
failButton.addEventListener('click', () => {
if (!isEnabled(failButton)) return;
queuedFailures += 1;
});Paste this into an agent to rebuild the pattern from scratch.
Build one card that covers a whole background job: not started, starting, working with an unknown total, working with a known total, cancelled, failed, and done. Progress is fetched on a timer rather than streamed, so the card has to stay honest between polls.
Reach for this for any job the server owns and the page merely observes — a scan, an import, an export, a transcode. Walk away when the work finishes inside a second: a card with six states for a 200ms request is furniture, and a button that briefly reads "Saving…" is the whole pattern you need.
The rule that makes it work: **a total of zero means the total is not known yet, and that switches the bar to indeterminate with a running count.** Not a 0% bar, and never a percentage computed against a guess. Show "1,412 files found so far" and swap to the determinate bar the moment a real total arrives. A fake percentage that jumps backwards when the total is discovered destroys trust in every number on the card.
**Treat a failed poll as transient, exactly like a thrown network error.** Count consecutive misses and keep polling; only declare failure after several in a row. Ending the loop on the first non-OK response is the bug that freezes a card mid-job forever — one dropped request, one server restart, and the card sits at 43% claiming to be working while nothing is watching.
Make each state a different shape, not the same pill in a different colour. Change the word, the pill's mark or border style (a tick for done, a cross for failed, a dashed border for cancelled), and the fill colour together, so the state reads on a greyscale screenshot and in a peripheral glance.
Say what cancellation actually did, and make it true. "Stopped at 812 files, nothing rolled back, Resume picks up from here" is information; "Cancelled" alone leaves someone wondering whether the half-finished work is now garbage. If the message promises a resume, the start button becomes "Resume scan" and continues from the stopped count; only Done and Failed start from zero.
Offer only the actions the current state allows: Cancel while a job is running, Start or Resume while none is. Mark the others with `aria-disabled` and ignore their clicks rather than using the `disabled` attribute, because a button that disables itself on press drops keyboard focus.
The visible line may update on every poll, so keep it out of the live region. Put a separate, visually hidden `role="status"` element beside it and write to it once per meaningful change — the state word with its message, a missed poll, a recovery — not once per poll, which turns a screen reader into a metronome. Give the track `role="progressbar"` named by the job title, set `aria-busy` on it while the job runs, and remove `aria-valuenow` while the total is unknown so the bar is indeterminate to assistive technology too; `aria-valuetext` carries the count.
Reserve height for the longest message so the buttons never move under the pointer when the line wraps. Messages are body-size text; only the pill and a caption are smaller.
The indeterminate animation must respect reduced motion: the stripes stop drifting, but the state must still read as indeterminate, so the mode has to be carried by an attribute and not by the animation alone. Every colour comes from theme custom properties.