Skip to content

Commit

Permalink
Merge pull request #54 from themoment-team/develop
Browse files Browse the repository at this point in the history
Release v1.2.1
  • Loading branch information
frorong committed Mar 5, 2024
2 parents c3fdc00 + 8a3639a commit ba2be1b
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 92 deletions.
8 changes: 3 additions & 5 deletions src/components/MainContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import { Banner, TabletSlide, PCSlide, MobileSlide } from 'components';
import { useWidthState } from 'stores';
import { WindowSize } from 'utils/windowSize';

import * as S from './style';

const TABLET_SIZE = 1280;
const MOBILE_SIZE = 620;

const MainContainer = () => {
const { width } = useWidthState();

const getSlideByWidth = () => {
if (width > TABLET_SIZE) {
if (width > WindowSize.TABLET_SIZE) {
return <PCSlide />;
} else if (width > MOBILE_SIZE) {
} else if (width > WindowSize.MOBILE_SIZE) {
return <TabletSlide />;
} else {
return <MobileSlide />;
Expand Down
8 changes: 7 additions & 1 deletion src/components/Nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const Nav = () => {
};

useEffect(() => {
setIsDark(localStorage.getItem('dark') === 'false');
const initialIsDark = window.matchMedia(
'(prefers-color-scheme: dark)',
).matches;
const localDark = localStorage.getItem('dark');

if (localDark) setIsDark(localStorage.getItem('dark') === 'false');
else setIsDark(initialIsDark);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
46 changes: 17 additions & 29 deletions src/components/Slide/PC/index.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
'use client';

import { useState } from 'react';

import { Vector } from 'assets';
import { Card } from 'components';
import { useFilterProjects } from 'hooks';
import { Card, SlideController } from 'components';
import { useFilterProjects, useHandleSlide } from 'hooks';
import { Device } from 'utils';
import { Direction } from 'utils';

import * as S from './style';

const CARDS_PER_PAGE = 3 as const;

const PC = () => {
const [slideIndex, setSlideIndex] = useState<number>(0);

const projects = useFilterProjects({ setSlideIndex });
const projects = useFilterProjects();

const maxIndex = Math.ceil(projects.length / CARDS_PER_PAGE) - 1;

const handlePrevSlide = () => {
if (slideIndex === 0) {
setSlideIndex(maxIndex);
} else {
setSlideIndex(curIndex => curIndex - 1);
}
};

const handleNextSlide = () => {
if (maxIndex === slideIndex) {
setSlideIndex(0);
} else {
setSlideIndex(curIndex => curIndex + 1);
}
};
const { slideIndex, handleNextSlide, handlePrevSlide } =
useHandleSlide(maxIndex);

return (
<S.CardContainer>
<S.PrevController type="button" onClick={handlePrevSlide}>
<Vector />
</S.PrevController>
<SlideController
view={Device.PC}
onClick={handlePrevSlide}
direction={Direction.LEFT}
/>
<S.Cards>
<S.MoveContainer slideIndex={slideIndex}>
{projects.map((data, slideIndex) => (
<Card key={slideIndex + data.id} data={data} />
))}
</S.MoveContainer>
</S.Cards>
<S.NextController type="button" onClick={handleNextSlide}>
<Vector />
</S.NextController>
<SlideController
view={Device.PC}
onClick={handleNextSlide}
direction={Direction.RIGHT}
/>
</S.CardContainer>
);
};
Expand Down
46 changes: 17 additions & 29 deletions src/components/Slide/Tablet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
'use client';

import { useState } from 'react';

import { Vector } from 'assets';
import { Card } from 'components';
import { useFilterProjects } from 'hooks';
import { Card, SlideController } from 'components';
import { useFilterProjects, useHandleSlide } from 'hooks';
import { Device } from 'utils';
import { Direction } from 'utils';

import * as S from './style';

const CARDS_PER_PAGE = 4 as const;

const Tablet = () => {
const [slideIndex, setSlideIndex] = useState<number>(0);

const projects = useFilterProjects({ setSlideIndex });
const projects = useFilterProjects();

const maxIndex = Math.ceil(projects.length / CARDS_PER_PAGE) - 1;

const handlePrevSlide = () => {
if (slideIndex === 0) {
setSlideIndex(maxIndex);
} else {
setSlideIndex(curIndex => curIndex - 1);
}
};

const handleNextSlide = () => {
if (maxIndex === slideIndex) {
setSlideIndex(0);
} else {
setSlideIndex(curIndex => curIndex + 1);
}
};
const { slideIndex, handleNextSlide, handlePrevSlide } =
useHandleSlide(maxIndex);

return (
<S.CardContainer>
<S.PrevController type="button" onClick={handlePrevSlide}>
<Vector />
</S.PrevController>
<SlideController
view={Device.TABLET}
onClick={handlePrevSlide}
direction={Direction.LEFT}
/>
<S.Cards>
<S.Slider slideIndex={slideIndex}>
<S.MoveContainer maxIndex={maxIndex}>
Expand All @@ -47,9 +33,11 @@ const Tablet = () => {
</S.MoveContainer>
</S.Slider>
</S.Cards>
<S.NextController type="button" onClick={handleNextSlide}>
<Vector />
</S.NextController>
<SlideController
view={Device.TABLET}
onClick={handleNextSlide}
direction={Direction.RIGHT}
/>
</S.CardContainer>
);
};
Expand Down
20 changes: 0 additions & 20 deletions src/components/Slide/Tablet/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,3 @@ export const MoveContainer = styled.div<{ maxIndex: number }>`
grid-template-rows: repeat(2, 1fr);
gap: 1.75rem;
`;

export const Controller = styled.button`
border: none;
z-index: 1;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
background-color: ${({ theme }) => theme.gray[0]};
`;

export const PrevController = styled(Controller)`
margin-right: 1.875rem;
`;

export const NextController = styled(Controller)`
margin-left: 1.875rem;
svg {
transform: matrix(-1, 0, 0, 1, 0, 0);
}
`;
42 changes: 42 additions & 0 deletions src/components/SlideController/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Device } from 'utils';
import { Direction } from 'utils';

import SlideController from '.';

import type { Meta, StoryObj } from '@storybook/react';

export default {
title: 'SlideController',
component: SlideController,
parameters: {
backgrounds: {
default: 'dark',
},
},
} as Meta<typeof SlideController>;

type Story = StoryObj<typeof SlideController>;

export const Left: Story = {
args: {
direction: Direction.LEFT,
onClick: () => {},
view: Device.TABLET,
},
};

export const Right: Story = {
args: {
direction: Direction.RIGHT,
onClick: () => {},
view: Device.TABLET,
},
};

export const PC: Story = {
args: {
direction: Direction.RIGHT,
onClick: () => {},
view: Device.PC,
},
};
31 changes: 31 additions & 0 deletions src/components/SlideController/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import { Vector } from 'assets';

import * as S from './style';

import type { Direction } from 'utils';
import type { Device } from 'utils';

interface Props {
direction: Direction;
onClick: () => void;
view: Device;
}

const ControllerType = {
left: S.PrevController,
right: S.NextController,
} as const;

const SlideController: React.FC<Props> = ({ direction, onClick, view }) => {
const Controller = ControllerType[direction];

return (
<Controller type="button" onClick={onClick} view={view}>
<Vector />
</Controller>
);
};

export default SlideController;
27 changes: 27 additions & 0 deletions src/components/SlideController/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import styled from '@emotion/styled';
import type { Device } from 'utils';

enum Margin {
PC = '3.125rem',
TABLET = '1.875rem',
}

export const Controller = styled.button<{ view: Device }>`
border: none;
z-index: 1;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
background-color: ${({ theme }) => theme.gray[0]};
`;

export const PrevController = styled(Controller)`
margin-right: ${({ view }) => Margin[view]};
`;

export const NextController = styled(Controller)`
margin-left: ${({ view }) => Margin[view]};
svg {
transform: matrix(-1, 0, 0, 1, 0, 0);
}
`;
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export { default as Nav } from './Nav';
export { default as NavigationEvents } from './NavigationEvents';
export { default as PCSlide } from './Slide/PC';
export { default as SearchBar } from './SearchBar';
export { default as SlideController } from './SlideController';
export { default as TabletSlide } from './Slide/Tablet';
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as useFilterProjects } from './useFilterProjects';
export { default as useHandleSlide } from './useHandleSlide';
export { default as useWindowResizeEffect } from './useWindowResizeEffect';
9 changes: 1 addition & 8 deletions src/hooks/useFilterProjects.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
'use client';

import type { Dispatch, SetStateAction } from 'react';
import { useEffect, useState } from 'react';

import project from 'constants/project.json';
import { useSearchState } from 'stores';

interface Props {
setSlideIndex: Dispatch<SetStateAction<number>>;
}

const useFilterProjects = ({ setSlideIndex }: Props) => {
const useFilterProjects = () => {
const { searchKeyword } = useSearchState();

const [filteredProjects, setFilteredProjects] = useState(project);
Expand All @@ -30,8 +25,6 @@ const useFilterProjects = ({ setSlideIndex }: Props) => {
);
}),
);

setSlideIndex(0);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchKeyword]);

Expand Down
30 changes: 30 additions & 0 deletions src/hooks/useHandleSlide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client';

import { useState } from 'react';

const useHandleSlide = (maxIndex: number) => {
const [slideIndex, setSlideIndex] = useState<number>(0);

const increaseIndex = (index: number) => index + 1;
const decreaseIndex = (index: number) => index - 1;

const handlePrevSlide = () => {
setSlideIndex(curIndex =>
curIndex === 0 ? maxIndex : decreaseIndex(curIndex),
);
};

const handleNextSlide = () => {
setSlideIndex(curIndex =>
curIndex === maxIndex ? 0 : increaseIndex(curIndex),
);
};

return {
slideIndex,
handlePrevSlide,
handleNextSlide,
};
};

export default useHandleSlide;
4 changes: 4 additions & 0 deletions src/utils/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Device {
PC = 'PC',
TABLET = 'TABLET',
}
4 changes: 4 additions & 0 deletions src/utils/direction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Direction {
LEFT = 'left',
RIGHT = 'right',
}
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './device';
export * from './direction';
export * from './windowSize';
4 changes: 4 additions & 0 deletions src/utils/windowSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum WindowSize {
TABLET_SIZE = 1280,
MOBILE_SIZE = 620,
}

0 comments on commit ba2be1b

Please sign in to comment.