Skip to content

Commit 7d40efc

Browse files
committed
feat: add top-center position + set variant as optional & improve global toast component
1 parent ecab24d commit 7d40efc

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

library/src/components/toast.tsx

+13-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ToastComponentProps extends ToastProps {
1919
}
2020

2121
const Toast = (props: ToastComponentProps) => {
22-
const IconComponent = icons[props.variant];
22+
const IconComponent = props.variant ? icons[props.variant] : Info;
2323
const [isExiting, setIsExiting] = useState<boolean>(false);
2424

2525
const delayDuration = props.delayDuration || 4000;
@@ -48,6 +48,7 @@ const Toast = (props: ToastComponentProps) => {
4848
const ANIMATION_ENTER_MAP: Record<Position, string> = {
4949
'top-left': 't_slide-top',
5050
'top-right': 't_slide-top',
51+
'top-center': 't_slide-top',
5152
'bottom-left': 't_slide-bottom',
5253
'bottom-right': 't_slide-bottom',
5354
'bottom-center': 't_slide-bottom',
@@ -56,6 +57,7 @@ const Toast = (props: ToastComponentProps) => {
5657
const ANIMATION_EXIT_MAP: Record<Position, string> = {
5758
'top-left': 't_slide-left',
5859
'top-right': 't_slide-right',
60+
'top-center': 't_slide-top-exit',
5961
'bottom-left': 't_slide-left',
6062
'bottom-right': 't_slide-right',
6163
'bottom-center': 't_slide-bottom-exit',
@@ -79,15 +81,16 @@ const Toast = (props: ToastComponentProps) => {
7981
onMouseLeave={handleMouseLeave}
8082
>
8183
<div className="t_container">
82-
{props.icon ? (
83-
props.icon
84-
) : (
85-
<IconComponent
86-
width={props.iconSize || 18}
87-
height={props.iconSize || 18}
88-
fill="currentColor"
89-
/>
90-
)}
84+
{props.variant &&
85+
(props.icon ? (
86+
<div className="t_icon">{props.icon}</div>
87+
) : (
88+
<IconComponent
89+
width={props.iconSize || 18}
90+
height={props.iconSize || 18}
91+
className="t_icon"
92+
/>
93+
))}
9194
<div className="t_content">
9295
<p>{props.text}</p>
9396
{props.description && <p>{props.description}</p>}

library/src/providers/toast-provider.tsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,25 @@ export const ToastProvider = ({
2626
};
2727
setToasts((prevToasts) => {
2828
if (prevToasts.length >= maxToasts) {
29-
return [newToast, ...prevToasts.slice(0, prevToasts.length - 1)];
29+
if (
30+
position === 'top-left' ||
31+
position === 'top-right' ||
32+
position === 'top-center'
33+
) {
34+
return [newToast, ...prevToasts.slice(0, prevToasts.length - 1)];
35+
} else {
36+
return [...prevToasts.slice(1), newToast];
37+
}
3038
} else {
31-
return [newToast, ...prevToasts];
39+
if (
40+
position === 'top-left' ||
41+
position === 'top-right' ||
42+
position === 'top-center'
43+
) {
44+
return [newToast, ...prevToasts];
45+
} else {
46+
return [...prevToasts, newToast];
47+
}
3248
}
3349
});
3450
};
@@ -54,6 +70,7 @@ export const ToastProvider = ({
5470
't_toasts',
5571
position === 'top-left' ? 't_top-left' : '',
5672
position === 'top-right' ? 't_top-right' : '',
73+
position === 'top-center' ? 't_top-center' : '',
5774
position === 'bottom-left' ? 't_bottom-left' : '',
5875
position === 'bottom-right' ? 't_bottom-right' : '',
5976
position === 'bottom-center' ? 't_bottom-center' : '',

library/src/styles/toast-component.css

+31-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
--background-color: #ffff;
66
--hover-bg-color: #f5f5f5;
77
--border-color: #e5e5e5;
8-
--title-color: #171717;
8+
--text-color: #171717;
99
--description-color: #262626;
1010
}
1111

@@ -14,7 +14,7 @@
1414
--background-color: #ffff;
1515
--hover-bg-color: #f5f5f5;
1616
--border-color: #e5e5e5;
17-
--title-color: #171717;
17+
--text-color: #171717;
1818
--description-color: #262626;
1919
}
2020

@@ -26,17 +26,17 @@
2626
--background-color: #171717;
2727
--hover-bg-color: #27272a;
2828
--border-color: #262626;
29-
--title-color: #fafafa;
29+
--text-color: #fafafa;
3030
--description-color: #262626;
3131
}
3232
}
3333

3434
.t_dark-theme {
3535
--box-shadow: rgb(0 0 0 / 0.1);
3636
--background-color: #171717;
37-
--hover-bg-color: #27272a;
38-
--border-color: #262626;
39-
--title-color: #fafafa;
37+
--hover-bg-color: #262626;
38+
--border-color: #27272a;
39+
--text-color: #fafafa;
4040
--description-color: #e5e5e5;
4141
}
4242

@@ -60,6 +60,7 @@
6060
border-radius: 0.375rem;
6161
font-size: 0.875rem;
6262
line-height: 1.25rem;
63+
overflow: hidden;
6364
}
6465

6566
.t_global button {
@@ -71,7 +72,11 @@
7172
width: 100%;
7273
height: 100wh;
7374
gap: 0.5rem;
74-
padding: 9px;
75+
padding: 12px;
76+
}
77+
78+
.t_icon {
79+
fill: var(--text-color);
7580
}
7681

7782
.t_content {
@@ -81,7 +86,7 @@
8186

8287
.t_content p {
8388
font-weight: 600;
84-
color: var(--title-color);
89+
color: var(--text-color);
8590
margin: 0;
8691
}
8792

@@ -108,13 +113,16 @@
108113
font-size: 13px;
109114
background-color: transparent;
110115
cursor: default;
116+
border: none;
111117
}
112118

113119
.t_actions > button:nth-child(1) {
120+
color: var(--text-color);
114121
font-weight: 500;
115122
}
116123

117124
.t_actions > button:nth-child(2) {
125+
color: var(--text-color);
118126
border-top: 1px solid var(--border-color);
119127
}
120128

@@ -203,6 +211,21 @@
203211
}
204212
}
205213

214+
.t_slide-top-exit {
215+
animation: slide-top-exit 0.4s ease;
216+
}
217+
218+
@keyframes slide-top-exit {
219+
0% {
220+
opacity: 1;
221+
transform: translateY(0);
222+
}
223+
100% {
224+
opacity: 0;
225+
transform: translateY(-100%);
226+
}
227+
}
228+
206229
.t_slide-right {
207230
animation: slide-right 0.4s ease;
208231
}

library/src/styles/toast-context.css

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
right: 0;
3838
}
3939

40+
.t_top-center {
41+
top: 0;
42+
left: 50%;
43+
transform: translateX(-50%);
44+
}
45+
4046
.t_bottom-left {
4147
bottom: 0;
4248
left: 0;
@@ -51,5 +57,4 @@
5157
bottom: 0;
5258
left: 50%;
5359
transform: translateX(-50%);
54-
padding: 10px;
5560
}

library/src/types/toast.types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ export type Variant = 'success' | 'error' | 'warning' | 'info';
44
export type Position =
55
| 'top-left'
66
| 'top-right'
7+
| 'top-center'
78
| 'bottom-left'
89
| 'bottom-right'
910
| 'bottom-center';
1011
export type Theme = 'light' | 'dark' | 'system';
1112

1213
export type ToastProps = {
1314
id?: number;
14-
variant: Variant;
15+
variant?: Variant;
1516
text: string;
1617
description?: string;
1718
icon?: ReactNode;

0 commit comments

Comments
 (0)