From de68ef4548b297cfab2df00da47bd0e027a80e57 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Thu, 13 Feb 2025 12:35:41 -0500 Subject: [PATCH] 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. --- .../RouteTransition/ProgressBar/index.tsx | 23 ++++++++++++++++--- tsconfig.base.json | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/ui/src/providers/RouteTransition/ProgressBar/index.tsx b/packages/ui/src/providers/RouteTransition/ProgressBar/index.tsx index 643685f082..3816d3e38e 100644 --- a/packages/ui/src/providers/RouteTransition/ProgressBar/index.tsx +++ b/packages/ui/src/providers/RouteTransition/ProgressBar/index.tsx @@ -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) + 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 = () => { ) } + + return null } diff --git a/tsconfig.base.json b/tsconfig.base.json index ffd7ec771c..c461af5dcb 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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"],