2018-04-15 03:27:03 +00:00
|
|
|
/**
|
|
|
|
* @prettier
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
|
2018-04-15 04:27:30 +00:00
|
|
|
import classNames from 'classnames';
|
2018-04-15 03:27:03 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
close: () => void;
|
2018-04-15 05:46:42 +00:00
|
|
|
imageURL?: string;
|
2018-04-15 05:48:43 +00:00
|
|
|
onNext?: () => void;
|
|
|
|
onPrevious?: () => void;
|
2018-04-15 05:46:42 +00:00
|
|
|
onSave: () => void;
|
2018-04-15 05:48:43 +00:00
|
|
|
shouldShowNextButton: boolean;
|
|
|
|
shouldShowPreviousButton: boolean;
|
2018-04-15 06:27:34 +00:00
|
|
|
shouldShowSaveAsButton: boolean;
|
2018-04-15 03:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = {
|
|
|
|
container: {
|
2018-04-15 04:27:30 +00:00
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.9)',
|
|
|
|
padding: 40,
|
|
|
|
} as React.CSSProperties,
|
|
|
|
objectContainer: {
|
2018-04-15 05:30:42 +00:00
|
|
|
flexGrow: 1,
|
2018-04-15 04:27:30 +00:00
|
|
|
display: 'inline-flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
} as React.CSSProperties,
|
2018-04-15 03:27:03 +00:00
|
|
|
image: {
|
2018-04-15 04:27:30 +00:00
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 0,
|
2018-04-15 03:27:03 +00:00
|
|
|
maxWidth: '100%',
|
|
|
|
maxHeight: '100%',
|
2018-04-15 04:27:30 +00:00
|
|
|
objectFit: 'contain',
|
|
|
|
} as React.CSSProperties,
|
|
|
|
controls: {
|
|
|
|
flexShrink: 0,
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
marginLeft: 10,
|
2018-04-15 04:57:12 +00:00
|
|
|
} as React.CSSProperties,
|
2018-04-15 03:27:03 +00:00
|
|
|
};
|
|
|
|
|
2018-04-15 04:27:30 +00:00
|
|
|
interface IconButtonProps {
|
2018-04-15 05:19:46 +00:00
|
|
|
type: 'save' | 'close' | 'previous' | 'next';
|
2018-04-15 04:27:30 +00:00
|
|
|
onClick?: () => void;
|
|
|
|
}
|
|
|
|
const IconButton = ({ onClick, type }: IconButtonProps) => (
|
2018-04-15 04:57:12 +00:00
|
|
|
<a href="#" onClick={onClick} className={classNames('iconButton', type)} />
|
2018-04-15 04:27:30 +00:00
|
|
|
);
|
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
export class Lightbox extends React.Component<Props, {}> {
|
2018-04-15 05:48:43 +00:00
|
|
|
public static defaultProps: Partial<Props> = {
|
|
|
|
shouldShowNextButton: false,
|
|
|
|
shouldShowPreviousButton: false,
|
2018-04-15 06:27:34 +00:00
|
|
|
shouldShowSaveAsButton: false,
|
2018-04-15 05:48:43 +00:00
|
|
|
};
|
|
|
|
|
2018-04-15 06:53:19 +00:00
|
|
|
private containerRef: HTMLDivElement | null = null;
|
|
|
|
|
2018-04-15 04:50:18 +00:00
|
|
|
public componentDidMount() {
|
|
|
|
const useCapture = true;
|
|
|
|
document.addEventListener('keyup', this.onKeyUp, useCapture);
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillMount() {
|
|
|
|
const useCapture = true;
|
|
|
|
document.removeEventListener('keyup', this.onKeyUp, useCapture);
|
|
|
|
}
|
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
public render() {
|
2018-04-15 05:48:43 +00:00
|
|
|
const {
|
|
|
|
imageURL,
|
|
|
|
shouldShowNextButton,
|
|
|
|
shouldShowPreviousButton,
|
2018-04-15 06:27:34 +00:00
|
|
|
shouldShowSaveAsButton,
|
2018-04-15 05:48:43 +00:00
|
|
|
} = this.props;
|
2018-04-15 03:27:03 +00:00
|
|
|
return (
|
2018-04-15 05:48:43 +00:00
|
|
|
<div
|
|
|
|
style={styles.container}
|
|
|
|
onClick={this.onContainerClick}
|
|
|
|
ref={this.setContainerRef}
|
|
|
|
>
|
2018-04-15 04:27:30 +00:00
|
|
|
<div style={styles.objectContainer}>
|
2018-04-15 05:48:43 +00:00
|
|
|
<img
|
|
|
|
style={styles.image}
|
|
|
|
src={imageURL}
|
|
|
|
onClick={this.onImageClick}
|
|
|
|
/>
|
2018-04-15 04:27:30 +00:00
|
|
|
</div>
|
|
|
|
<div style={styles.controls}>
|
2018-04-15 05:48:21 +00:00
|
|
|
<IconButton type="close" onClick={this.onClose} />
|
2018-04-15 06:27:34 +00:00
|
|
|
{shouldShowSaveAsButton ? (
|
|
|
|
<IconButton type="save" onClick={this.props.onSave} />
|
|
|
|
) : null}
|
2018-04-15 05:48:43 +00:00
|
|
|
{shouldShowPreviousButton ? (
|
|
|
|
<IconButton type="previous" onClick={this.props.onPrevious} />
|
|
|
|
) : null}
|
|
|
|
{shouldShowNextButton ? (
|
|
|
|
<IconButton type="next" onClick={this.props.onNext} />
|
|
|
|
) : null}
|
2018-04-15 04:27:30 +00:00
|
|
|
</div>
|
2018-04-15 03:27:03 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-04-15 04:50:18 +00:00
|
|
|
|
2018-04-15 05:48:21 +00:00
|
|
|
private setContainerRef = (value: HTMLDivElement) => {
|
|
|
|
this.containerRef = value;
|
2018-04-15 05:48:43 +00:00
|
|
|
};
|
2018-04-15 05:48:21 +00:00
|
|
|
|
|
|
|
private onClose = () => {
|
|
|
|
this.props.close();
|
|
|
|
};
|
|
|
|
|
2018-04-15 04:50:18 +00:00
|
|
|
private onKeyUp = (event: KeyboardEvent) => {
|
|
|
|
if (event.key !== 'Escape') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-15 05:48:21 +00:00
|
|
|
this.onClose();
|
2018-04-15 04:57:12 +00:00
|
|
|
};
|
2018-04-15 05:48:21 +00:00
|
|
|
|
|
|
|
private onContainerClick = (event: React.MouseEvent<HTMLDivElement>) => {
|
|
|
|
if (event.target !== this.containerRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.onClose();
|
2018-04-15 05:48:43 +00:00
|
|
|
};
|
2018-04-15 05:48:21 +00:00
|
|
|
|
|
|
|
private onImageClick = (event: React.MouseEvent<HTMLImageElement>) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
this.onClose();
|
2018-04-15 05:48:43 +00:00
|
|
|
};
|
2018-04-15 03:27:03 +00:00
|
|
|
}
|