| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Progress Bar" |
| 4 | description: Show progress bar during background job |
| 5 | icon: "icon-[vaadin--progressbar]" |
| 6 | --- |
| 7 | |
| 8 | <script> |
| 9 | var _job = { complete: false, percentComplete: 0 }; |
| 10 | |
| 11 | function _progressBar() { |
| 12 | return `<div class="flex flex-col items-center gap-2 w-full"> |
| 13 | <div class="w-full h-1.5 overflow-hidden rounded-full bg-neutral-200 dark:bg-neutral-800" role="progressbar" |
| 14 | aria-valuemin="0" aria-valuemax="100" aria-valuenow="${_job.percentComplete}"> |
| 15 | <div id="pb" class="w-full h-full rounded-full bg-neutral-800 dark:bg-neutral-300 origin-left transition-transform duration-[400ms] ease-in-out" style="transform:scaleX(${_job.percentComplete / 100})"></div> |
| 16 | </div> |
| 17 | <span class="text-xs text-neutral-600 dark:text-neutral-400 tabular-nums">${_job.percentComplete}%</span> |
| 18 | </div>`; |
| 19 | } |
| 20 | |
| 21 | function _jobView() { |
| 22 | return `<div id="job-status" class="flex flex-col items-center justify-center gap-4 w-full" |
| 23 | hx-trigger="every 400ms" hx-get="/job/progress" hx-swap="outerMorph"> |
| 24 | <div class="flex items-center justify-between w-full"> |
| 25 | <p class="text-sm font-medium text-neutral-700 dark:text-neutral-200" role="status">${_job.complete ? 'Complete' : 'Processing\u2026'}</p> |
| 26 | <button class="flex items-center gap-1.5 text-[0.8125rem] text-neutral-600 dark:text-neutral-400 hover:text-neutral-800 dark:hover:text-neutral-200 cursor-pointer transition duration-300 ${_job.complete ? 'opacity-100' : 'opacity-0 pointer-events-none'}" |
| 27 | hx-post="/start" hx-target="#job-status" hx-swap="outerMorph" ${_job.complete ? '' : 'tabindex="-1"'}><i class="icon-[mdi--refresh] size-3.5"></i> Run again</button> |
| 28 | </div> |
| 29 | <div class="w-full">${_progressBar()}</div> |
| 30 | </div>`; |
| 31 | } |
| 32 | |
| 33 | server.get("/demo", () => |
| 34 | `<div id="progress-demo" class="starting:opacity-0 transition duration-300 ease-out flex flex-col items-center justify-center text-center"> |
| 35 | <p class="text-sm text-neutral-500 dark:text-neutral-400 mb-4">Simulates a background job with server polling.</p> |
| 36 | <button class="text-sm font-medium text-neutral-600 dark:text-neutral-300 border border-neutral-200 dark:border-neutral-700 rounded-md px-5 py-2 cursor-pointer transition hover:bg-neutral-50 dark:hover:bg-neutral-850 hover:text-neutral-800 dark:hover:text-neutral-100 active:scale-[0.98]" |
| 37 | hx-post="/start" hx-target="#progress-demo" hx-swap="outerMorph"> |
| 38 | Start Job |
| 39 | </button> |
| 40 | </div>`); |
| 41 | |
| 42 | server.post("/start", () => { _job.complete = false; _job.percentComplete = 0; return _jobView(); }); |
| 43 | |
| 44 | server.get("/job/progress", () => { |
| 45 | _job.percentComplete = Math.min(100, _job.percentComplete + 8 + Math.floor(8 * Math.random())); |
| 46 | _job.complete = _job.percentComplete >= 100; |
| 47 | return { body: _jobView(), status: _job.complete ? 286 : 200 }; |
| 48 | }); |
| 49 | |
| 50 | server.start("/demo"); |
| 51 | </script> |
| 52 | |
| 53 | <div id="demo-content" class="not-prose demo-container flex flex-col justify-center min-h-[191px]"></div> |
| 54 | |
| 55 | ## Basic usage |
| 56 | |
| 57 | On the client, a button starts the job. |
| 58 | |
| 59 | ```html |
| 60 | <button hx-post="/start" hx-swap="outerMorph"> |
| 61 | Start Job |
| 62 | </button> |
| 63 | ``` |
| 64 | |
| 65 | On the server, respond with a container that polls for progress: |
| 66 | |
| 67 | ```html |
| 68 | <div hx-trigger="every 400ms" |
| 69 | hx-get="/job/progress" |
| 70 | hx-swap="outerMorph"> |
| 71 | ...progress bar... |
| 72 | </div> |
| 73 | ``` |
| 74 | |
| 75 | - [`hx-trigger`](/reference/attributes/hx-trigger)=[`"every 400ms"`](/reference/attributes/hx-trigger#polling) polls the server on an interval. |
| 76 | - [`outerMorph`](/reference/attributes/hx-swap#outermorph) morphs the element in place, so CSS transitions on `transform` animate smoothly. |
| 77 | |
| 78 | Each poll returns updated progress. When done, the server responds with [HTTP 286](https://en.wikipedia.org/wiki/86_(term)) to stop polling. |
| 79 | |
| 80 | ## Notes |
| 81 | |
| 82 | ### Use `transform` instead of `width` |
| 83 | |
| 84 | Animating `width` causes [layout recalculation](https://web.dev/articles/avoid-large-complex-layouts-and-layout-thrashing). Use `transform: scaleX()` instead ([GPU-composited](https://web.dev/articles/stick-to-compositor-only-properties-and-manage-layer-count), no layout thrashing): |
| 85 | |
| 86 | ```css |
| 87 | .bar { |
| 88 | width: 100%; |
| 89 | transform: scaleX(0); |
| 90 | transform-origin: left; |
| 91 | transition: transform 400ms ease-in-out; |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | The server returns `style="transform: scaleX(0.65)"` instead of `style="width: 65%"`. Same look, no layout thrashing. The demo above uses this approach. |