1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

fixed scroll bugs on photoswipe open

This commit is contained in:
Fedor Katurov 2021-05-11 12:06:41 +07:00
parent 05f09c382c
commit f87a7882f4
2 changed files with 27 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import React, { FC, useRef, useEffect, useMemo, useCallback } from 'react';
import React, { FC, useCallback, useEffect, useRef } from 'react';
import { connect } from 'react-redux';
import PhotoSwipeJs from 'photoswipe/dist/photoswipe.js';
@ -12,6 +12,7 @@ import { PRESETS } from '~/constants/urls';
import * as MODAL_ACTIONS from '~/redux/modal/actions';
import styles from './styles.module.scss';
import classNames from 'classnames';
import { useBlockBackButton } from '~/utils/hooks/useBlockBackButton';
const mapStateToProps = (state: IState) => ({
photoswipe: selectModal(state).photoswipe,
@ -67,21 +68,7 @@ const PhotoSwipeUnconnected: FC<Props> = ({ 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 = '';
};
}, []);
useBlockBackButton(closeModal);
return (
<div className="pswp" tabIndex={-1} role="dialog" aria-hidden="true" ref={ref}>

View file

@ -0,0 +1,24 @@
import { useEffect } from 'react';
import { history } from '~/redux/store';
/**
* useBlockBackButton - blocks back navigation and calls {callback}
* @param callback
*/
export const useBlockBackButton = (callback?: () => void) => {
useEffect(
() =>
history.listen((newLocation, action) => {
if (action !== 'POP') {
return;
}
history.goForward();
if (callback) {
callback();
}
}),
[callback, history]
);
};