Skip to content

Conversation

@Armadillidiid
Copy link

Summary

This PR adds two features:

  1. Start Delay: Optional startDelay prop to ProgressBarProvider
  2. Complete Control: Explicit control to stop/complete the progress bar with start and complete functions

Motivation

My primary motivation behind this is to prevent progress bars from appearing for fast route changes. Showing a progress indicator for every route change isn't necessary if the navigation completes in under 300ms (imho)

To achieve this behavior, I needed:

  1. A way to delay the progress bar appearance until a certain threshold
  2. Explicit control to cancel the progress bar if the route change completes before the delay

In the existing implementation, it only provides a start() function with no way to cancel or complete the progress explicitly.

API Changes

Before

const startProgress = useProgress();
startProgress(); // Only way to control progress

After

const { start, complete } = useProgress();
start();    // Start progress
complete(); // Explicitly complete progress

New Props

<ProgressBarProvider startDelay={300}>
  <ProgressBar className="..." />
</ProgressBarProvider>

Usage

export const Link = ({ children, ...props }) => {
  const pathname = usePathname();
  const initialPathnameRef = useRef(pathname);
  const router = useRouter();
  const progress = useProgress();

  // Watch for route changes and complete progress
  useEffect(() => {
    if (pathname !== initialPathnameRef.current) {
      progress.complete();
    }
  }, [pathname]);

  return (
    <NextLink
      onClick={(e) => {
        onClick?.(e);
        if (isModifiedEvent(e)) return;
        e.preventDefault();
        startTransition(() => {
          const { href, replace, scroll } = props;
          progress.start();
          // @ts-expect-error "Auth type doesn't match"
          const url = typeof href === "string" ? href : formatUrl(href);
          if (replace) {
            router.replace(url, { scroll });
          } else {
            router.push(url, { scroll });
          }
        });
      }}
      {...props}
    >
      {children}
    </NextLink>
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant