File tree 4 files changed +77
-27
lines changed
4 files changed +77
-27
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import Home from './pages/Home';
9
9
import Projects from './pages/Projects' ;
10
10
import ProjectSingle from './pages/ProjectSingle' ;
11
11
import { AnimatePresence } from 'framer-motion' ;
12
+ import UseScrollToTop from './hooks/useScrollToTop' ;
12
13
13
14
function App ( ) {
14
15
return (
@@ -30,6 +31,7 @@ function App() {
30
31
</ Routes >
31
32
< AppFooter />
32
33
</ Router >
34
+ < UseScrollToTop />
33
35
</ div >
34
36
</ AnimatePresence >
35
37
) ;
Original file line number Diff line number Diff line change
1
+ // NOTE: This scroll to top is the default react scroll to top behavior when visiting a new route.
2
+ // For the scroll to top behavior on a click event I have create a custom hook with the name of useScrollToTop under the hooks folder.
3
+
1
4
import { useEffect } from 'react' ;
2
5
import { useLocation } from 'react-router-dom' ;
3
6
Original file line number Diff line number Diff line change 1
1
.App {
2
- text-align : center;
2
+ text-align : center;
3
3
}
4
4
5
5
.App-logo {
6
- height : 40vmin ;
7
- pointer-events : none;
6
+ height : 40vmin ;
7
+ pointer-events : none;
8
8
}
9
9
10
10
@media (prefers-reduced-motion : no-preference) {
11
- .App-logo {
12
- animation : App-logo-spin infinite 20s linear;
13
- }
11
+ .App-logo {
12
+ animation : App-logo-spin infinite 20s linear;
13
+ }
14
14
}
15
15
16
16
.App-header {
17
- background-color : # 282c34 ;
18
- min-height : 100vh ;
19
- display : flex;
20
- flex-direction : column;
21
- align-items : center;
22
- justify-content : center;
23
- font-size : calc (10px + 2vmin );
24
- color : white;
17
+ background-color : # 282c34 ;
18
+ min-height : 100vh ;
19
+ display : flex;
20
+ flex-direction : column;
21
+ align-items : center;
22
+ justify-content : center;
23
+ font-size : calc (10px + 2vmin );
24
+ color : white;
25
25
}
26
26
27
27
.App-link {
28
- color : # 61dafb ;
28
+ color : # 61dafb ;
29
29
}
30
30
31
31
@keyframes App-logo-spin {
32
- from {
33
- transform : rotate (0deg );
34
- }
35
- to {
36
- transform : rotate (360deg );
37
- }
32
+ from {
33
+ transform : rotate (0deg );
34
+ }
35
+ to {
36
+ transform : rotate (360deg );
37
+ }
38
+ }
39
+
40
+ /* Scroll to top style */
41
+ .scrollToTop {
42
+ @apply bg-indigo-600;
43
+ @apply text-white;
44
+ position : fixed;
45
+ width : 100% ;
46
+ align-items : center;
47
+ height : 20px ;
48
+ justify-content : center;
49
+ z-index : 999 ;
50
+ cursor : pointer;
51
+ animation : fadeIn 0.3s ;
52
+ transition : opacity 0.4s ;
53
+ opacity : 0.5 ;
54
+ }
55
+
56
+ .scrollToTop : hover {
57
+ opacity : 1 ;
58
+ }
59
+
60
+ @keyframes fadeIn {
61
+ 0% {
62
+ opacity : 0 ;
63
+ }
64
+ 100% {
65
+ opacity : 0.5 ;
66
+ }
38
67
}
Original file line number Diff line number Diff line change 1
- import { useState } from 'react' ;
2
- import { FIChevronUp } from 'react-icons/fi' ;
1
+ // NOTE: This scroll to top is the actual working scroll to to when user clicks
2
+
3
+ import { useState , useEffect } from 'react' ;
4
+ import { FiChevronUp } from 'react-icons/fi' ;
3
5
4
6
const useScrollToTop = ( ) => {
5
7
const [ showScroll , setShowScroll ] = useState ( false ) ;
6
8
9
+ useEffect ( ( ) => {
10
+ window . addEventListener ( 'scroll' , scrollToTop ) ;
11
+ return function cleanup ( ) {
12
+ window . removeEventListener ( 'scroll' , scrollToTop ) ;
13
+ } ;
14
+ } ) ;
15
+
7
16
const scrollToTop = ( ) => {
8
- if ( ! showScroll & ( window . pageYOffset > 500 ) ) {
17
+ if ( ! showScroll && window . pageYOffset > 400 ) {
9
18
setShowScroll ( true ) ;
10
- } else if ( showScroll && window . pageYOffset <= 500 ) {
19
+ } else if ( showScroll && window . pageYOffset <= 400 ) {
11
20
setShowScroll ( false ) ;
12
21
}
13
22
} ;
@@ -23,10 +32,17 @@ const useScrollToTop = () => {
23
32
24
33
return (
25
34
< >
26
- < FIChevronUp
35
+ < FiChevronUp
27
36
className = "scrollToTop"
28
37
onClick = { backToTop }
29
- style = { { height : 40 , display : showScroll ? 'flex' : 'none' } }
38
+ style = { {
39
+ height : 45 ,
40
+ width : 45 ,
41
+ borderRadius : 50 ,
42
+ right : 50 ,
43
+ bottom : 50 ,
44
+ display : showScroll ? 'flex' : 'none' ,
45
+ } }
30
46
/>
31
47
</ >
32
48
) ;
You can’t perform that action at this time.
0 commit comments