Skip to content
On this page

useNProgress

Category
Export Size
462 B
Package
@vueuse/integrations
Last Changed
5 months ago

Reactive wrapper for nprogress.

Demo

Click to change progress status
Available in the @vueuse/integrations add-on.

Install

bash
npm i nprogress
npm i nprogress

Usage

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { isLoading } = useNProgress()

function toggle() {
  isLoading.value = !isLoading.value
}
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { isLoading } = useNProgress()

function toggle() {
  isLoading.value = !isLoading.value
}

Passing a progress percentage

You can pass a percentage to indicate where the bar should start from.

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { progress } = useNProgress(0.5)

function done() {
  progress.value = 1.0
}
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { progress } = useNProgress(0.5)

function done() {
  progress.value = 1.0
}

To change the progress percentage, set progress.value = n, where n is a number between 0..1.

Customization

Just edit nprogress.css to your liking. Tip: you probably only want to find and replace occurrences of #29d.

You can configure it by passing an object as a second parameter.

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

useNProgress(null, {
  minimum: 0.1,
  // ...
})
import { useNProgress } from '@vueuse/integrations/useNProgress'

useNProgress(null, {
  minimum: 0.1,
  // ...
})

Type Declarations

typescript
export declare type UseNProgressOptions = Partial<NProgressOptions>
/**
 * Reactive progress bar.
 *
 * @see https://vueuse.org/useNProgress
 */
export declare function useNProgress(
  currentProgress?: MaybeComputedRef<number | null | undefined>,
  options?: UseNProgressOptions
): {
  isLoading: WritableComputedRef<boolean>
  progress: Ref<number | (() => number | null | undefined) | null | undefined>
  start: () => nprogress.NProgress
  done: (force?: boolean | undefined) => nprogress.NProgress
  remove: () => void
}
export declare type UseNProgressReturn = ReturnType<typeof useNProgress>
export declare type UseNProgressOptions = Partial<NProgressOptions>
/**
 * Reactive progress bar.
 *
 * @see https://vueuse.org/useNProgress
 */
export declare function useNProgress(
  currentProgress?: MaybeComputedRef<number | null | undefined>,
  options?: UseNProgressOptions
): {
  isLoading: WritableComputedRef<boolean>
  progress: Ref<number | (() => number | null | undefined) | null | undefined>
  start: () => nprogress.NProgress
  done: (force?: boolean | undefined) => nprogress.NProgress
  remove: () => void
}
export declare type UseNProgressReturn = ReturnType<typeof useNProgress>

Source

SourceDemoDocs

Contributors

Anthony Fu
Antério Vieira
Agénor Debriat
webfansplz
Curt Grimes
Jelf
Michael Hüneburg
Alex Kozack

Changelog

v9.0.0 on 7/26/2022
a33a2 - fix: improve SSR support (#1990)
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.2.6 on 4/14/2022
14ae0 - fix: allow partial options (#1487)

Released under the MIT License.