{"version":3,"file":"react-nprogress.cjs","names":["defaultIncrement","useReducer","useRef","increment"],"sources":["../src/clamp.ts","../src/createTimeout.ts","../src/increment.ts","../src/useNProgress.tsx","../src/NProgress.tsx"],"sourcesContent":["export const clamp = (num: number, lower: number, upper: number): number => {\n  num = num <= upper ? num : upper\n  num = num >= lower ? num : lower\n  return num\n}\n","// Uses requestAnimationFrame rather than setTimeout for smoother animation\n// timing. Note that rAF is throttled or paused in background tabs, so progress\n// will stall when the tab is hidden and resume when it regains focus.\nexport const createTimeout = () => {\n  let handle: number | undefined\n\n  const cancel = (): void => {\n    if (handle !== undefined) {\n      window.cancelAnimationFrame(handle)\n    }\n  }\n\n  const schedule = (callback: () => void, delay: number): void => {\n    cancel()\n\n    let deltaTime\n    let start: number | undefined\n    const frame: FrameRequestCallback = (time) => {\n      start = start || time\n      deltaTime = time - start\n      if (deltaTime > delay) {\n        callback()\n        return\n      }\n      handle = window.requestAnimationFrame(frame)\n    }\n\n    handle = window.requestAnimationFrame(frame)\n  }\n\n  return {\n    cancel,\n    schedule,\n  }\n}\n","import { clamp } from './clamp'\n\nexport const increment = (progress: number): number => {\n  let amount = 0\n\n  if (progress >= 0 && progress < 0.2) {\n    amount = 0.1\n  } else if (progress >= 0.2 && progress < 0.5) {\n    amount = 0.04\n  } else if (progress >= 0.5 && progress < 0.8) {\n    amount = 0.02\n  } else if (progress >= 0.8 && progress < 0.99) {\n    amount = 0.005\n  }\n\n  return clamp(progress + amount, 0, 0.994)\n}\n","import { useEffect, useReducer, useRef } from 'react'\n\nimport { clamp } from './clamp'\nimport { createTimeout } from './createTimeout'\nimport { increment as defaultIncrement } from './increment'\nimport type { NProgressOptions, NProgressState } from './types'\n\n// A four-phase state machine. `idle` and `finished` both report\n// `isFinished: true` and differ only in the progress they hold, so the phase,\n// not `isFinished`, is what decides which transitions and timers apply.\ntype Phase = 'animating' | 'completing' | 'finished' | 'idle'\n\ninterface State {\n  phase: Phase\n  progress: number\n}\n\ntype Action =\n  | {\n      increment: (progress: number) => number\n      minimum: number\n      type: 'trickle'\n    }\n  | { minimum: number; type: 'start' }\n  | { type: 'complete' }\n  | { type: 'finish' }\n\nconst initialState: State = {\n  phase: 'idle',\n  progress: 0,\n}\n\nconst reducer = (state: State, action: Action): State => {\n  switch (action.type) {\n    case 'complete':\n      // The original nprogress `done()` computes a random progress jump before\n      // animating to 1, but its queue runs both steps in the same tick, so the\n      // jump's CSS is overwritten before paint and never renders. Omitting the\n      // jump changes nothing visually.\n      //\n      // Ignored unless an animation is actually running, which is what makes a\n      // StrictMode double-mount a no-op rather than a spurious completion.\n      return state.phase === 'animating'\n        ? { phase: 'completing', progress: 1 }\n        : state\n\n    case 'finish':\n      return { phase: 'finished', progress: 1 }\n\n    case 'start':\n      // Matches the original nprogress `start()`, which calls set(0) and so\n      // paints first at `minimum` before any trickle runs.\n      //\n      // Guarded the same way as `complete`, and for the same reason: a repeat\n      // dispatch against a running animation must not rewind the bar. That\n      // happens whenever `minimum` changes mid-animation, as well as on a\n      // StrictMode double-mount.\n      return state.phase === 'animating'\n        ? state\n        : {\n            phase: 'animating',\n            progress: clamp(0, action.minimum, 1),\n          }\n\n    case 'trickle':\n      // Clamped here rather than trusted from the increment function, so a\n      // custom one cannot take the bar outside the documented range. Stopping\n      // short of 1 is that function's own business: 1 is what completion\n      // means.\n      return {\n        ...state,\n        progress: clamp(action.increment(state.progress), action.minimum, 1),\n      }\n  }\n}\n\nexport const useNProgress = ({\n  animationDuration = 200,\n  increment = defaultIncrement,\n  incrementDuration = 200,\n  isAnimating = false,\n  minimum = 0.08,\n}: NProgressOptions = {}): NProgressState => {\n  const [{ phase, progress }, dispatch] = useReducer(reducer, initialState)\n\n  // Held in a ref so the trickle timer can read the latest increment function\n  // without listing it as a dependency. Consumers commonly pass an inline\n  // function, whose identity changes every render; depending on it directly\n  // would cancel and recreate the timer each time, and a render loop faster\n  // than `incrementDuration` would stop the bar advancing altogether.\n  const incrementRef = useRef(increment)\n  useEffect(() => {\n    incrementRef.current = increment\n  })\n\n  useEffect(() => {\n    dispatch(isAnimating ? { minimum, type: 'start' } : { type: 'complete' })\n  }, [isAnimating, minimum])\n\n  // A timer per running phase, rather than one effect branching over both.\n  // Each then depends only on the options its own phase reads, so changing an\n  // option the running phase ignores cannot cancel its timer. Both are keyed\n  // on the phase rather than on progress, so trickling does not tear down and\n  // recreate the timer mid-animation.\n  useEffect(() => {\n    if (phase !== 'animating') {\n      return\n    }\n\n    const timeout = createTimeout()\n\n    const trickle = () => {\n      dispatch({ increment: incrementRef.current, minimum, type: 'trickle' })\n      timeout.schedule(trickle, incrementDuration)\n    }\n    timeout.schedule(trickle, incrementDuration)\n\n    return () => timeout.cancel()\n  }, [incrementDuration, minimum, phase])\n\n  useEffect(() => {\n    if (phase !== 'completing') {\n      return\n    }\n\n    const timeout = createTimeout()\n    timeout.schedule(() => dispatch({ type: 'finish' }), animationDuration)\n\n    return () => timeout.cancel()\n  }, [animationDuration, phase])\n\n  return {\n    animationDuration,\n    isFinished: phase === 'finished' || phase === 'idle',\n    progress,\n  }\n}\n","import type { FC, ReactElement } from 'react'\n\nimport type { NProgressOptions, NProgressState } from './types'\nimport { useNProgress } from './useNProgress'\n\ntype Props = NProgressOptions & {\n  children: (renderProps: NProgressState) => ReactElement\n}\n\nexport const NProgress: FC<Props> = ({ children, ...restProps }: Props) => {\n  const renderProps = useNProgress(restProps)\n  return children(renderProps)\n}\n"],"mappings":";;;;AAAA,MAAa,SAAS,KAAa,OAAe,UAA0B;CAC1E,MAAM,OAAO,QAAQ,MAAM;CAC3B,MAAM,OAAO,QAAQ,MAAM;CAC3B,OAAO;AACT;;;ACDA,MAAa,sBAAsB;CACjC,IAAI;CAEJ,MAAM,eAAqB;EACzB,IAAI,WAAW,KAAA,GACb,OAAO,qBAAqB,MAAM;CAEtC;CAEA,MAAM,YAAY,UAAsB,UAAwB;EAC9D,OAAO;EAEP,IAAI;EACJ,IAAI;EACJ,MAAM,SAA+B,SAAS;GAC5C,QAAQ,SAAS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