|
| 1 | +import React, { PropTypes, Component } from 'react' |
| 2 | +import ReactTransitionGroup from 'react-addons-transition-group' |
| 3 | + |
| 4 | +import AppBar from 'material-ui/AppBar' |
| 5 | +import IconButton from 'material-ui/IconButton' |
| 6 | +import NavigationCloseIcon from 'material-ui/svg-icons/navigation/close' |
| 7 | + |
| 8 | +import FullscreenDialogFrame from './FullscreenDialogFrame' |
| 9 | + |
| 10 | +const getStyles = (props, theme) => ({ |
| 11 | + root: { |
| 12 | + display: 'flex', |
| 13 | + flexDirection: 'column' |
| 14 | + }, |
| 15 | + title: { |
| 16 | + fontSize: 20, |
| 17 | + fontWeight: 500 |
| 18 | + }, |
| 19 | + appBar: { |
| 20 | + height: (props.appBarStyle ? props.appBarStyle.height : null) || theme.appBar.height |
| 21 | + }, |
| 22 | + container: { |
| 23 | + flex: 1, |
| 24 | + overflow: 'auto', |
| 25 | + padding: 16 |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +export default class FullscreenDialog extends Component { |
| 30 | + componentDidMount () { |
| 31 | + this.id = `popup-${Date.now()}` |
| 32 | + if (this.props.open) { |
| 33 | + history.replaceState(this.id, 'popup') |
| 34 | + history.pushState(this.id, 'popup') |
| 35 | + } |
| 36 | + window.addEventListener('popstate', this.onPopState) |
| 37 | + } |
| 38 | + |
| 39 | + componentWillUnmount () { |
| 40 | + window.removeEventListener('popstate', this.onPopState) |
| 41 | + } |
| 42 | + |
| 43 | + onPopState = ({ state }) => { |
| 44 | + history.pushState(this.id, 'popup') |
| 45 | + if (state === this.id && this.props.onRequestClose) { |
| 46 | + this.props.onRequestClose() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + componentWillReceiveProps ({ open }) { |
| 51 | + if (open && !this.props.open) { |
| 52 | + history.replaceState(this.id, 'popup') |
| 53 | + history.pushState(this.id, 'popup') |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + render () { |
| 58 | + const styles = getStyles(this.props, this.context.muiTheme) |
| 59 | + |
| 60 | + const { |
| 61 | + actionButton, |
| 62 | + appBarStyle, |
| 63 | + children, |
| 64 | + closeIcon, |
| 65 | + containerStyle, |
| 66 | + open, |
| 67 | + style, |
| 68 | + title, |
| 69 | + titleStyle, |
| 70 | + } = this.props |
| 71 | + |
| 72 | + return ( |
| 73 | + <FullscreenDialogFrame |
| 74 | + open={open} |
| 75 | + style={{ ...style, ...styles.root }} |
| 76 | + > |
| 77 | + <AppBar |
| 78 | + title={title} |
| 79 | + titleStyle={{ ...styles.title, ...titleStyle }} |
| 80 | + style={{ ...styles.appBar, ...appBarStyle }} |
| 81 | + iconElementLeft={( |
| 82 | + <IconButton onTouchTap={() => this.props.onRequestClose()}> |
| 83 | + {closeIcon || <NavigationCloseIcon />} |
| 84 | + </IconButton> |
| 85 | + )} |
| 86 | + iconElementRight={actionButton} |
| 87 | + /> |
| 88 | + <div style={{ ...styles.container, ...containerStyle }}> |
| 89 | + {this.props.children} |
| 90 | + </div> |
| 91 | + </FullscreenDialogFrame> |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +FullscreenDialog.propTypes = { |
| 97 | + actionButton: PropTypes.node, |
| 98 | + children: PropTypes.node, |
| 99 | + closeIcon: PropTypes.node, |
| 100 | + onRequestClose: PropTypes.func, |
| 101 | + open: PropTypes.bool.isRequired, |
| 102 | + style: PropTypes.object, |
| 103 | + title: PropTypes.string |
| 104 | +} |
| 105 | + |
| 106 | +FullscreenDialog.defaultProps = { |
| 107 | + actions: [] |
| 108 | +} |
| 109 | + |
| 110 | +FullscreenDialog.contextTypes = { |
| 111 | + muiTheme: PropTypes.object |
| 112 | +} |
0 commit comments