Skip to content

Commit 731974e

Browse files
authored
Merge pull request #309 from MetroStar/toast-update-placement
Adding placement prop to toast with styles and keyframes.
2 parents d7d23fa + 6248f68 commit 731974e

File tree

4 files changed

+98
-19
lines changed

4 files changed

+98
-19
lines changed

packages/comet-extras/src/components/toast/toast.stories.tsx

+14-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const meta: Meta<typeof Toast> = {
1111
message: { control: 'text' },
1212
duration: { control: 'number' },
1313
type: { control: 'select', required: true },
14+
placement: { control: 'select', required: true },
1415
onClose: { action: 'close' },
1516
allowClose: { control: 'boolean' },
1617
className: { control: false },
@@ -34,6 +35,7 @@ const Template: StoryFn<typeof Toast> = (args: ToastProps) => {
3435
id: `toast-${args.type}`,
3536
message: `${!args.message ? 'Default toast notification for ' + args.type : args.message}`,
3637
type: `${args.type}`,
38+
placement: `${args.placement}`,
3739
duration: `${!args.duration ? 3000 : args.duration}`,
3840
allowClose: args.allowClose,
3941
};
@@ -46,18 +48,17 @@ const Template: StoryFn<typeof Toast> = (args: ToastProps) => {
4648
Send Toast
4749
</Button>
4850

49-
<div>
50-
{toasts.map((toast) => (
51-
<Toast
52-
key={toast.id}
53-
id={toast.id}
54-
message={toast.message}
55-
type={toast.type}
56-
duration={toast.duration}
57-
allowClose={toast.allowClose}
58-
/>
59-
))}
60-
</div>
51+
{toasts.map((toast) => (
52+
<Toast
53+
key={toast.id}
54+
id={toast.id}
55+
message={toast.message}
56+
type={toast.type}
57+
placement={toast.placement}
58+
duration={toast.duration}
59+
allowClose={toast.allowClose}
60+
/>
61+
))}
6162
</div>
6263
);
6364
};
@@ -67,6 +68,7 @@ Default.args = {
6768
id: 'toast-info',
6869
message: 'This is a toast notification',
6970
type: 'info',
71+
placement: 'topRight',
7072
duration: 3000,
7173
allowClose: true,
7274
};

packages/comet-extras/src/components/toast/toast.style.css

+52-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@
88
padding: 12px 16px;
99
color: #1b1b1b;
1010
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
11-
animation: slideIn 0.3s ease-out forwards;
1211
position: fixed;
12+
}
13+
14+
.toast--topLeft {
1315
top: 20px;
16+
left: 20px;
17+
animation: slideInFromLeft 0.3s ease-out forwards;
18+
}
19+
20+
.toast--topRight {
21+
top: 20px;
22+
right: 20px;
23+
animation: slideInFromRight 0.3s ease-out forwards;
24+
}
25+
26+
.toast--bottomLeft {
27+
bottom: 20px;
28+
left: 20px;
29+
animation: slideInFromLeft 0.3s ease-out forwards;
30+
}
31+
32+
.toast--bottomRight {
33+
bottom: 20px;
1434
right: 20px;
35+
animation: slideInFromRight 0.3s ease-out forwards;
1536
}
1637

1738
.toast--info {
@@ -59,11 +80,17 @@
5980
transition: background-color 0.2s;
6081
}
6182

62-
.toast--isLeaving {
63-
animation: slideOut 0.3s ease-in forwards;
83+
.toast--bottomRight.toast--isLeaving,
84+
.toast--topRight.toast--isLeaving {
85+
animation: slideOutToRight 0.3s ease-in forwards;
6486
}
6587

66-
@keyframes slideIn {
88+
.toast--bottomLeft.toast--isLeaving,
89+
.toast--topLeft.toast--isLeaving {
90+
animation: slideOutToLeft 0.3s ease-in forwards;
91+
}
92+
93+
@keyframes slideInFromRight {
6794
from {
6895
transform: translateX(100%);
6996
opacity: 0;
@@ -74,11 +101,18 @@
74101
}
75102
}
76103

77-
.toast--isLeaving {
78-
animation: slideOut 0.3s ease-in forwards;
104+
@keyframes slideInFromLeft {
105+
from {
106+
transform: translateX(-100%);
107+
opacity: 0;
108+
}
109+
to {
110+
transform: translateX(0);
111+
opacity: 1;
112+
}
79113
}
80114

81-
@keyframes slideOut {
115+
@keyframes slideOutToRight {
82116
from {
83117
transform: translateX(0);
84118
opacity: 1;
@@ -88,3 +122,14 @@
88122
opacity: 0;
89123
}
90124
}
125+
126+
@keyframes slideOutToLeft {
127+
from {
128+
transform: translateX(0);
129+
opacity: 1;
130+
}
131+
to {
132+
transform: translateX(-100%);
133+
opacity: 0;
134+
}
135+
}

packages/comet-extras/src/components/toast/toast.test.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ describe('Toast Component Tests', () => {
1919
expect(container.querySelector('#test-toast')).toHaveClass('toast--info');
2020
});
2121

22+
test('should render a Toast notification from the top left', () => {
23+
const { container } = render(
24+
<Toast
25+
id="test-toast"
26+
placement="topLeft"
27+
message="Testing message for notification"
28+
type="info"
29+
/>,
30+
);
31+
expect(container.querySelector('#test-toast')).toBeTruthy();
32+
expect(container.querySelector('#test-toast')).toHaveClass('toast--topLeft');
33+
});
34+
35+
test('should render a Toast notification from the bottom right', () => {
36+
const { container } = render(
37+
<Toast
38+
id="test-toast"
39+
placement="bottomRight"
40+
message="Testing message for notification"
41+
type="info"
42+
/>,
43+
);
44+
expect(container.querySelector('#test-toast')).toBeTruthy();
45+
expect(container.querySelector('#test-toast')).toHaveClass('toast--bottomRight');
46+
});
47+
2248
test('should render a warning Toast notification', () => {
2349
const { container } = render(
2450
<Toast id="test-toast" message="Testing message for notification" type="warning" />,

packages/comet-extras/src/components/toast/toast.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export interface ToastProps {
2020
* The type of toast which determines its color scheme
2121
* */
2222
type?: 'success' | 'error' | 'warning' | 'info' | 'emergency';
23+
/**
24+
* The placement of toast which determines where it appears
25+
* */
26+
placement?: 'topRight' | 'bottomRight' | 'topLeft' | 'bottomLeft';
2327
/**
2428
* Callback function when toast is closed either manually or automatically
2529
* */
@@ -39,6 +43,7 @@ export const Toast = ({
3943
message = 'This is a toast notification',
4044
duration = 3000,
4145
type = 'info',
46+
placement = 'topRight',
4247
className = '',
4348
onClose = () => {},
4449
allowClose = true,
@@ -49,6 +54,7 @@ export const Toast = ({
4954
const classes = classnames(
5055
'toast',
5156
`toast--${type}`,
57+
`toast--${placement}`,
5258
className,
5359
`${isLeaving ? 'toast--isLeaving' : ''}`,
5460
);

0 commit comments

Comments
 (0)