Skip to content

Commit 349bbcd

Browse files
committed
Modal: Component
1 parent 2a5cf97 commit 349bbcd

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/App.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,35 @@ import React, { Component } from 'react';
22
import logo from './logo.svg';
33
import './App.css';
44
import InputTag from './components/InputTag';
5+
import Modal from './components/Modal/Modal';
56

67

78
class App extends Component {
9+
state = {
10+
show: false
11+
}
12+
13+
showModal = () => {
14+
this.setState({
15+
...this.state,
16+
show: !this.state.show
17+
});
18+
}
819
render() {
920
return (
1021
<div className="App">
1122
<InputTag placeholder="press enter/space to + tag" />
1223

24+
<input type="button"
25+
onClick={this.showModal}
26+
value="Show Modal" />
27+
28+
<Modal
29+
onClose={this.showModal}
30+
show={this.state.show}>
31+
This message is from Modal!
32+
</Modal>
33+
1334
</div>
1435
);
1536
}

src/components/Modal/Modal.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
// gray background
5+
const backdropStyle = {
6+
position: 'fixed',
7+
top: 0,
8+
bottom: 0,
9+
left: 0,
10+
right: 0,
11+
backgroundColor: 'rgba(0,0,0,0.3)',
12+
padding: 50
13+
}
14+
15+
const modalStyle = {
16+
backgroundColor: '#fff',
17+
borderRadius: 5,
18+
maxWidth: 500,
19+
minHeight: 300,
20+
margin: '0 auto',
21+
padding: 30,
22+
position: "relative"
23+
};
24+
25+
const footerStyle = {
26+
position: "absolute",
27+
bottom: 20
28+
};
29+
30+
export default class Modal extends React.Component {
31+
onClose = (e) => {
32+
this.props.onClose && this.props.onClose(e);
33+
}
34+
render() {
35+
if (!this.props.show) {
36+
return null;
37+
}
38+
return (
39+
<div style={backdropStyle}>
40+
<div style={modalStyle}>
41+
{this.props.children}
42+
<div style={footerStyle}>
43+
<button onClick={(e) => { this.onClose(e)}}>
44+
Close
45+
</button>
46+
</div>
47+
</div>
48+
</div>
49+
)
50+
}
51+
}
52+
53+
Modal.propTypes = {
54+
onClose: PropTypes.func.isRequired
55+
}

0 commit comments

Comments
 (0)