fix(ui): adds delay to progress bar for fast networks (#11157)

On fast networks where page transitions are quick, such as local dev in
most cases, the progress bar should not render. This leads to a constant
flashing of the progress bar at the top of the screen and does not
provide any value.

The fix is to add a delay to the initial rendering of the progress bar,
and only show if the transition takes longer than _n_ milliseconds. This
value can be adjusted as needed, but right now is set to 150ms.

Introduced in #9275.
This commit is contained in:
Jacob Fletcher
2025-02-13 12:35:41 -05:00
committed by GitHub
parent f4639c418f
commit de68ef4548
2 changed files with 21 additions and 4 deletions

View File

@@ -1,11 +1,12 @@
'use client'
import React, { useEffect } from 'react'
import React, { useEffect, useRef } from 'react'
import { useRouteTransition } from '../index.js'
import './index.scss'
const transitionDuration = 200
const baseClass = 'progress-bar'
const initialDelay = 150
/**
* Renders a progress bar that shows the progress of a route transition.
@@ -23,13 +24,24 @@ const baseClass = 'progress-bar'
export const ProgressBar = () => {
const { isTransitioning, transitionProgress } = useRouteTransition()
const [progressToShow, setProgressToShow] = React.useState<null | number>(null)
const shouldDelayProgress = useRef(true)
useEffect(() => {
let clearTimerID: NodeJS.Timeout
let delayTimerID: NodeJS.Timeout
if (isTransitioning) {
setProgressToShow(transitionProgress)
if (shouldDelayProgress.current) {
delayTimerID = setTimeout(() => {
setProgressToShow(transitionProgress)
shouldDelayProgress.current = false
}, initialDelay)
} else {
setProgressToShow(transitionProgress)
}
} else {
shouldDelayProgress.current = true
// Fast forward to 100% when the transition is complete
// Then fade out the progress bar directly after
setProgressToShow(1)
@@ -41,7 +53,10 @@ export const ProgressBar = () => {
}, transitionDuration * 2)
}
return () => clearTimeout(clearTimerID)
return () => {
clearTimeout(clearTimerID)
clearTimeout(delayTimerID)
}
}, [isTransitioning, transitionProgress])
if (typeof progressToShow === 'number') {
@@ -64,4 +79,6 @@ export const ProgressBar = () => {
</div>
)
}
return null
}

View File

@@ -31,7 +31,7 @@
}
],
"paths": {
"@payload-config": ["./test/_community/config.ts"],
"@payload-config": ["./test/admin/config.ts"],
"@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],