Skip to content

Commit 565d533

Browse files
authored
Merge pull request realstoman#8 from NangialaiStoman/router-and-scroll-top
Router and scroll top
2 parents 350e299 + 9a7cd18 commit 565d533

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import About from './pages/AboutMe';
77
import Projects from './pages/Projects';
88
import Contact from './pages/Contact';
99
import ProjectSingle from './pages/ProjectSingle';
10+
import ScrollToTop from './components/ScrollToTop';
1011

1112
function App() {
1213
return (
1314
<div className=" bg-secondary-light dark:bg-primary-dark transition duration-300">
1415
<Router>
16+
<ScrollToTop />
1517
<AppHeader></AppHeader>
1618
<Routes>
1719
<Route path="/" element={<Home />} />

src/components/BackToTop.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect } from 'react';
2+
3+
const BackToTop = () => {
4+
const userScrollPosition = 0;
5+
6+
useEffect(() => {
7+
window.scrollTo(0, 0);
8+
}, [userScrollPosition]);
9+
10+
return <div></div>;
11+
};
12+
13+
export default BackToTop;

src/components/ProjectsGrid.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import ProjectsFilter from './ProjectsFilter';
44
import ProjectSingle from './ProjectSingle';
55

66
const ProjectsGrid = () => {
7+
const selectedProject = '';
8+
const searchProject = '';
9+
10+
const filteredProjects = () => {
11+
if (selectedProject) {
12+
return filterProjectsByCategory();
13+
} else if (searchProject) {
14+
return filterProjectsBySearch();
15+
}
16+
return ProjectsData;
17+
};
18+
19+
const filterProjectsByCategory = () => {
20+
return ProjectsData.filter((item) => {
21+
let category =
22+
item.category.charAt(0).toUpperCase() + item.category.slice(1);
23+
return category.includes(selectedProject);
24+
});
25+
};
26+
27+
const filterProjectsBySearch = () => {
28+
let project = new RegExp(searchProject, 'i');
29+
return ProjectsData.filter((el) => el.title.match(project));
30+
};
31+
732
return (
833
<section className="py-5 sm:py-10 mt-5 sm:mt-10">
934
{/* Projects grid title start */}
@@ -92,11 +117,6 @@ const ProjectsGrid = () => {
92117

93118
{/* Projects grid start */}
94119
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 mt-6 sm:gap-10">
95-
{/* <ProjectSingle
96-
v-for="project in filteredProjects"
97-
:key="project.id"
98-
:project="project"
99-
/> */}
100120
{ProjectsData.map((project) => (
101121
<ProjectSingle
102122
title={project.title}

src/components/ScrollToTop.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
const ScrollToTop = () => {
5+
const { pathname } = useLocation();
6+
7+
useEffect(() => {
8+
window.scrollTo(0, 0);
9+
}, [pathname]);
10+
return null;
11+
};
12+
13+
export default ScrollToTop;

0 commit comments

Comments
 (0)