bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/common/modal/simple/component.jsx

144 lines
3.7 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2019-05-17 06:13:08 +08:00
import { defineMessages, injectIntl } from 'react-intl';
2023-07-31 22:24:25 +08:00
import FocusTrap from 'focus-trap-react';
2021-11-11 03:10:35 +08:00
import Styled from './styles';
2019-05-17 06:13:08 +08:00
const intlMessages = defineMessages({
modalClose: {
id: 'app.modal.close',
description: 'Close',
},
modalCloseDescription: {
id: 'app.modal.close.description',
description: 'Disregards changes and closes the modal',
},
});
const propTypes = {
title: PropTypes.string,
dismiss: PropTypes.shape({
callback: PropTypes.func,
}),
headerPosition: PropTypes.string,
2023-07-31 22:24:25 +08:00
shouldCloseOnOverlayClick: PropTypes.bool,
shouldShowCloseButton: PropTypes.bool,
overlayClassName: PropTypes.string,
modalisOpen: PropTypes.bool,
};
const defaultProps = {
2023-07-31 22:24:25 +08:00
title: '',
dismiss: {
callback: null,
},
2017-04-19 03:06:51 +08:00
shouldCloseOnOverlayClick: true,
2019-02-27 01:40:01 +08:00
shouldShowCloseButton: true,
overlayClassName: 'modalOverlay',
headerPosition: 'inner',
2023-07-31 22:24:25 +08:00
modalisOpen: false,
};
class ModalSimple extends Component {
2019-01-15 04:23:46 +08:00
constructor(props) {
super(props);
2023-07-31 22:24:25 +08:00
this.modalRef = React.createRef();
2019-01-15 04:23:46 +08:00
this.handleDismiss = this.handleDismiss.bind(this);
2023-07-31 22:24:25 +08:00
this.handleRequestClose = this.handleRequestClose.bind(this);
this.handleOutsideClick = this.handleOutsideClick.bind(this);
}
componentDidMount() {
document.addEventListener('mousedown', this.handleOutsideClick, false);
}
componentWillUnmount() {
document.removeEventListener('mousedown', this.handleOutsideClick, false);
2019-01-15 04:23:46 +08:00
}
handleDismiss() {
2023-07-31 22:24:25 +08:00
const { modalHide, dismiss } = this.props;
if (!dismiss || !modalHide) return;
2019-06-22 01:39:12 +08:00
modalHide(dismiss.callback);
2019-01-15 04:23:46 +08:00
}
2023-07-31 22:24:25 +08:00
handleRequestClose(event) {
const { onRequestClose } = this.props;
const closeModal = onRequestClose || this.handleDismiss;
closeModal();
if (event && event.type === 'click') {
setTimeout(() => {
if (document.activeElement) {
document.activeElement.blur();
}
}, 0);
}
}
handleOutsideClick(e) {
const { modalisOpen } = this.props;
if (this.modalRef.current && !this.modalRef.current.contains(e.target) && modalisOpen) {
this.handleRequestClose(e);
}
}
render() {
const {
2022-11-24 22:53:30 +08:00
id,
2019-05-17 06:13:08 +08:00
intl,
title,
hideBorder,
dismiss,
2017-04-19 03:06:51 +08:00
className,
modalisOpen,
2019-01-11 06:58:42 +08:00
onRequestClose,
2019-02-27 01:40:01 +08:00
shouldShowCloseButton,
contentLabel,
headerPosition,
'data-test': dataTest,
2023-07-31 22:24:25 +08:00
children,
2017-10-11 06:08:51 +08:00
...otherProps
} = this.props;
return (
2021-11-11 03:10:35 +08:00
<Styled.SimpleModal
2023-07-31 22:24:25 +08:00
id={id || 'simpleModal'}
2017-04-19 03:06:51 +08:00
isOpen={modalisOpen}
2021-11-11 03:10:35 +08:00
className={className}
2023-07-31 22:24:25 +08:00
onRequestClose={this.handleRequestClose}
contentLabel={title || contentLabel}
dataTest={dataTest}
2017-04-19 03:06:51 +08:00
{...otherProps}
>
2023-07-31 22:24:25 +08:00
<FocusTrap active={modalisOpen} focusTrapOptions={{ initialFocus: false }}>
<div ref={this.modalRef}>
<Styled.Header
hideBorder={hideBorder}
headerPosition={headerPosition}
shouldShowCloseButton={shouldShowCloseButton}
modalDismissDescription={intl.formatMessage(intlMessages.modalCloseDescription)}
closeButtonProps={{
label: intl.formatMessage(intlMessages.modalClose),
'aria-label': `${intl.formatMessage(intlMessages.modalClose)} ${title || contentLabel}`,
onClick: this.handleRequestClose,
}}
>
{title || ''}
</Styled.Header>
<Styled.Content>
{children}
</Styled.Content>
</div>
</FocusTrap>
2021-11-11 03:10:35 +08:00
</Styled.SimpleModal>
);
}
2017-10-11 06:08:51 +08:00
}
ModalSimple.propTypes = propTypes;
ModalSimple.defaultProps = defaultProps;
export default injectIntl(ModalSimple);