A highly customizable React top-loading bar component with hook-based, ref-based, and state-based control patterns.
npx @tessl/cli install tessl/npm-react-top-loading-bar@3.0.0react-top-loading-bar is a highly customizable React component that provides a YouTube-style top-loading bar for indicating progress in web applications. The library offers maximum flexibility through three distinct implementation patterns: hook-based control with context, ref-based imperative control, and state-based controlled component patterns. Each approach provides complete access to the loading bar's visual styling, animation timing, and programmatic control capabilities.
npm:
npm install react-top-loading-baryarn:
yarn add react-top-loading-barCDN:
https://unpkg.com/react-top-loading-bar// ESM (preferred)
import LoadingBar, {
LoadingBarRef,
LoadingBarContainer,
useLoadingBar,
type IProps
} from "react-top-loading-bar";
// CommonJS
const LoadingBar = require("react-top-loading-bar").default;
const { LoadingBarContainer, useLoadingBar } = require("react-top-loading-bar");The most common usage pattern with hooks:
import React from "react";
import { LoadingBarContainer, useLoadingBar } from "react-top-loading-bar";
const App = () => {
const { start, complete } = useLoadingBar({
color: "blue",
height: 3,
});
return (
<div>
<button onClick={() => start()}>Start Loading</button>
<button onClick={() => complete()}>Complete</button>
</div>
);
};
const Parent = () => (
<LoadingBarContainer>
<App />
</LoadingBarContainer>
);The library is built around three core components:
The loading bar supports both continuous loading (automatically incrementing progress) and static loading (fixed progress values) with comprehensive visual customization options.
interface IProps {
// Progress Control
progress?: number;
onLoaderFinished?: () => void;
// Visual Styling
color?: string;
background?: string;
height?: number;
shadow?: boolean;
className?: string;
containerClassName?: string;
style?: CSSProperties;
containerStyle?: CSSProperties;
shadowStyle?: CSSProperties;
// Animation Timing
loaderSpeed?: number;
transitionTime?: number;
waitingTime?: number;
}interface LoadingBarRef {
// Start Methods
continuousStart(startingValue?: number, refreshRate?: number): void;
staticStart(startingValue?: number): void;
start(type?: "continuous" | "static", startingValue?: number, refreshRate?: number): void;
// Control Methods
complete(): void;
increase(value: number): void;
decrease(value: number): void;
getProgress(): number;
}Use the useLoadingBar hook within a LoadingBarContainer for declarative loading bar control with React context.
Use React refs with the LoadingBarRef interface for imperative control over the loading bar with direct method calls.
Use the controlled component pattern by managing progress state and passing it as a prop to the LoadingBar component.
The loading bar supports extensive visual customization:
Fine-tune loading animations with timing properties:
Two distinct loading modes provide different user experience patterns:
Both modes support custom starting values and complete visual customization.