gab-social/app/javascript/gabsocial/components/modal/modal_base.js

189 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-02-24 21:56:07 +00:00
import { Fragment } from 'react'
import { injectIntl, FormattedMessage, 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'
2020-05-07 06:55:24 +01:00
import Responsive from '../../features/ui/util/responsive_component'
2020-05-14 21:45:39 +01:00
const initialState = getWindowDimension()
const messages = defineMessages({
confirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' },
2020-02-29 15:42:47 +00:00
delete: { id: 'confirmations.delete.message', defaultMessage: 'Are you sure you want to delete this status?' },
2020-02-24 21:56:07 +00:00
})
2020-04-11 23:29:19 +01:00
const mapStateToProps = (state) => ({
2019-08-23 00:05:20 +01:00
composeId: state.getIn(['compose', 'id']),
composeText: state.getIn(['compose', 'text']),
2020-02-24 21:56:07 +00:00
})
const mapDispatchToProps = (dispatch) => ({
onOpenModal(type, opts) {
2020-02-24 21:56:07 +00:00
dispatch(openModal(type, opts))
},
onCancelReplyCompose() {
2020-02-24 21:56:07 +00:00
dispatch(cancelReplyCompose())
},
2020-02-24 21:56:07 +00:00
})
2020-02-25 16:04:44 +00:00
export default
@connect(mapStateToProps, mapDispatchToProps)
@injectIntl
class ModalBase extends PureComponent {
static propTypes = {
children: PropTypes.node,
onClose: PropTypes.func.isRequired,
onOpenModal: PropTypes.func.isRequired,
onCancelReplyCompose: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
2019-08-23 00:05:20 +01:00
composeId: PropTypes.string,
composeText: PropTypes.string,
type: PropTypes.string,
2020-05-14 21:45:39 +01:00
isCenteredXS: PropTypes.bool,
2020-02-24 21:56:07 +00: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
}
2020-02-24 21:56:07 +00:00
activeElement = this.state.revealed ? document.activeElement : null
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()
}
}
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
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') {
onOpenModal('CONFIRM', {
2020-02-29 15:42:47 +00:00
message: intl.formatMessage(messages.delete),
confirm: intl.formatMessage(messages.confirm),
onConfirm: () => onCancelReplyCompose(),
onCancel: () => onOpenModal('COMPOSE'),
2020-02-24 21:56:07 +00:00
})
} else {
2020-02-24 21:56:07 +00:00
onClose()
}
2020-02-24 21:56:07 +00: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)
}
componentWillReceiveProps(nextProps) {
if (!!nextProps.children && !this.props.children) {
2020-02-24 21:56:07 +00:00
this.activeElement = document.activeElement
2020-02-24 21:56:07 +00:00
this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true))
} else if (!nextProps.children) {
2020-02-24 21:56:07 +00:00
this.setState({ revealed: false })
}
if (!nextProps.children && !!this.props.children) {
2020-02-24 21:56:07 +00:00
this.activeElement.focus()
this.activeElement = null
}
}
componentDidUpdate(prevProps) {
if (!this.props.children && !!prevProps.children) {
2020-02-24 21:56:07 +00:00
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'))
}
if (this.props.children) {
requestAnimationFrame(() => {
2020-02-24 21:56:07 +00:00
this.setState({ revealed: true })
})
}
}
2020-05-14 21:45:39 +01:00
handleResize = () => {
const { width } = getWindowDimension()
this.setState({ width })
}
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)
}
getSiblings = () => {
2020-02-24 21:56:07 +00:00
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node)
}
2020-04-07 02:53:23 +01:00
setRef = (n) => {
this.node = n
}
setDialog = (n) => {
this.dialog = n
}
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
2020-05-14 21:45:39 +01:00
const containerClasses = CX({
2020-02-24 21:56:07 +00:00
default: 1,
z4: 1,
2020-03-08 16:46:00 +00:00
height100PC: visible,
width100PC: visible,
2020-02-24 21:56:07 +00:00
displayNone: !visible,
})
2020-05-14 21:45:39 +01:00
const dialogClasses = CX({
default: 1,
posFixed: 1,
alignItemsCenter: 1,
justifyContentCenter: !isXS || isCenteredXS,
justifyContentEnd: isXS && !isCenteredXS,
z4: 1,
width100PC: 1,
height100PC: 1,
top0: 1,
rightAuto: 1,
bottomAuto: 1,
left0: 1,
})
return (
2020-02-24 21:56:07 +00:00
<div ref={this.setRef} className={containerClasses}>
{
!!visible &&
<Fragment>
<div
role='presentation'
2020-04-29 23:32:49 +01:00
className={[_s.default, _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-02-24 21:56:07 +00:00
</Fragment>
}
</div>
2020-02-24 21:56:07 +00:00
)
}
}