Эх сурвалжийг харах

feat: add loading indicator during page transitions (#119)

* feat: add loading indicator during page transitions

* fix: update loading bar color

---------
Co-authored-by: satnaing <satnaingdev@gmail.com>
UncleBigBay 1 жил өмнө
parent
commit
fb9e0b9323

+ 1 - 0
package.json

@@ -48,6 +48,7 @@
     "react-day-picker": "8.10.1",
     "react-day-picker": "8.10.1",
     "react-dom": "^19.0.0",
     "react-dom": "^19.0.0",
     "react-hook-form": "^7.54.0",
     "react-hook-form": "^7.54.0",
+    "react-top-loading-bar": "^3.0.2",
     "recharts": "^2.14.1",
     "recharts": "^2.14.1",
     "tailwind-merge": "^2.5.5",
     "tailwind-merge": "^2.5.5",
     "tailwindcss-animate": "^1.0.7",
     "tailwindcss-animate": "^1.0.7",

+ 13 - 0
pnpm-lock.yaml

@@ -113,6 +113,9 @@ importers:
       react-hook-form:
       react-hook-form:
         specifier: ^7.54.0
         specifier: ^7.54.0
         version: 7.54.0(react@19.0.0)
         version: 7.54.0(react@19.0.0)
+      react-top-loading-bar:
+        specifier: ^3.0.2
+        version: 3.0.2(react@19.0.0)
       recharts:
       recharts:
         specifier: ^2.14.1
         specifier: ^2.14.1
         version: 2.14.1(react-dom@19.0.0(react@19.0.0))(react@19.0.0)
         version: 2.14.1(react-dom@19.0.0(react@19.0.0))(react@19.0.0)
@@ -2604,6 +2607,12 @@ packages:
       '@types/react':
       '@types/react':
         optional: true
         optional: true
 
 
+  react-top-loading-bar@3.0.2:
+    resolution: {integrity: sha512-hW0CHrHqKdBOBsVhms73ka0rgb9/aoiRfqo7jiS1vwIYDK7VkyMj52ypo5ewhshTQGHKE6mRvR99GZBZ+FuM/Q==}
+    engines: {node: '>=14'}
+    peerDependencies:
+      react: ^16 || ^17 || ^18 || ^19
+
   react-transition-group@4.4.5:
   react-transition-group@4.4.5:
     resolution: {integrity: sha512-pZcd1MCJoiKiBR2NRxeCRg13uCXbydPnmB4EOeRrY7480qNWO8IIgQG6zlDkm6uRMsURXPuKq0GWtiM59a5Q6g==}
     resolution: {integrity: sha512-pZcd1MCJoiKiBR2NRxeCRg13uCXbydPnmB4EOeRrY7480qNWO8IIgQG6zlDkm6uRMsURXPuKq0GWtiM59a5Q6g==}
     peerDependencies:
     peerDependencies:
@@ -5188,6 +5197,10 @@ snapshots:
     optionalDependencies:
     optionalDependencies:
       '@types/react': 19.0.1
       '@types/react': 19.0.1
 
 
+  react-top-loading-bar@3.0.2(react@19.0.0):
+    dependencies:
+      react: 19.0.0
+
   react-transition-group@4.4.5(react-dom@19.0.0(react@19.0.0))(react@19.0.0):
   react-transition-group@4.4.5(react-dom@19.0.0(react@19.0.0))(react@19.0.0):
     dependencies:
     dependencies:
       '@babel/runtime': 7.26.0
       '@babel/runtime': 7.26.0

+ 25 - 0
src/components/navigation-progress.tsx

@@ -0,0 +1,25 @@
+import { useEffect, useRef } from 'react'
+import { useRouterState } from '@tanstack/react-router'
+import LoadingBar, { LoadingBarRef } from 'react-top-loading-bar'
+
+export function NavigationProgress() {
+  const ref = useRef<LoadingBarRef>(null)
+  const state = useRouterState()
+
+  useEffect(() => {
+    if (state.status === 'pending') {
+      ref.current?.continuousStart()
+    } else {
+      ref.current?.complete()
+    }
+  }, [state.status])
+
+  return (
+    <LoadingBar
+      color='hsl(var(--muted-foreground))'
+      ref={ref}
+      shadow={true}
+      height={2}
+    />
+  )
+}

+ 2 - 0
src/routes/__root.tsx

@@ -3,6 +3,7 @@ import { createRootRouteWithContext, Outlet } from '@tanstack/react-router'
 import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
 import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
 import { TanStackRouterDevtools } from '@tanstack/router-devtools'
 import { TanStackRouterDevtools } from '@tanstack/router-devtools'
 import { Toaster } from '@/components/ui/toaster'
 import { Toaster } from '@/components/ui/toaster'
+import { NavigationProgress } from '@/components/navigation-progress'
 import GeneralError from '@/features/errors/general-error'
 import GeneralError from '@/features/errors/general-error'
 import NotFoundError from '@/features/errors/not-found-error'
 import NotFoundError from '@/features/errors/not-found-error'
 
 
@@ -12,6 +13,7 @@ export const Route = createRootRouteWithContext<{
   component: () => {
   component: () => {
     return (
     return (
       <>
       <>
+        <NavigationProgress />
         <Outlet />
         <Outlet />
         <Toaster />
         <Toaster />
         {import.meta.env.MODE === 'development' && (
         {import.meta.env.MODE === 'development' && (