Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/BAR-36] 모달 컴포넌트 개발 #21

Merged
merged 14 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AppProps } from 'next/app';
import '@/src/styles/global.css';

import Layout from '@/src/components/Layout/Layout';
import Modal from '@/src/components/Modal/Modal';
import TanstackQueryProvider from '@/src/components/Providers/TanstackQueryProvider';
import Toast from '@/src/components/Toast/Toast';
import { pretendard } from '@/src/styles/font';
Expand All @@ -14,6 +15,7 @@ const App = ({ Component, pageProps }: AppProps) => {
<Layout>
<Component {...pageProps} />
<Toast />
<Modal />
</Layout>
</TanstackQueryProvider>
</main>
Expand Down
23 changes: 23 additions & 0 deletions src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Meta, StoryObj } from '@storybook/react';

import ModalContainer from './ModalContainer';

const meta: Meta<typeof ModalContainer> = {
title: 'Components/Modal',
component: ModalContainer,
decorators: [
(Story) => (
<div style={{ height: '500px' }}>
<Story />
</div>
),
],
};

export default meta;

type Story = StoryObj<typeof ModalContainer>;

export const Second: Story = {
args: { title: '제목', children: '설명' },
};
13 changes: 13 additions & 0 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useModalStore } from '@/src/stores/modalStore';

import DeleteArticle from './components/DeleteArticle';

const Modal = () => {
const { type } = useModalStore();

if (type === 'deleteArticle') return <DeleteArticle />;

return null;
};

export default Modal;
81 changes: 81 additions & 0 deletions src/components/Modal/ModalContainer.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modal.tsx가 하나의 파일에서 너무 많은 일을 하고 있는것 같습니다 !

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

디자인적 요소 (버튼 등)를 포함하지 않은 모달이 존재하면 더 좋을것 같아요

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { PropsWithChildren } from 'react';
import { assignInlineVars } from '@vanilla-extract/dynamic';

import { useModalStore } from '@/src/stores/modalStore';
import { COLORS } from '@/src/styles/tokens';

import * as styles from './style.css';

interface ModalButtonProps {
text?: string;
backgroundColor?: string;
color?: string;
onClick: () => void;
}

interface ModalContainerProps {
title: string;
firstButtonProps?: ModalButtonProps;
secondButtonProps?: ModalButtonProps;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순 의견; first, second 보다 left, right이 더 명시적으로 보일 것 같습니당

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmswl98
버튼이 1개만 존재하는 경우는 아직 없지만, 그런 형태가 들어온다고 생각해봤을 때는 first, second라는 명명이 좋을 것 같다고 생각했는데 혹시 이 의견에 대해서는 어떻게 생각하시나요!?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first, second는 2개 이상의 버튼을 내포할 수 있다고 생각됩니다 !

보여주신 형태는 ConfirmModal의 형태로 파악되는데 만약 한 개만 필요한 경우를 고려한다면 차라리 confirm, cancel 등의 네이밍은 어떨까용 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first, second는 2개 이상의 버튼을 내포할 수 있다고 생각됩니다 !

보여주신 형태는 ConfirmModal의 형태로 파악되는데 만약 한 개만 필요한 경우를 고려한다면 차라리 confirm, cancel 등의 네이밍은 어떨까용 ?

이 의견도 좋다고 생각합니다!


const ModalContainer = ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 디자인 상으로 Modal 타입이 여러 가지라,

해당 컴포넌트 내부에서 모든 content 요소들을 prop으로 받아서 컴포넌트 내부에서 분기 처리하여 그려내기 보다,
compound pattern으로 설계해 content는 children으로 받아서 그려내는 방식으로 구현되면 다양한 Modal 컴포넌트들에 대응이 쉬울 것 같아요.

제가 생각했던 Modal 컴포넌트의 대략적인 사용 코드는 다음과 같아요.

const LoginModal = () => {
  return (
    <Modal>
      <h1>바로 시작하기</h1>
      <Button>구글 로그인</Button>
      <Button>카카오 로그인</Button>
    </Modal>
  )
}

children,
title,
firstButtonProps,
secondButtonProps,
}: PropsWithChildren<ModalContainerProps>) => {
const { closeModal } = useModalStore();

const {
text: firstButtonText = '취소',
color: firstButtonColor = COLORS['Grey/600'],
backgroundColor: firstButtonBackgroundColor = COLORS['Grey/150'],
onClick: onClickFirstButton = closeModal,
} = firstButtonProps || {};

const {
text: secondButtonText = '만들기',
color: secondButtonColor = COLORS['Grey/White'],
backgroundColor: secondButtonBackgroundColor = COLORS['Blue/Default'],
} = secondButtonProps || {};

return (
<>
<div className={styles.dimmed} />
<div className={styles.modalStyle}>
<strong className={styles.title}>{title}</strong>
{typeof children === 'string' ? (
<p className={styles.body}>{children}</p>
) : (
<>{children}</>
)}
<div>
<button
type="button"
className={styles.button}
style={assignInlineVars({
[styles.buttonColor]: firstButtonColor,
[styles.buttonBackgroundColor]: firstButtonBackgroundColor,
})}
onClick={onClickFirstButton}
>
{firstButtonText}
</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Button 컴포넌트도 따로 공통 컴포넌트로 만들면 좋을 것 같습니다.

<button
type="button"
className={styles.button}
style={assignInlineVars({
[styles.buttonColor]: secondButtonColor,
[styles.buttonBackgroundColor]: secondButtonBackgroundColor,
})}
>
{secondButtonText}
</button>
</div>
</div>
</>
);
};

