1- // Import External Dependencies
21import { useEffect , useState } from "react" ;
32
4- // Load Styling
5- import "./ScrollToTop.scss" ;
6-
73function ScrollToTop ( ) {
84 const [ isVisible , setIsVisible ] = useState ( false ) ;
95
106 /**
117 * Show button when page is scrolled down
128 */
139 const toggleVisibility = ( ) => {
14- if ( window . scrollY > 300 ) {
15- setIsVisible ( true ) ;
16- } else {
17- setIsVisible ( false ) ;
18- }
10+ // Cleaned up if/else redundancy
11+ setIsVisible ( window . scrollY > 300 ) ;
1912 } ;
2013
2114 /**
@@ -30,36 +23,45 @@ function ScrollToTop() {
3023
3124 useEffect ( ( ) => {
3225 window . addEventListener ( "scroll" , toggleVisibility ) ;
33-
34- return ( ) => {
35- window . removeEventListener ( "scroll" , toggleVisibility ) ;
36- } ;
26+ return ( ) => window . removeEventListener ( "scroll" , toggleVisibility ) ;
3727 } , [ ] ) ;
3828
3929 return (
40- < div className = "scroll-to-top" >
41- { isVisible && (
42- < button
43- className = "scroll-to-top__button"
44- onClick = { scrollToTop }
45- aria-label = "Scroll to top"
46- title = "Scroll to top"
30+ < div className = "fixed bottom-4 right-4 md:bottom-8 md:right-8 z-[999] pointer-events-none" >
31+ < button
32+ className = { `
33+ group flex items-center justify-center
34+ w-12 h-12 rounded-full cursor-pointer
35+ bg-white border-[5px] border-[#1c78c0]
36+ shadow-[0_4px_12px_rgba(0,0,0,0.15)]
37+ transition-all duration-300 ease-in-out
38+ hover:bg-[#1c78c0] hover:border-[#f5f5f5] hover:shadow-[0_6px_16px_rgba(0,0,0,0.2)] hover:-translate-y-0.5
39+ active:translate-y-0
40+ dark:bg-[#f5f5f5]
41+ motion-reduce:transition-none motion-reduce:hover:transform-none
42+ ${
43+ isVisible
44+ ? "opacity-100 translate-y-0 pointer-events-auto"
45+ : "opacity-0 translate-y-2.5 pointer-events-none"
46+ }
47+ ` }
48+ onClick = { scrollToTop }
49+ aria-label = "Scroll to top"
50+ title = "Scroll to top"
51+ disabled = { ! isVisible }
52+ >
53+ < svg
54+ className = "w-8 h-8 text-slate-800 transition-colors duration-300 ease-in-out group-hover:text-white"
55+ viewBox = "0 0 24 24"
56+ fill = "none"
57+ stroke = "currentColor"
58+ strokeWidth = "2"
59+ strokeLinecap = "round"
60+ strokeLinejoin = "round"
4761 >
48- < svg
49- className = "scroll-to-top__icon"
50- width = "24"
51- height = "24"
52- viewBox = "0 0 24 24"
53- fill = "none"
54- stroke = "currentColor"
55- strokeWidth = "2"
56- strokeLinecap = "round"
57- strokeLinejoin = "round"
58- >
59- < polyline points = "18 15 12 9 6 15" > </ polyline >
60- </ svg >
61- </ button >
62- ) }
62+ < polyline points = "18 15 12 9 6 15" > </ polyline >
63+ </ svg >
64+ </ button >
6365 </ div >
6466 ) ;
6567}
0 commit comments