Skip to content

Commit 654e47c

Browse files
committed
update style for Toast and add dismiss button
1 parent 0ebe353 commit 654e47c

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

packages/react-scripts/template/src/Toast.css

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
bottom: 0;
44
left: 0;
55
right: 0;
6-
max-width: 100%;
7-
width: 240px;
86
padding: 16px;
97
margin: auto;
108
transition: 250ms;
119
background: #222;
1210
color: white;
13-
text-align: center;
1411
line-height: 24px;
1512
border-top-left-radius: 2px;
1613
border-top-right-radius: 2px;
14+
display: flex;
1715
}
1816

1917
.Toast.active {
@@ -22,4 +20,20 @@
2220

2321
.Toast.inactive {
2422
transform: translateY(100%);
23+
}
24+
25+
.Toast-button {
26+
padding: 0;
27+
background: transparent;
28+
border: 0;
29+
display: inline-block;
30+
color: #61dafb;
31+
font-size: inherit;
32+
line-height: inherit;
33+
text-transform: uppercase;
34+
flex: 0 0 0;
35+
}
36+
37+
.Toast-text {
38+
flex: 1;
2539
}

packages/react-scripts/template/src/Toast.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ export default class Toast extends React.Component {
55
state = {
66
active: true,
77
};
8+
9+
close = () => {
10+
clearTimeout(this.timeout);
11+
this.setState({
12+
active: false,
13+
});
14+
};
815
componentDidMount() {
9-
setTimeout(
16+
this.timeout = setTimeout(
1017
() => {
1118
this.setState({
1219
active: false,
@@ -15,10 +22,16 @@ export default class Toast extends React.Component {
1522
this.props.timeout
1623
);
1724
}
25+
componentWillUnmount() {
26+
clearTimeout(this.timeout);
27+
}
1828
render() {
1929
return (
2030
<div className={`Toast ${this.state.active ? 'active' : 'inactive'}`}>
21-
{this.props.children}
31+
<span className="Toast-text">
32+
{this.props.children}
33+
</span>
34+
<button className="Toast-button" onClick={this.close}>Dismiss</button>
2235
</div>
2336
);
2437
}

packages/react-scripts/template/src/registerServiceWorker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (!dom) {
2424
function showMessage(message) {
2525
if (useToast) {
2626
ReactDOM.render(
27-
<Toast timeout={3000}>
27+
<Toast>
2828
{message}
2929
</Toast>,
3030
dom

0 commit comments

Comments
 (0)