session-desktop/ts/components/dialog/SessionModal.tsx

132 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-12-18 01:50:19 +01:00
import React from 'react';
2020-01-22 04:09:34 +01:00
import classNames from 'classnames';
2019-12-18 01:50:19 +01:00
import { SessionIconButton, SessionIconType } from '../session/icon';
2021-08-06 03:09:14 +02:00
import { SessionButtonColor, SessionButtonType } from '../session/SessionButton';
2019-12-18 01:50:19 +01:00
interface Props {
title: string;
onClose: any;
2019-12-30 07:38:28 +01:00
showExitIcon?: boolean;
showHeader?: boolean;
2020-01-22 04:09:34 +01:00
headerReverse?: boolean;
2020-01-22 04:28:29 +01:00
//Maximum of two icons or buttons in header
headerIconButtons?: Array<{
2020-01-22 04:09:34 +01:00
iconType: SessionIconType;
iconRotation: number;
onClick?: any;
}>;
headerButtons?: Array<{
buttonType: SessionButtonType;
buttonColor: SessionButtonColor;
text: string;
onClick?: any;
}>;
2019-12-18 01:50:19 +01:00
}
interface State {
isVisible: boolean;
}
2020-01-03 01:48:55 +01:00
export class SessionModal extends React.PureComponent<Props, State> {
2019-12-30 07:38:28 +01:00
public static defaultProps = {
showExitIcon: true,
showHeader: true,
2020-01-22 04:09:34 +01:00
headerReverse: false,
2019-12-30 07:38:28 +01:00
};
2020-01-03 01:48:55 +01:00
private node: HTMLDivElement | null;
2019-12-18 01:50:19 +01:00
constructor(props: any) {
super(props);
this.state = {
isVisible: true,
};
this.close = this.close.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.node = null;
2019-12-18 01:50:19 +01:00
}
public componentDidMount() {
window.addEventListener('keyup', this.onKeyUp);
document.addEventListener('mousedown', this.handleClick, false);
}
public componentWillUnmount() {
window.removeEventListener('keyup', this.onKeyUp);
document.removeEventListener('mousedown', this.handleClick, false);
}
public handleClick = (e: any) => {
if (this.node && this.node.contains(e.target)) {
return;
}
this.close();
};
2019-12-18 01:50:19 +01:00
public render() {
2021-04-22 10:03:58 +02:00
const { title, headerIconButtons, showExitIcon, showHeader, headerReverse } = this.props;
const { isVisible } = this.state;
return isVisible ? (
<div ref={node => (this.node = node)} className={'session-modal'}>
2020-01-03 01:48:55 +01:00
{showHeader ? (
2019-12-30 07:38:28 +01:00
<>
2021-04-22 10:03:58 +02:00
<div className={classNames('session-modal__header', headerReverse && 'reverse')}>
2019-12-30 07:38:28 +01:00
<div className="session-modal__header__close">
{showExitIcon ? (
<SessionIconButton iconType="exit" iconSize="small" onClick={this.close} />
2020-01-03 01:48:55 +01:00
) : null}
2019-12-30 07:38:28 +01:00
</div>
<div className="session-modal__header__title">{title}</div>
<div className="session-modal__header__icons">
{headerIconButtons
? headerIconButtons.map((iconItem: any) => {
return (
<SessionIconButton
2020-01-22 04:09:34 +01:00
key={iconItem.iconType}
iconType={iconItem.iconType}
iconSize={'large'}
2020-01-22 04:09:34 +01:00
iconRotation={iconItem.iconRotation}
onClick={iconItem.onClick}
2019-12-30 07:38:28 +01:00
/>
);
})
: null}
</div>
</div>
</>
2020-01-03 01:48:55 +01:00
) : null}
2019-12-22 23:44:18 +01:00
<div className="session-modal__body">{this.props.children}</div>
</div>
) : null;
}
public close() {
this.setState({
isVisible: false,
});
document.removeEventListener('mousedown', this.handleClick, false);
if (this.props.onClose) {
this.props.onClose();
}
}
2019-12-22 23:44:18 +01:00
public onKeyUp(event: any) {
switch (event.key) {
case 'Esc':
case 'Escape':
this.close();
break;
default:
}
2019-12-18 01:50:19 +01:00
}
}