Skip to content

Commit 80673d0

Browse files
authored
Merge pull request #32 from codeitFE11-part3-team7/feature/#15_SnackBar-컴포넌트-제작
#15 snack bar 컴포넌트 제작
2 parents bd78baa + b30a546 commit 80673d0

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

components/SnackBar.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
interface SnackBarProps {
2+
state: 'fail' | 'success' | 'info' | 'null';
3+
}
4+
5+
// state props styles object
6+
const stateConfig = {
7+
fail: {
8+
style:
9+
'bg-red-50 border-red-100 fixed left-1/2 transform -translate-x-1/2 top-[120px] mx-auto mo:bottom-[80px] mo:top-auto w-[384px] mo:w-[328px] whitespace-nowrap',
10+
text: '다른 친구가 편집하고 있어요. 나중에 다시 시도해 주세요.',
11+
icon: '/icon/icon-fail.svg',
12+
textStyle: 'text-red-100 text-14sb mo:text-12sb',
13+
},
14+
success: {
15+
style:
16+
'bg-green-100 border-green-200 fixed left-1/2 transform -translate-x-1/2 top-[120px] mx-auto mo:bottom-[80px] mo:top-auto w-[247px] mo:w-[210px] whitespace-nowrap',
17+
text: '내 위키 링크가 복사되었습니다.',
18+
icon: '/icon/icon-success.svg',
19+
textStyle: 'text-green-300 text-14sb mo:text-12sb',
20+
},
21+
info: {
22+
style: 'bg-background border-white',
23+
text: '앞 사람의 편집이 끝나면 위키 참여가 가능합니다.',
24+
icon: '/icon/icon-info.svg',
25+
textStyle: 'text-gray-500 text-14 mo:text-12',
26+
},
27+
null: {
28+
style: 'hidden',
29+
text: '',
30+
icon: '',
31+
textStyle: '',
32+
},
33+
};
34+
35+
/**
36+
* SnackBar 컴포넌트
37+
* @param {SnackBarProps} { state } - state: 'fail' | 'success' | 'info' | 'null'
38+
*/
39+
export default function SnackBar({ state }: SnackBarProps) {
40+
const { style, text, icon, textStyle } = stateConfig[state];
41+
42+
return (
43+
<div
44+
className={`rounded-custom ${style} animate-fadeIn flex items-center gap-[15px] border px-5 py-[11px] shadow-custom mo:px-[15px] mo:py-[11px]`}
45+
>
46+
{icon && <img src={icon} alt="snackbar icon" className="h-5 w-5" />}
47+
<p className={`${textStyle} break-words mo:break-words`}>{text}</p>
48+
</div>
49+
);
50+
}

pages/test/index.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1+
import { useState } from 'react';
2+
13
import Button from '@/components/Button';
24
import LinkBar from '@/components/LinkBar';
5+
import SnackBar from '@/components/SnackBar';
36

47
export default function Test() {
58
const commonCellClass = 'border-r border-gray-300';
69
const commonRowClass = 'flex flex-wrap items-end gap-2';
710

11+
// ----snackBar(start)----
12+
const [snackState, setSnackState] = useState<
13+
'fail' | 'success' | 'info' | 'null'
14+
>('null');
15+
16+
const handleSuccess = () => {
17+
setSnackState('success');
18+
setTimeout(() => setSnackState('null'), 1500);
19+
};
20+
21+
const handleFail = () => {
22+
setSnackState('fail');
23+
setTimeout(() => setSnackState('null'), 1500);
24+
};
25+
// ----snackBar(end)----
26+
827
return (
928
<div className="px-4 py-10">
1029
<table className="w-full border-collapse border border-gray-300">
@@ -33,7 +52,16 @@ export default function Test() {
3352
</Button>
3453
</td>
3554
</tr>
36-
<tr>
55+
<tr className="border-b border-gray-300">
56+
<td className={commonCellClass}>SnackBar</td>
57+
<td className={commonRowClass}>
58+
<SnackBar state="info" />
59+
<Button onClick={handleSuccess}>복사</Button>
60+
<Button onClick={handleFail}>에러</Button>
61+
{snackState !== 'null' && <SnackBar state={snackState} />}
62+
</td>
63+
</tr>
64+
<tr className="border-b border-gray-300">
3765
<td className={commonCellClass}>LinkBar</td>
3866
<td className={commonRowClass}>
3967
<LinkBar link="https://www.google.com" />

public/icon/icon-fail.svg

Lines changed: 3 additions & 0 deletions
Loading

public/icon/icon-info.svg

Lines changed: 3 additions & 0 deletions
Loading

public/icon/icon-success.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)