make click outside of lightbox close the lightbox

pull/1969/head
Audric Ackermann 4 years ago
parent 6bd8b3695a
commit b05afc7c3f
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -196,12 +196,12 @@ const Icon = ({
export const LightboxObject = ({ export const LightboxObject = ({
objectURL, objectURL,
contentType, contentType,
videoRef, renderedRef,
onObjectClick, onObjectClick,
}: { }: {
objectURL: string; objectURL: string;
contentType: MIME.MIMEType; contentType: MIME.MIMEType;
videoRef: React.MutableRefObject<any>; renderedRef: React.MutableRefObject<any>;
onObjectClick: (event: any) => any; onObjectClick: (event: any) => any;
}) => { }) => {
const { urlToLoad } = useEncryptedFileFetch(objectURL, contentType); const { urlToLoad } = useEncryptedFileFetch(objectURL, contentType);
@ -215,10 +215,10 @@ export const LightboxObject = ({
// auto play video on showing a video attachment // auto play video on showing a video attachment
useUnmount(() => { useUnmount(() => {
if (!videoRef?.current) { if (!renderedRef?.current) {
return; return;
} }
videoRef.current.pause.pause(); renderedRef.current.pause.pause();
}); });
if (isImageTypeSupported) { if (isImageTypeSupported) {
@ -228,6 +228,7 @@ export const LightboxObject = ({
onDragStart={onDragStart} onDragStart={onDragStart}
alt={window.i18n('lightboxImageAlt')} alt={window.i18n('lightboxImageAlt')}
src={urlToLoad} src={urlToLoad}
ref={renderedRef}
/> />
); );
} }
@ -235,15 +236,15 @@ export const LightboxObject = ({
const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType); const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType);
if (isVideoTypeSupported) { if (isVideoTypeSupported) {
if (urlToLoad) { if (urlToLoad) {
if (videoRef?.current?.paused) { if (renderedRef?.current?.paused) {
void videoRef?.current?.play(); void renderedRef?.current?.play();
} }
} }
return ( return (
<video <video
role="button" role="button"
ref={videoRef} ref={renderedRef}
controls={true} controls={true}
style={styles.object as any} style={styles.object as any}
key={urlToLoad} key={urlToLoad}
@ -268,8 +269,7 @@ export const LightboxObject = ({
}; };
export const Lightbox = (props: Props) => { export const Lightbox = (props: Props) => {
const videoRef = useRef<any>(null); const renderedRef = useRef<any>(null);
const containerRef = useRef<HTMLDivElement>(null);
const dispatch = useDispatch(); const dispatch = useDispatch();
const { caption, contentType, objectURL, onNext, onPrevious, onSave } = props; const { caption, contentType, objectURL, onNext, onPrevious, onSave } = props;
@ -279,28 +279,24 @@ export const Lightbox = (props: Props) => {
}; };
const onContainerClick = (event: React.MouseEvent<HTMLDivElement>) => { const onContainerClick = (event: React.MouseEvent<HTMLDivElement>) => {
if (containerRef && event.target !== containerRef.current) { debugger;
if (renderedRef && event.target === renderedRef.current) {
return; return;
} }
dispatch(showLightBox(undefined)); dispatch(showLightBox(undefined));
}; };
return ( return (
<div style={styles.container as any} role="dialog"> <div style={styles.container as any} role="dialog" onClick={onContainerClick}>
<div style={styles.mainContainer as any}> <div style={styles.mainContainer as any}>
<div style={styles.controlsOffsetPlaceholder} /> <div style={styles.controlsOffsetPlaceholder} />
<div <div style={styles.objectParentContainer} role="button">
style={styles.objectParentContainer}
onClick={onContainerClick}
ref={containerRef}
role="button"
>
<div style={styles.objectContainer as any}> <div style={styles.objectContainer as any}>
{!is.undefined(contentType) ? ( {!is.undefined(contentType) ? (
<LightboxObject <LightboxObject
objectURL={objectURL} objectURL={objectURL}
contentType={contentType} contentType={contentType}
videoRef={videoRef} renderedRef={renderedRef}
onObjectClick={onObjectClick} onObjectClick={onObjectClick}
/> />
) : null} ) : null}

Loading…
Cancel
Save