Skip to content

Commit

Permalink
feat: window size 체킹하는 방법 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
99-zziy committed Sep 23, 2023
1 parent 37eb58e commit 9a8f76f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/hooks/useCheckWindowSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from 'react';

import { SIZE, SizeKey } from '~/styles/media';

type UseCheckWindowSizeProps = SizeKey;

export const useCheckWindowSize = (size: UseCheckWindowSizeProps) => {
const [isTargetSize, setIsTargetSize] = useState(false);

const checkWindowSize = () => {
setIsTargetSize(window.innerWidth <= SIZE[size]);
};

useEffect(() => {
checkWindowSize();
window.addEventListener('resize', checkWindowSize);
return () => {
window.removeEventListener('resize', checkWindowSize);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return {
isTargetSize,
};
};
4 changes: 3 additions & 1 deletion src/pages/project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Thumbnail } from '~/components/Thumbnail';
import { Link } from '~/components/Thumbnail/Thumbnail';
import { staggerHalf } from '~/constant/motion';
import { PROJECT_LIST } from '~/constant/project';
import { useCheckWindowSize } from '~/hooks/useCheckWindowSize';
import { mediaQuery } from '~/styles/media';
import { getCurrentProjects, getTenUnderProjects, sliceByPage } from '~/utils/pagination';

Expand All @@ -20,6 +21,7 @@ export default function ProjectPage() {
const [currentTab, setCurrentTab] = useState(ALL_TAB);
const [selectedProjectList, setSelectedProjectList] = useState(PROJECT_LIST);
const [currentPage, setCurrentPage] = useState(FIRST_PAGE);
const { isTargetSize: isTabletSize } = useCheckWindowSize('tablet');

useEffect(() => {
setCurrentPage(1);
Expand Down Expand Up @@ -53,7 +55,7 @@ export default function ProjectPage() {
exit="exit"
variants={staggerHalf}
>
{sliceByPage(selectedProjectList, currentPage).map(project => (
{sliceByPage(selectedProjectList, currentPage, isTabletSize).map(project => (
<Thumbnail
key={project.title}
img={`/images/project/${project.subTitle}/${project.title}.png`}
Expand Down
10 changes: 4 additions & 6 deletions src/utils/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Project } from '~/constant/project';
import { SIZE } from '~/styles/media';

export const sliceByPage = (projects: Project[], page: number) => {
if (typeof window !== 'undefined') {
if (window.innerWidth <= SIZE.tablet) {
return projects.slice(6 * (page - 1), 6 * page);
}
export const sliceByPage = (projects: Project[], page: number, isTabletSize: boolean) => {
if (isTabletSize) {
return projects.slice(6 * (page - 1), 6 * page);
}

return projects.slice(9 * (page - 1), 9 * page);
};

Expand Down

0 comments on commit 9a8f76f

Please sign in to comment.