BProgress

repository·main·Indexed 20 days ago

https://github.com/imskyleen/bprogress

A modern, TypeScript-based progress bar library designed as an alternative to NProgress. It provides smooth, animated loading indicators for web applications with a core package (@bprogress/core) and specialized integrations for React, Next.js, Remix, and Vue.

Tokens
50.2K
Snippets
221
Records
269
Agent score
69%

What's inside bprogress

  1. What is BProgress?

    main

    BProgress (short for "Bar Progress") is a modern, TypeScript-based reimplementation of the NProgress library. It is designed to provide lightweight, smooth, and animated progress bars to visually indicate loading states during page transitions, data loading, or asynchronous operations.

    Key improvements over the original NProgress include:

    • Modern TypeScript codebase: Fully rewritten for better type safety and developer experience.
    • Active Maintenance: Addresses long-standing community requests and provides modern best practices.
    • Framework-Specific Packages: Dedicated integrations for Next.js, React, Remix, and Vue to ensure seamless and efficient usage within specific ecosystems.
  2. Overview of BProgress

    main
    BProgress is a modern TypeScript reimplementation of NProgress. It provides a lightweight, animated progress bar to visually indicate loading states during page transitions, data loading, or asynchronous operations. It is designed to be more performant and customizable than the original NProgress, following modern best practices and offering dedicated integration packages for various web frameworks.
  3. Configure positionUsing for absolute progress bars

    main

    By default, BProgress uses translate3d for positioning to leverage GPU acceleration and ensure smooth animations.

    If you need to create absolute position progress bars inside elements that are not directly aligned with the screen edges, you must set the positionUsing option to 'width' within the ProgressProvider options.

    <template>
      <ProgressProvider :options="{ template: null, positionUsing: 'width' }">
        <Progress />
      </ProgressProvider>
    </template>
  4. Use multiple progress bars with custom templates

    main

    Starting from version 1.1, BProgress supports using multiple progress bars simultaneously. To enable this mode, you must set the template option to null in the global configuration.

    Once configured, you can manually add progress bar HTML structures anywhere in your application. Ensure that the container div with the class bprogress is set to display: none by default in your CSS to prevent it from appearing before it is activated.

    BProgress.configure({
      template: null,
    });
    <div class="bprogress" style="display: none">
      <div class="bar">
        <div class="peg"></div>
      </div>
      <div class="spinner">
        <div class="spinner-icon"></div>
      </div>
      <div class="indeterminate">
        <div class="inc"></div>
        <div class="dec"></div>
      </div>
    </div>
  5. Use the useAnchorProgress hook for navigation detection

    main

    The useAnchorProgress hook is used to trigger a progress bar in frameworks where the router does not natively detect the start and end of navigation (for example, the Next.js App Directory router). It achieves this by activating navigation detection based on <a> elements in the DOM and window.history changes.

    This differs from the useProgress hook, which should be used if your framework's router does provide explicit start and end events (like the Next.js Pages Directory router).

    import { useAnchorProgress } from '@bprogress/react';
    
    // Inside a component
    useAnchorProgress(
      {
        // options object
      },
      [dependencies]
    );
  6. Eject the configuration with `npm run eject`

    main

    If you need full control over the build tool and configuration (webpack, Babel, ESLint, etc.), you can run npm run eject.

    Warning: This is a one-way operation. Once you eject, you cannot go back.

    Running this command removes the single build dependency and copies all configuration files and transitive dependencies directly into your project. After ejecting, all commands except eject will still work but will point to the newly copied scripts, allowing you to tweak them manually.

    npm run eject
  7. Use the Bar component

    main

    The Bar component is used to render the progress bar element. It must be used inside a Progress component to function correctly.

    To use it, import Bar from your specific framework package (e.g., @bprogress/next, @bprogress/react, or @bprogress/remix) and place it within the Progress component. You can optionally pass children to the Bar, such as a Peg component or a custom element, to represent the progress indicator.

    import { Bar } from '@bprogress/next';
    
    // Inside a Progress component
    <Bar>
      <Peg />
    </Bar>
  8. Use the Progress component

    main

    The Progress component allows you to integrate one or more progress bars anywhere in your application. If you do not use a custom Progress component, a default progress bar is created automatically by the provider.

    Important Requirements

    1. Provider Configuration: The Progress component must be used inside a ProgressProvider where the template option is explicitly set to null.
    2. DOM Behavior: When using a custom component, BProgress toggles visibility between display: block and display: none rather than adding/removing the component from the DOM. This may impact performance for complex components.
    3. Display Property: The component has display: none; by default and switches to display: block; when active. You cannot manually override the display property via styles.
    import { Progress } from '@bprogress/next'; // Replace with your specific package
    import { ProgressProvider } from '@bprogress/next';
    
    // The Progress component MUST be wrapped in a Provider with template: null
    <ProgressProvider options={{ template: null }}>
      <Progress />
    </ProgressProvider>