Skip to content

Commit 3a535f0

Browse files
committed
Scroll to top desing and implementation
1 parent 46fca8b commit 3a535f0

File tree

4 files changed

+77
-27
lines changed

4 files changed

+77
-27
lines changed

src/App.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Home from './pages/Home';
99
import Projects from './pages/Projects';
1010
import ProjectSingle from './pages/ProjectSingle';
1111
import { AnimatePresence } from 'framer-motion';
12+
import UseScrollToTop from './hooks/useScrollToTop';
1213

1314
function App() {
1415
return (
@@ -30,6 +31,7 @@ function App() {
3031
</Routes>
3132
<AppFooter />
3233
</Router>
34+
<UseScrollToTop />
3335
</div>
3436
</AnimatePresence>
3537
);

src/components/ScrollToTop.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
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+
14
import { useEffect } from 'react';
25
import { useLocation } from 'react-router-dom';
36

src/css/App.css

+50-21
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,67 @@
11
.App {
2-
text-align: center;
2+
text-align: center;
33
}
44

55
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
6+
height: 40vmin;
7+
pointer-events: none;
88
}
99

1010
@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+
}
1414
}
1515

1616
.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;
2525
}
2626

2727
.App-link {
28-
color: #61dafb;
28+
color: #61dafb;
2929
}
3030

3131
@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+
}
3867
}

src/hooks/useScrollToTop.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
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';
35

46
const useScrollToTop = () => {
57
const [showScroll, setShowScroll] = useState(false);
68

9+
useEffect(() => {
10+
window.addEventListener('scroll', scrollToTop);
11+
return function cleanup() {
12+
window.removeEventListener('scroll', scrollToTop);
13+
};
14+
});
15+
716
const scrollToTop = () => {
8-
if (!showScroll & (window.pageYOffset > 500)) {
17+
if (!showScroll && window.pageYOffset > 400) {
918
setShowScroll(true);
10-
} else if (showScroll && window.pageYOffset <= 500) {
19+
} else if (showScroll && window.pageYOffset <= 400) {
1120
setShowScroll(false);
1221
}
1322
};
@@ -23,10 +32,17 @@ const useScrollToTop = () => {
2332

2433
return (
2534
<>
26-
<FIChevronUp
35+
<FiChevronUp
2736
className="scrollToTop"
2837
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+
}}
3046
/>
3147
</>
3248
);

0 commit comments

Comments
 (0)