forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadingProgress.js
46 lines (42 loc) · 1.24 KB
/
LoadingProgress.js
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
37
38
39
40
41
42
43
44
45
46
import { loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
/**
* 加载进度条
* NProgress实现
*/
export default function LoadingProgress() {
const router = useRouter()
const [NProgress, setNProgress] = useState(null)
// 加载进度条
useEffect(() => {
loadExternalResource(
'https://cdnjs.snrat.com/ajax/libs/nprogress/0.2.0/nprogress.min.js',
'js'
).then(() => {
if (window.NProgress) {
setNProgress(window.NProgress)
// 调速
window.NProgress.settings.minimun = 0.1
loadExternalResource(
'https://cdnjs.snrat.com/ajax/libs/nprogress/0.2.0/nprogress.min.css',
'css'
)
}
})
const handleStart = url => {
NProgress?.start()
}
const handleStop = () => {
NProgress?.done()
}
router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeError', handleStop)
router.events.on('routeChangeComplete', handleStop)
return () => {
router.events.off('routeChangeStart', handleStart)
router.events.off('routeChangeComplete', handleStop)
router.events.off('routeChangeError', handleStop)
}
}, [router])
}