From 49efc452d3eaa6bfed4ddb80e59a2f294f3d711a Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Fri, 26 Jun 2020 11:33:10 +0700 Subject: [PATCH] closing modal on back button --- src/containers/dialogs/PhotoSwipe/index.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/containers/dialogs/PhotoSwipe/index.tsx b/src/containers/dialogs/PhotoSwipe/index.tsx index 46c00c04..ec6bfdba 100644 --- a/src/containers/dialogs/PhotoSwipe/index.tsx +++ b/src/containers/dialogs/PhotoSwipe/index.tsx @@ -67,6 +67,20 @@ const PhotoSwipeUnconnected: FC = ({ photoswipe, modalSetShown }) => { }); }, [photoswipe.images, photoswipe.index]); + const closeOnHashChange = useCallback(() => { + if (window.location.hash !== '#preview') return closeModal(); + }, [closeModal]); + + useEffect(() => { + window.addEventListener('hashchange', closeOnHashChange); + return () => window.removeEventListener('hashchange', closeOnHashChange); + }, [closeOnHashChange]); + + useEffect(() => { + window.location.hash = 'preview'; + return () => (window.location.hash = ''); + }, []); + return (