2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-08-19 01:22:15 +01:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl'
|
2020-05-14 21:45:39 +01:00
|
|
|
import { getWindowDimension } from '../../utils/is_mobile'
|
2020-02-24 21:56:07 +00:00
|
|
|
import { openModal } from '../../actions/modal'
|
|
|
|
import { cancelReplyCompose } from '../../actions/compose'
|
2020-05-14 21:45:39 +01:00
|
|
|
import {
|
|
|
|
CX,
|
|
|
|
BREAKPOINT_EXTRA_SMALL,
|
|
|
|
} from '../../constants'
|
|
|
|
|
|
|
|
const initialState = getWindowDimension()
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-08-17 21:07:16 +01:00
|
|
|
class ModalBase extends React.PureComponent {
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
revealed: !!this.props.children,
|
2020-05-14 21:45:39 +01:00
|
|
|
width: initialState.width,
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
activeElement = this.state.revealed ? document.activeElement : null
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
handleKeyUp = (e) => {
|
2020-04-08 02:06:59 +01:00
|
|
|
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27) && !!this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.handleOnClose()
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:53:23 +01:00
|
|
|
handleOnClose = (e) => {
|
2020-02-24 21:56:07 +00:00
|
|
|
const { onOpenModal, composeText, composeId, onClose, intl, type, onCancelReplyCompose } = this.props
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-04-08 02:06:59 +01:00
|
|
|
if (!!e && this.dialog !== e.target) return
|
2020-04-07 02:53:23 +01:00
|
|
|
|
2020-04-08 02:06:59 +01:00
|
|
|
if (!composeId && composeText && type === 'COMPOSE') {
|
2019-08-07 06:02:36 +01:00
|
|
|
onOpenModal('CONFIRM', {
|
2020-02-29 15:42:47 +00:00
|
|
|
message: intl.formatMessage(messages.delete),
|
2019-08-07 06:02:36 +01:00
|
|
|
confirm: intl.formatMessage(messages.confirm),
|
|
|
|
onConfirm: () => onCancelReplyCompose(),
|
|
|
|
onCancel: () => onOpenModal('COMPOSE'),
|
2020-02-24 21:56:07 +00:00
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
} else {
|
2020-02-24 21:56:07 +00:00
|
|
|
onClose()
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-05-13 01:36:54 +01:00
|
|
|
componentDidMount() {
|
2020-05-14 21:45:39 +01:00
|
|
|
this.handleResize()
|
2020-02-24 21:56:07 +00:00
|
|
|
window.addEventListener('keyup', this.handleKeyUp, false)
|
2020-05-14 21:45:39 +01:00
|
|
|
window.addEventListener('resize', this.handleResize, false)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 01:36:54 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2019-08-07 06:02:36 +01:00
|
|
|
if (!!nextProps.children && !this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.activeElement = document.activeElement
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true))
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (!nextProps.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ revealed: false })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextProps.children && !!this.props.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.activeElement.focus()
|
|
|
|
this.activeElement = null
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 01:36:54 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-08-07 06:02:36 +01:00
|
|
|
if (!this.props.children && !!prevProps.children) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'))
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.children) {
|
|
|
|
requestAnimationFrame(() => {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ revealed: true })
|
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 21:45:39 +01:00
|
|
|
handleResize = () => {
|
|
|
|
const { width } = getWindowDimension()
|
|
|
|
|
|
|
|
this.setState({ width })
|
|
|
|
}
|
|
|
|
|
2020-05-13 01:36:54 +01:00
|
|
|
componentWillUnmount() {
|
2020-02-24 21:56:07 +00:00
|
|
|
window.removeEventListener('keyup', this.handleKeyUp)
|
2020-05-14 21:45:39 +01:00
|
|
|
window.removeEventListener('resize', this.handleResize, false)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getSiblings = () => {
|
2020-02-24 21:56:07 +00:00
|
|
|
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 02:53:23 +01:00
|
|
|
setRef = (n) => {
|
|
|
|
this.node = n
|
|
|
|
}
|
|
|
|
|
|
|
|
setDialog = (n) => {
|
|
|
|
this.dialog = n
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 01:36:54 +01:00
|
|
|
render() {
|
2020-05-14 21:45:39 +01:00
|
|
|
const { children, isCenteredXS } = this.props
|
|
|
|
const { width } = this.state
|
2020-02-24 21:56:07 +00:00
|
|
|
|
2020-05-14 21:45:39 +01:00
|
|
|
const isXS = width <= BREAKPOINT_EXTRA_SMALL
|
2020-02-24 21:56:07 +00:00
|
|
|
const visible = !!children
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-05-14 21:45:39 +01:00
|
|
|
const containerClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-02-24 21:56:07 +00:00
|
|
|
z4: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
h100PC: visible,
|
|
|
|
w100PC: visible,
|
2020-02-24 21:56:07 +00:00
|
|
|
displayNone: !visible,
|
|
|
|
})
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-05-14 21:45:39 +01:00
|
|
|
const dialogClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-05-14 21:45:39 +01:00
|
|
|
posFixed: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
aiCenter: 1,
|
|
|
|
jcCenter: !isXS || isCenteredXS,
|
|
|
|
jcEnd: isXS && !isCenteredXS,
|
2020-05-14 21:45:39 +01:00
|
|
|
z4: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
w100PC: 1,
|
|
|
|
h100PC: 1,
|
2020-05-14 21:45:39 +01:00
|
|
|
top0: 1,
|
|
|
|
rightAuto: 1,
|
|
|
|
bottomAuto: 1,
|
|
|
|
left0: 1,
|
|
|
|
})
|
|
|
|
|
2019-08-07 06:02:36 +01:00
|
|
|
return (
|
2020-02-24 21:56:07 +00:00
|
|
|
<div ref={this.setRef} className={containerClasses}>
|
|
|
|
{
|
|
|
|
!!visible &&
|
2020-08-17 21:07:16 +01:00
|
|
|
<React.Fragment>
|
2020-02-24 21:56:07 +00:00
|
|
|
<div
|
|
|
|
role='presentation'
|
2020-08-18 21:49:11 +01:00
|
|
|
className={[_s.d, _s.bgBlackOpaque, _s.posFixed, _s.z3, _s.top0, _s.right0, _s.bottom0, _s.left0].join(' ')}
|
2020-02-24 21:56:07 +00:00
|
|
|
/>
|
2020-05-14 21:45:39 +01:00
|
|
|
<div
|
|
|
|
ref={this.setDialog}
|
|
|
|
role='dialog'
|
|
|
|
onClick={this.handleOnClose}
|
|
|
|
className={dialogClasses}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
2020-08-17 21:07:16 +01:00
|
|
|
</React.Fragment>
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
</div>
|
2020-02-24 21:56:07 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 18:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
confirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' },
|
|
|
|
delete: { id: 'confirmations.delete.message', defaultMessage: 'Are you sure you want to delete this status?' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
composeId: state.getIn(['compose', 'id']),
|
|
|
|
composeText: state.getIn(['compose', 'text']),
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onOpenModal(type, opts) {
|
|
|
|
dispatch(openModal(type, opts))
|
|
|
|
},
|
|
|
|
onCancelReplyCompose() {
|
|
|
|
dispatch(cancelReplyCompose())
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ModalBase.propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
onOpenModal: PropTypes.func.isRequired,
|
|
|
|
onCancelReplyCompose: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
composeId: PropTypes.string,
|
|
|
|
composeText: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
isCenteredXS: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ModalBase))
|