Skip to content

Commit c1aa950

Browse files
author
Machiko Yasuda
committed
feat(toast): add close icon
Signed-off-by: Machiko Yasuda <machiko@reactioncommerce.com>
1 parent 5f5a732 commit c1aa950

File tree

3 files changed

+75
-45
lines changed

3 files changed

+75
-45
lines changed

package/src/components/Toast/Toast.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
11
import React from "react";
22
import PropTypes from "prop-types";
3-
import { Snackbar } from "@material-ui/core";
3+
import { Snackbar, IconButton } from "@material-ui/core";
4+
import CloseIcon from "mdi-material-ui/Close";
5+
import { makeStyles } from "@material-ui/core/styles";
46
import { ToastWrapper } from "./helpers";
57

8+
const useStyles = makeStyles((theme) => ({
9+
close: {
10+
padding: 4,
11+
marginRight: -8
12+
}
13+
}));
14+
615
/**
716
* @name Toast
817
* @param {Object} props Component props
918
* @returns {React.Component} returns a React component
1019
*/
1120
const Toast = React.forwardRef(function Toast(props, ref) {
21+
const classes = useStyles();
1222
const { className, message, variant, title, ...otherProps } = props;
23+
const [open, setOpen] = React.useState(false);
24+
25+
const handleClose = (event, reason) => {
26+
if (reason === "clickaway") {
27+
return;
28+
}
29+
30+
setOpen(false);
31+
};
32+
1333
return (
1434
<Snackbar
1535
ref={ref}
36+
open={open}
1637
{...otherProps}
1738
>
1839
<ToastWrapper
1940
props={otherProps}
2041
variant={variant}
2142
title={title}
2243
message={message}
44+
onClose={handleClose}
45+
action={
46+
<IconButton
47+
key="close"
48+
aria-label="close"
49+
color="inherit"
50+
className={classes.close}
51+
onClick={handleClose}
52+
>
53+
<CloseIcon />
54+
</IconButton>}
2355
/>
2456
</Snackbar>
2557
);
@@ -34,9 +66,12 @@ Toast.propTypes = {
3466
*/
3567
className: PropTypes.string,
3668
/**
37-
* Message
69+
* Message: Node
70+
*/
71+
message: PropTypes.node,
72+
/**
73+
* Title: Optional
3874
*/
39-
message: PropTypes.string,
4075
title: PropTypes.string,
4176
/**
4277
* Variant: Info, Success, Warning, Error

package/src/components/Toast/Toast.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
### Overview
22

3-
<!-- Get this short paragraph from design. -->
3+
Toasts are used to give action-based feedback messages and convey critical or informational account-related messages. Use Toasts when a user needs more detailed information for an action.
44

5-
The X component inherits from the Material-UI [X component](https://material-ui.com/components/X/). Refer to the Material-UI [X docs](https://material-ui.com/api/X/) for more information.
5+
The Toast component inherits from the Material-UI [Snackbar component](https://material-ui.com/components/snackbars/). Refer to the Material-UI [Snackbar docs](https://material-ui.com/api/snackbar/) for more information.
66

77
### Usage
88

9-
<!-- Show all the variants/combos we use in Reaction Admin, without the code box > -->
10-
119
```jsx
1210
<Toast
13-
anchorOrigin={{
14-
vertical: 'bottom',
15-
horizontal: 'left',
16-
}}
1711
open
18-
autoHideDuration={6000}
19-
ContentProps={{
20-
'aria-describedby': 'message-id',
21-
}}
12+
autoHideDuration={300}
2213
message={"Note archived"}
2314
title={"Title"}
2415
/>
2516
```
2617

18+
Toasts are most often used when the user has taken an action. Messages appear in context and communicate when that action is successful, unsuccessful, or that it otherwise needs attention and further context.
19+
20+
Language should be polite, clear and concise. Optionally, a title can be added to a Toast to give clarity, or when there are 2 or more lines of information to display.
21+
22+
Toasts should guide the user into taking corrective action if necessary.
23+
24+
Users should be able to dismiss Toasts when appropriate. Information and success alerts can close automatically after 10 seconds. Error alerts should be persistent, and close only when action is resolved.
25+
2726
#### Types
2827

2928
##### Information
3029

31-
<!-- Explain when to use this type of the component, and give a real life Reaction Admin example. If needed, add instruction for developers on how to set up the component. -->
32-
33-
Use a X component to allow a user to XX, such as XYXY.
30+
- Used when there is information or tips that users can benefit from
31+
- Can close automatically after 10 seconds
3432

3533
```jsx
3634
import Button from "../Button";
@@ -73,14 +71,15 @@ function OpenToast(props) {
7371
);
7472
}
7573

76-
<OpenToast message="Information toast" title="Info" variant="info" />
74+
<OpenToast message={<span>Information toast</span>} title="Info" variant="info" />
7775
```
7876

7977
##### Success
8078

8179
<!-- Explain when to use this type of the component, and give a real life Reaction Admin example. If needed, add instruction for developers on how to set up the component. -->
8280

83-
Use a X component to allow a user to XX, such as XYXY.
81+
- Used when an action has been completed successfully
82+
- Can close automatically after 10 seconds
8483

8584
```jsx
8685
import Button from "../Button";
@@ -122,14 +121,13 @@ function OpenToast(props) {
122121
);
123122
}
124123

125-
<OpenToast message="Success toast" variant="success" />
124+
<OpenToast message={<span>Success toast</span>} variant="success" />
126125
```
127126

128127
##### Warning
129128

130-
<!-- Explain when to use this type of the component, and give a real life Reaction Admin example. If needed, add instruction for developers on how to set up the component. -->
131-
132-
Use a X component to allow a user to XX, such as XYXY.
129+
- Used when an action or item needs attention
130+
- Should not close automatically, unless the action has been resolved
133131

134132
```jsx
135133
import Button from "../Button";
@@ -171,14 +169,13 @@ function OpenToast(props) {
171169
);
172170
}
173171

174-
<OpenToast message="Warning toast" variant="warning" />
172+
<OpenToast message={<span>Warning toast</span>} variant="warning" />
175173
```
176174

177175
##### Error
178176

179-
<!-- Explain when to use this type of the component, and give a real life Reaction Admin example. If needed, add instruction for developers on how to set up the component. -->
180-
181-
Use a X component to allow a user to XX, such as XYXY.
177+
- Used when the system has failed to complete an action, or the user has made an error
178+
- Should not close automatically, unless the action has been resolved
182179

183180
```jsx
184181
import Button from "../Button";
@@ -220,5 +217,5 @@ function OpenToast(props) {
220217
);
221218
}
222219

223-
<OpenToast message="Error toast" variant="error" />
220+
<OpenToast message={<span>Error toast</span>} variant="error" />
224221
```

package/src/components/Toast/helpers/ToastWrapper.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ import { Paper, Typography } from "@material-ui/core";
55
import { makeStyles } from "@material-ui/core/styles";
66

77
const useStyles = makeStyles((theme) => ({
8-
message: {
9-
padding: "8px 0"
8+
messageWrapper: {
9+
// padding: "8px 0"
1010
},
1111
title: {
12-
padding: "8px 0 0 0",
12+
padding: "4px 0 8px 0",
1313
fontWeight: theme.typography.fontWeightSemiBold
1414
},
1515
action: {
16-
display: "flex",
17-
alignItems: "center",
18-
marginLeft: "auto",
19-
paddingLeft: 16,
20-
marginRight: -8
16+
marginLeft: "auto"
2117
},
2218
success: {
2319
fontSize: theme.typography.fontSize,
@@ -27,7 +23,7 @@ const useStyles = makeStyles((theme) => ({
2723
padding: "8px 16px",
2824
borderRadius: theme.shape.borderRadius,
2925
display: "flex",
30-
flexDirection: "column",
26+
flexDirection: "row",
3127
minWidth: 288
3228
},
3329
error: {
@@ -38,7 +34,7 @@ const useStyles = makeStyles((theme) => ({
3834
padding: "8px 16px",
3935
borderRadius: theme.shape.borderRadius,
4036
display: "flex",
41-
flexDirection: "column",
37+
flexDirection: "row",
4238
minWidth: 288
4339
},
4440
info: {
@@ -49,7 +45,7 @@ const useStyles = makeStyles((theme) => ({
4945
padding: "8px 16px",
5046
borderRadius: theme.shape.borderRadius,
5147
display: "flex",
52-
flexDirection: "column",
48+
flexDirection: "row",
5349
minWidth: 288
5450
},
5551
warning: {
@@ -60,7 +56,7 @@ const useStyles = makeStyles((theme) => ({
6056
padding: "8px 16px",
6157
borderRadius: theme.shape.borderRadius,
6258
display: "flex",
63-
flexDirection: "column",
59+
flexDirection: "row",
6460
minWidth: 288
6561
}
6662
}));
@@ -76,7 +72,7 @@ export default function ToastWrapper(props) {
7672

7773
return (
7874
<Paper
79-
component={Typography}
75+
component={"div"}
8076
role="alertdialog"
8177
square
8278
elevation={6}
@@ -85,8 +81,10 @@ export default function ToastWrapper(props) {
8581
title={title}
8682
{...otherProps}
8783
>
88-
{ title ? <Typography variant="h4" component="div" className={classes.title}>{title}</Typography> : null }
89-
<div className={classes.message}>{message}</div>
84+
<div className={classes.messageWrapper}>
85+
{ title ? <Typography variant="h4" component="div" className={classes.title}>{title}</Typography> : null }
86+
{message}
87+
</div>
9088
{action ? <div className={classes.action}>{action}</div> : null }
9189
</Paper>
9290
);
@@ -95,7 +93,7 @@ export default function ToastWrapper(props) {
9593
ToastWrapper.propTypes = {
9694
action: PropTypes.node,
9795
className: PropTypes.string,
98-
message: PropTypes.string,
96+
message: PropTypes.node,
9997
title: PropTypes.string,
10098
variant: PropTypes.oneOf(["error", "info", "success", "warning"]).isRequired
10199
};

0 commit comments

Comments
 (0)