NProgress

repository·master·Indexed 12 days ago

https://github.com/rstacruz/nprogress

A minimalist progress bar library for Ajax-heavy applications, providing a slim loading indicator inspired by Google, YouTube, and Medium. Version 0.2.0 includes a manual API for controlling progress via start(), done(), set(), and inc(), as well as integration support for Turbolinks and Pjax. It features a configurable API for customizing animation speed, easing, and templates, and includes a promise() method for tracking jQuery promises.

Tokens
2K
Snippets
10
Records
12
Agent score
49%

What's inside NProgress

  1. Install NProgress via npm or CDN

    master

    You can install NProgress using npm or include the assets directly via CDN.

    Using npm:

    $ npm install --save nprogress

    Using CDN (unpkg):

    • JS: https://unpkg.com/nprogress@0.2.0/nprogress.js
    • CSS: https://unpkg.com/nprogress@0.2.0/nprogress.css

    Manual Setup: Ensure both nprogress.js and nprogress.css are added to your project and linked in your HTML.

    <script src='nprogress.js'></script>
    <link rel='stylesheet' href='nprogress.css'/>
  2. Configure NProgress options

    master

    Use NProgress.configure() to customize the behavior and appearance of the progress bar.

    Available configuration keys:

    • minimum: Minimum percentage upon starting (default: 0.08).
    • template: Custom HTML markup. Must include an element with role='bar'.
    • easing: CSS easing string (default: 'ease').
    • speed: Animation speed in ms (default: 200).
    • trickle: Boolean to enable/disable automatic incrementing (default: true).
    • trickleSpeed: Interval for trickle/increment in ms (default: 200).
    • showSpinner: Boolean to show/hide the loading spinner (default: true).
    • parent: The container element (default: body).
    NProgress.configure({
      minimum: 0.1,
      easing: 'ease',
      speed: 500,
      trickle: false,
      showSpinner: false,
      parent: '#container'
    });
  3. Integrate NProgress with Turbolinks (version 3 and below)

    master

    For Turbolinks 1.3.0+, use these event listeners:

    $(document).on('page:fetch',   function() { NProgress.start(); });
    $(document).on('page:change',  function() { NProgress.done(); });
    $(document).on('page:restore', function() { NProgress.remove(); });
  4. Integrate NProgress with Turbolinks (version 5+)

    master

    For Turbolinks 5+, use the following event listeners to trigger the progress bar during page transitions:

    $(document).on('turbolinks:click', function() {
      NProgress.start();
    });
    $(document).on('turbolinks:render', function() {
      NProgress.done();
      NProgress.remove();
    });
  5. Advanced NProgress API: Percentages, Incrementing, and Status

    master

    NProgress provides fine-grained control over the progress state:

    • Set specific percentage: Use .set(n) where n is a number between 0..1.
    • Increment progress: Use .inc() to increment by a random amount (useful for image loads). Use .inc(n) to increment by a specific value (e.g., 0.2). Note that incrementing will never reach 100%.
    • Force completion: Call .done(true) to show the progress bar even if .start() was never called.
    • Get current status: Access the current progress value via the .status property.
    NProgress.set(0.4);     // Set to 40%
    NProgress.inc(0.2);     // Increment by 0.2
    NProgress.done(true);   // Force completion
    console.log(NProgress.status); // Get current status
  6. Configure NProgress settings

    master

    Use NProgress.configure(options) to override default settings. The options object can contain any of the following keys:

    KeyDefaultDescription
    minimum0.08The minimum progress value shown when starting.
    easing'linear'The CSS easing function for the bar transition.
    positionUsing''CSS positioning method: 'translate3d', 'translate', or 'margin'.
    speed200Transition speed in milliseconds.
    trickletrueWhether to automatically increment progress while waiting.
    trickleSpeed200Interval in milliseconds for the trickle effect.
    showSpinnertrueWhether to show the loading spinner.
    barSelector'[role="bar"]'CSS selector for the progress bar element.
    spinnerSelector'[role="spinner"]'CSS selector for the spinner element.
    parent'body'The DOM element or selector to which the progress bar is appended.
    template(HTML string)The HTML template used for the bar and spinner.
    NProgress.configure({
      minimum: 0.1,
      showSpinner: false,
      speed: 400
    });
  7. Control the progress bar with NProgress API

    master

    NProgress provides a simple API to manage the lifecycle and state of the progress bar. You can start the bar, increment it manually, set it to a specific value, or finish the progress animation.

    • NProgress.start(): Shows the progress bar. If the bar is already visible, it starts a 'trickle' effect that increments the progress automatically.
    • NProgress.done(force): Hides the progress bar. If force is true, it will show the bar even if it was previously hidden. It performs a realistic motion animation before finishing.
    • NProgress.set(n): Sets the progress bar to a specific completion level, where n is a number from 0.0 to 1.0.
    • NProgress.inc(amount): Increments the progress by a given amount. If amount is not a number, it uses a predefined step based on the current progress level. If the bar hasn't started, calling inc() will call start().
    • NProgress.trickle(): A convenience method that calls inc() without arguments to move the bar forward slightly.
    NProgress.start();
    
    // Increment manually
    NProgress.inc(0.1);
    
    // Set to a specific value
    NProgress.set(0.5);
    
    // Finish the progress
    NProgress.done();
  8. Track multiple jQuery promises with NProgress.promise()

    master

    If you are using jQuery, NProgress.promise($promise) allows you to track the progress of one or more jQuery promises.

    When you pass a promise to this method, NProgress starts the bar. As each promise resolves, the progress bar increments. Once all supplied promises have resolved, NProgress.done() is called automatically.

    Note: This method requires the presence of jQuery and expects the promise to have a .state() method and an .always() method.

    // Example with a single jQuery promise
    const myPromise = $.ajax('/api/data');
    NProgress.promise(myPromise);
    
    // Example with multiple promises
    const p1 = $.ajax('/api/1');
    const p2 = $.ajax('/api/2');
    NProgress.promise(p1);
    NProgress.promise(p2);
  9. Check NProgress status and rendering state

    master

    You can query the current state of NProgress using these methods:

    • NProgress.isStarted(): Returns true if the progress bar is currently active (i.e., NProgress.status is a number).
    • NProgress.isRendered(): Returns true if the progress bar element is currently present in the DOM.
    • NProgress.status: A property that holds the current progress value (a number from 0.0 to 1.0). It is null when the bar is not active.