Loading Bar
The LoadingBar is a thin bar fixed to the top of the page. It tells people the app is busy, for example while a page navigates or while selected API calls are in flight.
The progress is simulated. The bar jumps in quickly, slows down as it nears 95%, and only reaches 100% when the work really finishes. Because of that it needs no progress data from the backend and works for any request.
Use it for page-level or background activity. For a measurable operation such as an upload or a pipeline run, use Progress. For content placeholders, use Skeleton.
Press Start in the example, wait a moment, then press Done. The demo uses an inline bar so it stays inside the example; in an app the bar is fixed to the top of the viewport.
How it behaves
| Behavior | Default | What it does |
|---|---|---|
| Show delay | 120 ms | Work that finishes faster never shows the bar, so quick requests do not flash it. |
| Minimum visible time | 300 ms | Once shown, the bar stays at least this long so it does not blink. |
| Initial progress | 8% | Where the bar starts when it appears. |
| Trickle | 250 ms | Every step moves a small fraction of the remaining distance, so it slows down near the cap. |
| Progress cap | 95% | Trickling never goes past this. Only finishing the work reaches 100%. |
| Completion | 200 ms | The fill runs to 100%, then the bar fades out (200 ms) and rewinds while invisible. |
| Safety net | 30 s | If a task never calls done, the bar force-completes. Set maxDuration: 0 to disable. |
All of these can be changed. See Customizing the timings.
Installation
import { LoadingBar, loadingBar } from '@harnessio/ui/components'
//...
// 1. Mount once, near the root of your appreturn ( <> <Outlet /> <LoadingBar /> </>)
// 2. Start and stop it from anywhereconst key = loadingBar.start()await doWork()loadingBar.done(key)Core idea
There is one shared bar for the whole app. You mount <LoadingBar /> once, and any code can register work with it:
loadingBar.start(key?)registers a unit of work and returns its key.loadingBar.done(key?)marks it finished.
The bar completes only when every started task is done. A page navigation plus three API calls therefore appear as one continuous bar instead of four flickering ones.
Keys make the calls safe. Starting a key that is already active does nothing, and done with an unknown key is ignored, so a duplicate or forgotten call cannot corrupt the count.
Pick the scenario that matches what you need:
| I want the bar to show for… | Use |
|---|---|
| Page navigation and route loaders | Page navigation |
| One specific query or mutation | A specific API |
| A chosen set of queries, app-wide | Selected APIs by opt-in |
A plain fetch or any async function | Plain fetch and async functions |
Lazy-loaded routes and Suspense | Lazy routes and Suspense |
| Several things at once | Concurrent work |
| A bar inside a card or panel, not the whole page | A bar inside a panel |
| Full manual control | Manual start and stop |
Mount the bar once
Place LoadingBar next to your root layout, for example beside the Toaster. It is fixed to the top of the viewport, sits above the app shell, never takes focus and ignores pointer events.
import { LoadingBar } from '@harnessio/ui/components'
function AppShell() { return ( <> <Outlet /> <Toaster /> <LoadingBar /> </> )}Mount it only once. Two bars on the same page would draw on top of each other.
Page navigation
@harnessio/ui does not depend on a router, so connect navigation in your app. With a React Router data router, useNavigation reports loading or submitting while a route loader or action runs, and useLoadingBarTask turns that into start and done.
import { useNavigation } from 'react-router-dom'import { LoadingBar, useLoadingBarTask } from '@harnessio/ui/components'
export function AppLoadingBar() { const navigation = useNavigation()
// Active for as long as a navigation is in progress useLoadingBarTask(navigation.state !== 'idle', 'navigation')
return <LoadingBar />}Render AppLoadingBar inside the router, because useNavigation needs the router context. Navigations that finish in under 120 ms never show the bar.
useNavigation only reports state for data routers (createBrowserRouter with RouterProvider). With the legacy BrowserRouter there is no navigation state, so start the bar yourself from your own loading signal.
A specific API call
When you already have a loading flag for one request, hand it to useLoadingBarTask. The bar starts when the flag becomes true and finishes when it becomes false or the component unmounts.
import { useLoadingBarTask } from '@harnessio/ui/components'
function RepositoryPage() { const { data, isLoading } = useGetRepositoryQuery({ repoRef })
// Initial load only. Use isFetching to include background refetches. useLoadingBarTask(isLoading, 'repository')
return <RepositoryView data={data} />}Mutations work the same way:
const { mutate, isLoading } = useDeleteBranchMutation();
useLoadingBarTask(isLoading, "delete-branch");Choose the flag on purpose. isLoading is true only for the first load, whereas isFetching is also true for refetches and polling. Polling queries should not drive the bar.
Selected APIs by opt-in
Not every request deserves a bar. Polling and background refetches would keep it on screen all the time. A good pattern is to opt in per query or mutation using React Query’s meta, and connect the caches to the bar once.
1. Connect the caches once, where you create the query client. This is written for React Query v4. In v5 the mutation status is 'pending' instead of 'loading'.
import { MutationCache, QueryCache, QueryClient } from "@tanstack/react-query";import { loadingBar } from "@harnessio/ui/components";
const queryCache = new QueryCache();const mutationCache = new MutationCache();
queryCache.subscribe((event) => { const { query } = event; if (query.meta?.loadingBar !== true) return;
const key = `query:${query.queryHash}`; if (event.type !== "removed" && query.state.fetchStatus === "fetching") { loadingBar.start(key); } else { loadingBar.done(key); }});
mutationCache.subscribe((event) => { const { mutation } = event; if (mutation.options.meta?.loadingBar !== true) return;
const key = `mutation:${mutation.mutationId}`; if (mutation.state.status === "loading") { loadingBar.start(key); } else { loadingBar.done(key); }});
export const queryClient = new QueryClient({ queryCache, mutationCache });2. Opt in on the calls that should show the bar:
// Only this query drives the baruseGetRepositoryQuery({ repoRef }, { meta: { loadingBar: true } });
// A mutationuseMergePullRequestMutation({ meta: { loadingBar: true } });
// A polling query: no meta, so no barusePipelineStatusQuery({ id }, { refetchInterval: 5000 });Plain fetch and async functions
For code outside React Query, withLoadingBar wraps a promise, or a function that returns one. The bar finishes whether the promise resolves or rejects, and the original result (or error) is passed through.
import { withLoadingBar } from "@harnessio/ui/components";
// Pass a promiseconst blob = await withLoadingBar( fetch(url).then((res) => res.blob()), "download",);
// Or a function. The bar starts first, then the function runs.const user = await withLoadingBar(() => api.getUser(id), "get-user");
// Errors still reach your catch block, and the bar still completestry { await withLoadingBar(() => api.saveSettings(values), "save-settings");} catch (error) { toast({ title: "Could not save", variant: "destructive" });}The second argument is the key. It is optional, but a descriptive key makes the work easy to find when debugging.
Lazy routes and Suspense
A Suspense fallback mounts while a lazy chunk loads and unmounts when it is ready, which is exactly the lifetime of the work. Render a tiny component that holds the bar for as long as it is mounted.
import { Suspense } from 'react'import { useLoadingBarTask } from '@harnessio/ui/components'
function LoadingBarFallback() { useLoadingBarTask(true, 'suspense') return null}
<Suspense fallback={<LoadingBarFallback />}> <LazyPage /></Suspense>If you want a skeleton too, render it from the same fallback: useLoadingBarTask(true, 'suspense') then return <PageSkeleton />.
Concurrent work
Start as many tasks as you need. The bar keeps going until the longest one finishes, and the final jump to 100% happens once, at the end. Give every task its own key.
New work that arrives while the bar is already showing keeps the bar going instead of starting a second one. If it arrives while the bar is completing, the bar restarts from the beginning.
A bar inside a component
The page-level bar is fixed to the top of the viewport. To show activity at the top of a card, accordion item, tab panel, dialog or any other region, give that region its own bar:
- Create a scoped controller with
useLoadingBarController(). It is stable across renders and reset on unmount. - Make the region
relative(and usuallyoverflow-hidden), then render<LoadingBar inline controller={bar} />as its first child. The inline bar is absolutely positioned at the top edge of that region. - Drive it with
useLoadingBarTask(isLoading, key, bar)orwithLoadingBar(promise, key, bar), passing the same controller.
Scoped controllers share no state: a region’s bar never reacts to page-level work, and the page-level bar never reacts to a region.
import { LoadingBar, useLoadingBarController, useLoadingBarTask } from '@harnessio/ui/components'
function ActivityCard() { const bar = useLoadingBarController() const { isLoading } = useActivityQuery()
// The bar runs while the query is loading useLoadingBarTask(isLoading, 'activity', bar)
return ( <Card.Root className="relative overflow-hidden"> <LoadingBar inline controller={bar} /> <Card.Content>...</Card.Content> </Card.Root> )}Accordion content
Put the bar inside Accordion.Content so it only exists while the item is open. Mount the loading component inside the content and the work starts when the item opens.
Tab content
Use one controller per tab panel so each tab shows only its own activity, even when several tabs load at the same time.
Reusable wrapper
If many regions need a bar, wrap the pattern once:
import { LoadingBar, useLoadingBarController, useLoadingBarTask } from '@harnessio/ui/components'
function LoadingRegion({ loading, className, children }: { loading: boolean; className?: string; children: ReactNode }) { const bar = useLoadingBarController() useLoadingBarTask(loading, 'region', bar)
return ( <div className={cn('relative overflow-hidden', className)}> <LoadingBar inline controller={bar} /> {children} </div> )}
// <LoadingRegion loading={isFetching}>...</LoadingRegion>Tips
- The parent must be
position: relative, or the bar attaches to the nearest positioned ancestor (or the page). - Add
overflow-hiddenso the glow at the leading edge does not spill out of rounded corners. - The bar is 3px high and sits on top of the region’s first row. Add a little top padding if it overlaps content.
- Use a stable key per region. Reusing a key is idempotent, so a re-render never double-counts.
Manual start and stop
When none of the helpers fit, use the imperative API. Always release the key in finally, so an error cannot leave the bar running.
import { loadingBar } from "@harnessio/ui/components";
const key = loadingBar.start("export"); // or loadingBar.start() for an auto keytry { await exportReport();} finally { loadingBar.done(key);}loadingBar.done()with no key finishes the most recently started task.loadingBar.reset()forgets all active work and completes the bar immediately. Use it when you leave a context where in-flight requests no longer matter, such as a logout.
Controlled mode
Pass progress (0 to 1), and optionally visible, to render an exact value instead of following a controller. It is useful for visual tests and for driving the bar from progress you do know. Out-of-range values are clamped.
When only progress is set, the bar is visible whenever progress > 0. When progress or visible is set, the controller is ignored.
Customizing the timings
Create a controller with your own options and pass it to both the bar and the helpers.
import { LoadingBar, createLoadingBarController, useLoadingBarTask } from '@harnessio/ui/components'
// Module level, so it is created onceconst slowPageBar = createLoadingBarController({ showDelay: 300, // only show for work longer than 300 ms minVisibleTime: 500, initialProgress: 0.15, maxProgress: 0.9, trickleInterval: 400, maxDuration: 60_000, // 0 disables the safety net})
useLoadingBarTask(isLoading, 'report', slowPageBar)<LoadingBar controller={slowPageBar} />Every option is listed in Properties.
Testing code that uses the bar
Create an isolated controller so tests do not share the global one, and use fake timers, because every phase is timer driven.
import { createLoadingBarController } from "@harnessio/ui/components";
vi.useFakeTimers();const bar = createLoadingBarController({ showDelay: 120, minVisibleTime: 0 });
bar.start("a");vi.advanceTimersByTime(120);expect(bar.getSnapshot()).toEqual({ visible: true, progress: 0.08 });
bar.done("a");expect(bar.getSnapshot().progress).toBe(1);Pass random: () => 0.5 for fully deterministic trickle steps. If you only need to assert that your code started and stopped the bar, spy on loadingBar.start and loadingBar.done.
Best practices
Do
- Mount one page-level
LoadingBarand share it throughloadingBar. - Use the bar for waits of roughly a quarter of a second or more. The show delay hides anything shorter.
- Opt in the requests that matter to the user, such as navigation, saves and heavy loads.
- Give tasks descriptive keys, and release them in
finallywhen starting manually.
Don’t
- Don’t drive it from polling or background refetches. It would stay on screen constantly.
- Don’t show it for something you can measure. Use Progress with a real value.
- Don’t rely on the percentage. It is an estimate for orientation, not a measurement.
- Don’t create a new controller on every render. Use
useMemoor a module-level constant.
Troubleshooting
| Symptom | Likely cause |
|---|---|
| The bar never appears | No <LoadingBar /> is mounted, the work finished within the show delay, or the bar and the code use different controllers. |
| The bar never appears on navigation | Not a data router, or AppLoadingBar is rendered outside the router. |
| The bar stays at about 95% | A started task was never finished. Release keys in finally. The safety net completes it after 30 s. |
| The bar is always on screen | A polling or refetching query drives it. Use isLoading instead of isFetching, or remove the loadingBar meta. |
| Two bars draw on top of each other | <LoadingBar /> is mounted twice. Keep one page-level instance. |
| An inline bar is invisible | The parent is not position: relative, or it has no height and overflow: hidden. |
Accessibility
- The bar renders
role="progressbar"witharia-valuemin,aria-valuemaxandaria-valuenow, plusaria-busywhile loading. - It has the accessible name “Loading”. Override it with
aria-labelwhen the context needs a more specific name. - While idle it is
aria-hidden, so assistive technology does not announce an empty progress bar. - It never receives focus and does not intercept pointer events.
- With
prefers-reduced-motion: reduce, transitions and the leading glow are turned off, so the bar changes without animating. - The value is an estimate, not a measurement. The bar does not announce progress changes on its own. If a long operation needs to be announced, also show a status message in a live region such as a toast.
Properties
LoadingBar
Prop | Required | Default | Type |
|---|---|---|---|
| controller | false | loadingBar | LoadingBarController |
| progress | false | number | |
| visible | false | boolean | |
| inline | false | false | boolean |
| className | false | string | |
| aria-label | false | 'Loading' | string |
All other div attributes are forwarded to the root element, and the ref points to it.
loadingBar and controllers
loadingBar is the shared controller. createLoadingBarController(options) returns an isolated one with the same methods.
Prop | Required | Default | Type |
|---|---|---|---|
| start | false | (key?: string) => string | |
| done | false | (key?: string) => void | |
| reset | false | () => void | |
| subscribe | false | (listener: () => void) => () => void | |
| getSnapshot | false | () => { visible: boolean; progress: number } |
LoadingBarOptions
Prop | Required | Default | Type |
|---|---|---|---|
| showDelay | false | 120 | number |
| minVisibleTime | false | 300 | number |
| trickleInterval | false | 250 | number |
| initialProgress | false | 0.08 | number |
| maxProgress | false | 0.95 | number |
| completeDuration | false | 200 | number |
| fadeDuration | false | 200 | number |
| maxDuration | false | 30000 | number |
| random | false | Math.random | () => number |
Hooks and helpers
Prop | Required | Default | Type |
|---|---|---|---|
| useLoadingBarTask | false | (active: boolean, key?: string, controller?: LoadingBarController) => void | |
| withLoadingBar | false | <T>(task: Promise<T> | (() => Promise<T>), key?: string, controller?: LoadingBarController) => Promise<T> | |
| useLoadingBar | false | (controller?: LoadingBarController) => { visible: boolean; progress: number } | |
| useLoadingBarController | false | (options?: LoadingBarOptions) => LoadingBarController | |
| createLoadingBarController | false | (options?: LoadingBarOptions) => LoadingBarController |