1
1
import throttle from 'lodash.throttle'
2
2
import { useCallback , useEffect , useState } from 'react'
3
- import FloatDarkModeButton from './FloatDarkModeButton '
4
- import JumpToTopButton from './JumpToTopButton '
3
+ import ButtonDarkModeFloat from './ButtonFloatDarkMode '
4
+ import ButtonJumpToTop from './ButtonJumpToTop '
5
5
6
6
/**
7
7
* 悬浮在右下角的按钮,当页面向下滚动100px时会出现
@@ -10,33 +10,40 @@ import JumpToTopButton from './JumpToTopButton'
10
10
*/
11
11
export default function RightFloatArea ( { floatSlot } ) {
12
12
const [ showFloatButton , switchShow ] = useState ( false )
13
- const scrollListener = useCallback ( throttle ( ( ) => {
14
- const targetRef = document . getElementById ( 'wrapper' )
15
- const clientHeight = targetRef ?. clientHeight
16
- const scrollY = window . pageYOffset
17
- const fullHeight = clientHeight - window . outerHeight
18
- let per = parseFloat ( ( ( scrollY / fullHeight ) * 100 ) . toFixed ( 0 ) )
19
- if ( per > 100 ) per = 100
20
- const shouldShow = scrollY > 100 && per > 0
13
+ const scrollListener = useCallback (
14
+ throttle ( ( ) => {
15
+ const targetRef = document . getElementById ( 'wrapper' )
16
+ const clientHeight = targetRef ?. clientHeight
17
+ const scrollY = window . pageYOffset
18
+ const fullHeight = clientHeight - window . outerHeight
19
+ let per = parseFloat ( ( ( scrollY / fullHeight ) * 100 ) . toFixed ( 0 ) )
20
+ if ( per > 100 ) per = 100
21
+ const shouldShow = scrollY > 100 && per > 0
21
22
22
- // 右下角显示悬浮按钮
23
- if ( shouldShow !== showFloatButton ) {
24
- switchShow ( shouldShow )
25
- }
26
- } , 200 ) )
23
+ // 右下角显示悬浮按钮
24
+ if ( shouldShow !== showFloatButton ) {
25
+ switchShow ( shouldShow )
26
+ }
27
+ } , 200 )
28
+ )
27
29
28
30
useEffect ( ( ) => {
29
31
document . addEventListener ( 'scroll' , scrollListener )
30
32
return ( ) => document . removeEventListener ( 'scroll' , scrollListener )
31
33
} , [ ] )
32
34
33
35
return (
34
- < div className = { ( showFloatButton ? 'opacity-100 ' : 'invisible opacity-0' ) + ' duration-300 transition-all bottom-12 right-1 fixed justify-end z-20 text-white bg-indigo-500 dark:bg-hexo-black-gray rounded-sm' } >
35
- < div className = { 'justify-center flex flex-col items-center cursor-pointer' } >
36
- < FloatDarkModeButton />
37
- { floatSlot }
38
- < JumpToTopButton />
39
- </ div >
40
- </ div >
36
+ < div
37
+ className = {
38
+ ( showFloatButton ? 'opacity-100 ' : 'invisible opacity-0' ) +
39
+ ' duration-300 transition-all bottom-12 right-1 fixed justify-end z-20 text-white bg-indigo-500 dark:bg-hexo-black-gray rounded-sm'
40
+ } >
41
+ < div
42
+ className = { 'justify-center flex flex-col items-center cursor-pointer' } >
43
+ < ButtonDarkModeFloat />
44
+ { floatSlot }
45
+ < ButtonJumpToTop />
46
+ </ div >
47
+ </ div >
41
48
)
42
49
}
0 commit comments