The loading bar for React

All loading at once? Hold my bar.

No start. No done. Wrap what’s loading, and the bar takes care of the rest.

get started

npm install react-holdbar

under 2 kB gzipped, stylesheet included

The idea

One bar, even when several things are happening.

Saving, fetching, changing page: every part of your app can hold the bar, and none of them has to know about the others. It finishes only when everything is done, and since it’s drawn in CSS, it’s on screen before your JavaScript kicks in.

Even a quick load gets a smooth finish. minVisible lets you keep the bar visible a little longer.

Under the hood

Two components. The page stays in sync.

Render Holdbar once for the page-wide bar. Then place Holdbar.Hold beside any work that can take time. React counts what is still pending; CSS gives the shared signal its look and motion.

Holdbar

Holdbar(props: HoldbarProps): JSX.Element

This is the bar your whole page shares. Render Holdbar once near the root; it does not wrap your app or add a layout element. Its props are optional.

The stylesheet draws the visible line with ::before and currentColor, so it fits your design through the color and styles you already use. Every Holdbar on the page reflects the same state, which is why one is enough.

Holdbar also works from Server Components. The server starts in idle; browser-specific behavior stays in the client components inside it.

props

  • heightnumberdefault 2

    Set the bar height in pixels. This writes to --holdbar-height; without it, the stylesheet uses 2px or your own CSS value.

  • fillDurationnumberdefault 12000

    Set how long the fill takes to reach 90% width. This is an animation, not a progress measurement: it moves quickly at first, slows down, then waits at 90% for the final hold.

  • minVisiblenumberdefault 0

    Give the bar a minimum on-screen time in milliseconds. If the work finishes sooner, it waits before finishing. At 0, a quick task still gets the full finish animation, so the bar does not flash.

  • detachFromViewTransitionbooleandefault false

    Keep the bar in place at the top while the page changes in a view transition. It gets its own view-transition-name.

  • ...restComponentProps<"div">

    Other props go to the div, including className, style, id, data attributes and ref. Your style and onAnimationEnd are preserved.

Holdbar.Hold

Holdbar.Hold(): JSX.Element

Holdbar.Hold is a small component that tells the bar a task is in progress. Render it while the work is pending, then remove it when the task finishes. It takes no props, so it fits naturally in a Suspense fallback or around a pending save.

Holdbar counts these components. The first one opens the bar, and the last one to leave lets it finish. A save can end while navigation is still underway without closing the bar too soon. You can place Holdbar.Hold anywhere in the tree, inside or outside Holdbar. It registers in a layout effect, before the browser paints.

Try the button in the hero: it keeps the bar open for three seconds.

The life of a hold

The bar knows when to wait and when to leave

One shared bar moves through three CSS-visible states in data-holdbar-state. Holds start the loading state; their timing determines when the bar can finish.

idleNo `Holdbar.Hold` components are present. This is the server’s starting state.
first Hold component appears
loadingAt least one `Holdbar.Hold` is present. The bar fills towards 90%.
last Hold component is removed
finishingThe last `Holdbar.Hold` is removed. The bar completes, then fades.

After the finish animation, the bar returns to idle, unless another Holdbar.Hold is present, in which case it goes back to loading. A component added during finishing waits for the animation to end; one added earlier cancels the finish. minVisible can postpone the start of finishing.

Make it yours

CSS does the drawing. Your design sets the tone.

Import react-holdbar/holdbar.css once; the components do not import it for you. It draws a fixed bar across the top of the viewport, above the page, while letting pointer events pass through. Its color comes from currentColor, so style it with the color you already use.

Use --holdbar-height and --holdbar-fill-duration to tune the bar in CSS. Set them on [data-holdbar], or pass height and fillDuration to Holdbar; both routes set the same custom properties.

holdbar-fill eases the bar from 8% to 90% width, fast at first and slower as it goes. holdbar-finish completes it in 200ms and fades it by 450ms. Keep that name: the animation ending is what lets the bar leave finishing.

For people who prefer reduced motion, the fill is skipped. The bar appears at full width and fades out as usual.

Set detachFromViewTransition and the bar gets the holdbar view-transition name. It stays at the top while the page changes beneath it.

main.css
@import "react-holdbar/holdbar.css";

[data-holdbar] {
  --holdbar-height: 3px;
  --holdbar-fill-duration: 12s;
  color: var(--color-accent);
}

give it a try

hold the bar

No setup, no timing, no cleanup.