|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { withStyles } from '@material-ui/core/styles'; |
| 4 | +import Popper from '@material-ui/core/Popper'; |
| 5 | +import Typography from '@material-ui/core/Typography'; |
| 6 | +import Button from '@material-ui/core/Button'; |
| 7 | +import Fade from '@material-ui/core/Fade'; |
| 8 | +import Paper from '@material-ui/core/Paper'; |
| 9 | +import toRenderProps from 'recompose/toRenderProps'; |
| 10 | +import withState from 'recompose/withState'; |
| 11 | + |
| 12 | +const WithState = toRenderProps(withState('anchorEl', 'updateAnchorEl', null)); |
| 13 | + |
| 14 | +const styles = theme => ({ |
| 15 | + typography: { |
| 16 | + padding: theme.spacing.unit * 2, |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +function RenderPropsPopper(props) { |
| 21 | + const { classes } = props; |
| 22 | + |
| 23 | + return ( |
| 24 | + <WithState> |
| 25 | + {({ anchorEl, updateAnchorEl }) => { |
| 26 | + const open = Boolean(anchorEl); |
| 27 | + const id = open ? 'render-props-popper' : null; |
| 28 | + return ( |
| 29 | + <React.Fragment> |
| 30 | + <Button |
| 31 | + aria-describedby={id} |
| 32 | + variant="contained" |
| 33 | + onClick={event => { |
| 34 | + updateAnchorEl(anchorEl ? null : event.currentTarget); |
| 35 | + }} |
| 36 | + > |
| 37 | + Toggle Popper |
| 38 | + </Button> |
| 39 | + <Popper id={id} open={open} anchorEl={anchorEl} transition> |
| 40 | + {({ TransitionProps }) => ( |
| 41 | + <Fade {...TransitionProps} timeout={350}> |
| 42 | + <Paper> |
| 43 | + <Typography className={classes.typography}> |
| 44 | + The content of the Popper. |
| 45 | + </Typography> |
| 46 | + </Paper> |
| 47 | + </Fade> |
| 48 | + )} |
| 49 | + </Popper> |
| 50 | + </React.Fragment> |
| 51 | + ); |
| 52 | + }} |
| 53 | + </WithState> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +RenderPropsPopper.propTypes = { |
| 58 | + classes: PropTypes.object.isRequired, |
| 59 | +}; |
| 60 | + |
| 61 | +export default withStyles(styles)(RenderPropsPopper); |
0 commit comments