1- import React , { useEffect , useState } from "react" ;
1+ import React , { useState , useEffect } from "react" ;
22import { FaArrowUp } from "react-icons/fa" ;
33
44export default function ScrollBottomToTop ( ) {
5- const [ showButton , setShowButton ] = useState ( false ) ;
5+ const [ isVisible , setIsVisible ] = useState ( false ) ;
6+
7+ const toggleVisibility = ( ) => {
8+ if ( window . pageYOffset > 300 ) {
9+ setIsVisible ( true ) ;
10+ } else {
11+ setIsVisible ( false ) ;
12+ }
13+ } ;
614
715 const scrollToTop = ( ) => {
816 window . scrollTo ( {
@@ -11,29 +19,25 @@ export default function ScrollBottomToTop() {
1119 } ) ;
1220 } ;
1321
14- const handleScroll = ( ) => {
15- if ( window . scrollY > 300 ) {
16- setShowButton ( true ) ;
17- } else {
18- setShowButton ( false ) ;
19- }
20- } ;
21-
2222 useEffect ( ( ) => {
23- window . addEventListener ( "scroll" , handleScroll ) ;
23+ window . addEventListener ( "scroll" , toggleVisibility ) ;
24+
2425 return ( ) => {
25- window . removeEventListener ( "scroll" , handleScroll ) ;
26+ window . removeEventListener ( "scroll" , toggleVisibility ) ;
2627 } ;
2728 } , [ ] ) ;
2829
2930 return (
30- showButton && (
31- < button
32- onClick = { scrollToTop }
33- className = "fixed right-5 bottom-20 z-50 cursor-pointer rounded-lg border-none bg-blue-600 p-2.5 text-white opacity-80 shadow-md transition-opacity duration-300 hover:bg-blue-700 hover:opacity-100"
34- >
35- < FaArrowUp />
36- </ button >
37- )
31+ < div className = "scroll-to-top" >
32+ { isVisible && (
33+ < button
34+ onClick = { scrollToTop }
35+ className = "fixed right-5 bottom-5 z-50 cursor-pointer rounded-full border-none bg-gray-700 p-3 text-white shadow-lg transition-opacity duration-300 hover:bg-gray-800 dark:bg-gray-600 dark:hover:bg-gray-700"
36+ style = { { backgroundColor : "var(--ifm-color-primary)" } }
37+ >
38+ < FaArrowUp />
39+ </ button >
40+ ) }
41+ </ div >
3842 ) ;
3943}
0 commit comments