From 142236640ea54e71355bd540eab655c27432eabf Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Sun, 15 Apr 2018 01:48:21 -0400 Subject: [PATCH] Click lightbox to close --- ts/components/Lightbox.tsx | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/ts/components/Lightbox.tsx b/ts/components/Lightbox.tsx index 3a158a37a..e44572328 100644 --- a/ts/components/Lightbox.tsx +++ b/ts/components/Lightbox.tsx @@ -52,6 +52,8 @@ const IconButton = ({ onClick, type }: IconButtonProps) => ( ); export class Lightbox extends React.Component { + private containerRef: HTMLDivElement | null = null; + public componentDidMount() { const useCapture = true; document.addEventListener('keyup', this.onKeyUp, useCapture); @@ -65,12 +67,12 @@ export class Lightbox extends React.Component { public render() { const { imageURL } = this.props; return ( -
+
- +
- + @@ -79,11 +81,31 @@ export class Lightbox extends React.Component { ); } + private setContainerRef = (value: HTMLDivElement) => { + this.containerRef = value; + } + + private onClose = () => { + this.props.close(); + }; + private onKeyUp = (event: KeyboardEvent) => { if (event.key !== 'Escape') { return; } - this.props.close(); + this.onClose(); }; + + private onContainerClick = (event: React.MouseEvent) => { + if (event.target !== this.containerRef) { + return; + } + this.onClose(); + } + + private onImageClick = (event: React.MouseEvent) => { + event.stopPropagation(); + this.onClose(); + } }