Created
April 8, 2019 19:53
-
-
Save anyley/dc85b8840d7bf305c36a01f68927a457 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { withStyles } from '@material-ui/core/styles'; | |
import Typography from '@material-ui/core/Typography'; | |
import Modal from '@material-ui/core/Modal'; | |
import Button from '@material-ui/core/Button'; | |
function rand() { | |
return Math.round(Math.random() * 20) - 10; | |
} | |
function getModalStyle() { | |
const top = 50 + rand(); | |
const left = 50 + rand(); | |
return { | |
top: `${top}%`, | |
left: `${left}%`, | |
transform: `translate(-${top}%, -${left}%)`, | |
}; | |
} | |
const styles = theme => ({ | |
paper: { | |
position: 'absolute', | |
width: theme.spacing.unit * 50, | |
backgroundColor: theme.palette.background.paper, | |
boxShadow: theme.shadows[5], | |
padding: theme.spacing.unit * 4, | |
}, | |
}); | |
class SimpleModal extends React.Component { | |
state = { | |
open: false, | |
}; | |
handleOpen = () => { | |
this.setState({ open: true }); | |
}; | |
handleClose = () => { | |
this.setState({ open: false }); | |
}; | |
render() { | |
const { classes } = this.props; | |
return ( | |
<div> | |
<Typography gutterBottom>Click to get the full Modal experience!</Typography> | |
<Button onClick={this.handleOpen}>Open Modal</Button> | |
<Modal | |
aria-labelledby="simple-modal-title" | |
aria-describedby="simple-modal-description" | |
open={this.state.open} | |
onClose={this.handleClose} | |
> | |
<div style={getModalStyle()} className={classes.paper}> | |
<Typography variant="title" id="modal-title"> | |
Text in a modal | |
</Typography> | |
<Typography variant="subheading" id="simple-modal-description"> | |
Duis mollis, est non commodo luctus, nisi erat porttitor ligula. | |
</Typography> | |
<SimpleModalWrapped /> | |
</div> | |
</Modal> | |
</div> | |
); | |
} | |
} | |
SimpleModal.propTypes = { | |
classes: PropTypes.object.isRequired, | |
}; | |
// We need an intermediary variable for handling the recursive nesting. | |
const SimpleModalWrapped = withStyles(styles)(SimpleModal); | |
export default SimpleModalWrapped; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment