-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading-bar.tsx
More file actions
36 lines (29 loc) · 1.16 KB
/
Copy pathloading-bar.tsx
File metadata and controls
36 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use client' // This ensures the component runs client-side
import { useState, useEffect } from 'react'
import { useRouter } from 'next/navigation' // from next/navigation for the app router
const LoadingBar = () => {
const router = useRouter()
const [loading, setLoading] = useState(true)
useEffect(() => {
const handleStart = () => setLoading(true)
const handleComplete = () => setLoading(false)
// The app router doesn't expose router events, so we can listen to navigation start/end with manual calls.
// Any programmatic navigation (router.push, router.replace) should trigger the loading state.
// If you have links using <Link>, it will automatically handle navigation without needing events.
handleStart()
return () => {
handleComplete() // Cleanup on component unmount
}
}, [router])
return (
loading && (
<div
className="flex w-full h-[1px] z-50 bg-red-500 animate__animated animate__fadeIn animate__faster"
style={{ animationDuration: '0.5s' }}
>
<div className="h-full bg-blue-600 animate-pulse" style={{ width: '100%' }} />
</div>
)
)
}
export default LoadingBar