signal-desktop/ts/components/Lightbox.tsx

225 lines
5 KiB
TypeScript
Raw Normal View History

2018-04-14 23:27:03 -04:00
/**
* @prettier
*/
import React from 'react';
2018-04-15 00:27:30 -04:00
import classNames from 'classnames';
2018-04-25 18:15:57 -04:00
import is from '@sindresorhus/is';
import * as GoogleChrome from '../util/GoogleChrome';
import * as MIME from '../types/MIME';
2018-04-14 23:27:03 -04:00
interface Props {
close: () => void;
2018-04-25 18:15:57 -04:00
objectURL: string;
contentType: MIME.MIMEType | undefined;
onNext?: () => void;
onPrevious?: () => void;
2018-04-25 18:15:57 -04:00
onSave?: () => void;
2018-04-14 23:27:03 -04:00
}
const styles = {
container: {
2018-04-15 00:27:30 -04:00
display: 'flex',
2018-04-26 11:50:54 -04:00
flexDirection: 'column',
2018-04-15 00:27:30 -04:00
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.9)',
2018-04-26 11:50:54 -04:00
} as React.CSSProperties,
mainContainer: {
display: 'flex',
flexDirection: 'row',
paddingTop: 40,
paddingLeft: 40,
paddingRight: 40,
paddingBottom: 0,
2018-04-15 00:27:30 -04:00
} as React.CSSProperties,
objectContainer: {
flexGrow: 1,
2018-04-15 00:27:30 -04:00
display: 'inline-flex',
justifyContent: 'center',
} as React.CSSProperties,
2018-04-14 23:27:03 -04:00
image: {
2018-04-15 00:27:30 -04:00
flexGrow: 1,
flexShrink: 0,
2018-04-14 23:27:03 -04:00
maxWidth: '100%',
maxHeight: '100%',
2018-04-15 00:27:30 -04:00
objectFit: 'contain',
} as React.CSSProperties,
controls: {
flexShrink: 0,
display: 'flex',
flexDirection: 'column',
marginLeft: 10,
2018-04-15 00:57:12 -04:00
} as React.CSSProperties,
2018-04-26 11:50:54 -04:00
navigationContainer: {
flexShrink: 0,
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
} as React.CSSProperties,
saveButton: {
marginTop: 10,
},
iconButtonPlaceholder: {
// Dimensions match `.iconButton`:
display: 'inline-block',
width: 50,
height: 50,
},
2018-04-14 23:27:03 -04:00
};
2018-04-15 00:27:30 -04:00
interface IconButtonProps {
onClick?: () => void;
2018-04-26 11:50:54 -04:00
style?: React.CSSProperties;
type: 'save' | 'close' | 'previous' | 'next';
2018-04-15 00:27:30 -04:00
}
2018-04-26 11:50:54 -04:00
const IconButton = ({ onClick, style, type }: IconButtonProps) => {
const clickHandler = (event: React.MouseEvent<HTMLAnchorElement>): void => {
event.preventDefault();
if (!onClick) {
return;
}
onClick();
};
return (
<a
href="#"
onClick={clickHandler}
className={classNames('iconButton', type)}
2018-04-26 11:50:54 -04:00
style={style}
/>
);
};
2018-04-15 00:27:30 -04:00
2018-04-26 11:50:54 -04:00
const IconButtonPlaceholder = () => (
<div style={styles.iconButtonPlaceholder} />
);
2018-04-14 23:27:03 -04:00
export class Lightbox extends React.Component<Props, {}> {
2018-04-15 02:53:19 -04:00
private containerRef: HTMLDivElement | null = null;
2018-04-15 00:50:18 -04:00
public componentDidMount() {
const useCapture = true;
document.addEventListener('keyup', this.onKeyUp, useCapture);
}
2018-04-24 12:08:18 -04:00
public componentWillUnmount() {
2018-04-15 00:50:18 -04:00
const useCapture = true;
document.removeEventListener('keyup', this.onKeyUp, useCapture);
}
2018-04-14 23:27:03 -04:00
public render() {
2018-04-25 18:15:57 -04:00
const { contentType, objectURL } = this.props;
2018-04-14 23:27:03 -04:00
return (
<div
style={styles.container}
onClick={this.onContainerClick}
ref={this.setContainerRef}
>
2018-04-26 11:50:54 -04:00
<div style={styles.mainContainer}>
<div style={styles.objectContainer}>
{!is.undefined(contentType)
? this.renderObject({ objectURL, contentType })
: null}
</div>
<div style={styles.controls}>
<IconButton type="close" onClick={this.onClose} />
{this.props.onSave ? (
<IconButton
type="save"
onClick={this.props.onSave}
style={styles.saveButton}
/>
) : null}
</div>
2018-04-15 00:27:30 -04:00
</div>
2018-04-26 11:50:54 -04:00
<div style={styles.navigationContainer}>
{this.props.onPrevious ? (
<IconButton type="previous" onClick={this.props.onPrevious} />
2018-04-26 11:50:54 -04:00
) : (
<IconButtonPlaceholder />
)}
{this.props.onNext ? (
<IconButton type="next" onClick={this.props.onNext} />
2018-04-26 11:50:54 -04:00
) : (
<IconButtonPlaceholder />
)}
2018-04-15 00:27:30 -04:00
</div>
2018-04-14 23:27:03 -04:00
</div>
);
}
2018-04-15 00:50:18 -04:00
2018-04-25 18:15:57 -04:00
private renderObject = ({
objectURL,
contentType,
}: {
objectURL: string;
contentType: MIME.MIMEType;
}) => {
const isImage = GoogleChrome.isImageTypeSupported(contentType);
if (isImage) {
return (
<img
style={styles.image}
src={objectURL}
onClick={this.onObjectClick}
/>
);
}
const isVideo = GoogleChrome.isVideoTypeSupported(contentType);
if (isVideo) {
return (
<video controls>
<source src={objectURL} />
</video>
);
}
// tslint:disable-next-line no-console
console.log('Lightbox: Unexpected content type', { contentType });
return null;
};
2018-04-15 01:48:21 -04:00
private setContainerRef = (value: HTMLDivElement) => {
this.containerRef = value;
};
2018-04-15 01:48:21 -04:00
private onClose = () => {
2018-04-24 16:12:11 -04:00
const { close } = this.props;
if (!close) {
return;
}
close();
2018-04-15 01:48:21 -04:00
};
2018-04-15 00:50:18 -04:00
private onKeyUp = (event: KeyboardEvent) => {
if (event.key !== 'Escape') {
return;
}
2018-04-15 01:48:21 -04:00
this.onClose();
2018-04-15 00:57:12 -04:00
};
2018-04-15 01:48:21 -04:00
private onContainerClick = (event: React.MouseEvent<HTMLDivElement>) => {
if (event.target !== this.containerRef) {
return;
}
this.onClose();
};
2018-04-15 01:48:21 -04:00
2018-04-25 18:15:57 -04:00
private onObjectClick = (event: React.MouseEvent<HTMLImageElement>) => {
2018-04-15 01:48:21 -04:00
event.stopPropagation();
this.onClose();
};
2018-04-14 23:27:03 -04:00
}