export default ModalContainer;
17 changes: 17 additions & 0 deletions src/components/Modal/components/DeleteArticle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ModalContainer from '../ModalContainer';

const DeleteArticle = () => {
return (
<ModalContainer
title="해당 글을 삭제할까요?"
secondButtonProps={{
text: '삭제하기',
onClick: () => {},
}}
>
{'삭제한 글은 복구할 수 없어요!\n삭제하시겠어요'}
</ModalContainer>
);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const DeleteArticle = () => {
return (
<ModalContainer
title="해당 글을 삭제할까요?"
secondButtonProps={{
text: '삭제하기',
onClick: () => {},
}}
>
{'삭제한 글은 복구할 수 없어요!\n삭제하시겠어요'}
</ModalContainer>
);
};
const DeleteArticle = () => {
return (
<ModalContainer>
<ModalHeader>
<h1>해당 글을 삭제할까요?</h1>
</ModalHeader>
<ModalContent>
<p>{'삭제한 글은 복구할 수 없어요!\n삭제하시겠어요'}</p>
</ModalContent>
<ModalFooter>
<button>취소</button>
<button>삭제하기</button>
</ModalFooter>
</ModalContainer>
);
};

추상화가 된다면 이렇게 활용할 수 있습니다!


export default DeleteArticle;
32 changes: 0 additions & 32 deletions src/components/Modal/stores/modalStore.ts

This file was deleted.

69 changes: 69 additions & 0 deletions src/components/Modal/style.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { createVar, style } from '@vanilla-extract/css';

import { sprinkles } from '@/src/styles/sprinkles.css';
import { COLORS } from '@/src/styles/tokens';
import { middleLayer, positionCenter, topLayer } from '@/src/styles/utils.css';

export const modalStyle = style([
positionCenter,
topLayer,
{
padding: '32px',
width: '400px',
borderRadius: '16px',
backgroundColor: COLORS['Grey/White'],
},
]);

export const dimmed = style([
middleLayer,
{
backgroundColor: COLORS['Dim/50'],
position: 'fixed',
left: '0',
top: '0',
right: '0',
bottom: '0',
},
]);

export const title = style([
sprinkles({
typography: '20/Title/Semibold',
}),
{
display: 'block',
marginBottom: '18px',
},
]);

export const body = style([
sprinkles({
typography: '15/Body/Regular',
}),
{
display: 'block',
},
]);

export const buttonColor = createVar();
export const buttonBackgroundColor = createVar();

export const button = style([
sprinkles({
typography: '15/Title/Medium',
}),
{
width: '164px',
color: buttonColor,
backgroundColor: buttonBackgroundColor,
padding: '16px 40px',
borderRadius: '8px',
marginTop: '24px',
selectors: {
'& + &': {
marginLeft: '8px',
},
},
},
]);
6 changes: 3 additions & 3 deletions src/components/Toast/Toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { TOAST_DURATION_TIME } from '@/src/models/toastModel';
import { useToast } from '@/src/stores/toastStore';
import { useToastStore } from '@/src/stores/toastStore';

import Toast from './Toast';

Expand All @@ -23,7 +23,7 @@ type Story = StoryObj<typeof Toast>;

export const Basic: Story = {
render: function Render() {
const { showToast } = useToast();
const { showToast } = useToastStore();

useEffect(() => {
showToast({ message: '테스트', type: TOAST_DURATION_TIME.SHOW });
Expand All @@ -42,7 +42,7 @@ export const Basic: Story = {

export const WithAction: Story = {
render: function Render() {
const { showToast } = useToast();
const { showToast } = useToastStore();

useEffect(() => {
showToast({ message: '테스트', type: TOAST_DURATION_TIME.ACTION });
Expand Down
6 changes: 3 additions & 3 deletions src/components/Toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useEffect } from 'react';

import { useToast } from '@/src/stores/toastStore';
import { useToastStore } from '@/src/stores/toastStore';

import * as styles from './style.css';

const Toast = () => {
const { toastData, hideToast } = useToast();
const { toastData, hideToast } = useToastStore();

const { message, type, isToastVisible } = toastData;

useEffect(() => {
let timer;
let timer: ReturnType<typeof setTimeout>;

if (isToastVisible) {
timer = setTimeout(() => hideToast(), type);
Expand Down
6 changes: 3 additions & 3 deletions src/components/Toast/style.css.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { recipe } from '@vanilla-extract/recipes';

import { sprinkles } from '@/src/styles/sprinkles.css';
import { theme } from '@/src/styles/theme.css';
import { COLORS } from '@/src/styles/tokens';
import { modalLayer } from '@/src/styles/utils.css';

export const toast = recipe({
Expand All @@ -11,9 +11,9 @@ export const toast = recipe({
typography: '14/Body/Regular',
}),
{
backgroundColor: theme.colors['Dim/70'],
backgroundColor: COLORS['Dim/70'],
position: 'fixed',
color: theme.colors['Grey/White'],
color: COLORS['Grey/White'],
bottom: 0,
left: '50%',
transform: 'translateX(-50%) translateY(30px)',
Expand Down
31 changes: 31 additions & 0 deletions src/stores/modalStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createStore } from 'zustand';
import { shallow } from 'zustand/shallow';
import { useStoreWithEqualityFn as useStore } from 'zustand/traditional';

type ModalType = 'no' | 'deleteArticle';

interface State {
type: ModalType;
}

interface Action {
openModal: (type: ModalType) => void;
closeModal: () => void;
}

export const modalStore = createStore<State & Action>((set) => ({
type: 'no',
openModal: (type) => set({ type }),
closeModal: () => set({ type: 'no' }),
}));

export const useModalStore = () =>
useStore(
modalStore,
(state) => ({
type: state.type,
openModal: state.openModal,
closeModal: state.closeModal,
}),
shallow,
);
10 changes: 3 additions & 7 deletions src/stores/toastStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ export const toastStore = createStore<State & Action>((set, get) => ({
},
}));

const useToastStore = <T>(
selector: (state: State & Action) => T,
equals?: (a: T, b: T) => boolean,
) => useStore(toastStore, selector, equals);

export const useToast = () =>
useToastStore(
export const useToastStore = () =>
useStore(
toastStore,
(state) => ({
toastData: state.toastData,
showToast: state.showToast,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"strict": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
Expand Down