mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
Merge pull request #65 from muerwre/task/#62
fixed scroll bugs on photoswipe open
This commit is contained in:
commit
77e7bf138e
2 changed files with 27 additions and 16 deletions
|
@ -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 { connect } from 'react-redux';
|
||||||
|
|
||||||
import PhotoSwipeJs from 'photoswipe/dist/photoswipe.js';
|
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 * as MODAL_ACTIONS from '~/redux/modal/actions';
|
||||||
import styles from './styles.module.scss';
|
import styles from './styles.module.scss';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import { useBlockBackButton } from '~/utils/hooks/useBlockBackButton';
|
||||||
|
|
||||||
const mapStateToProps = (state: IState) => ({
|
const mapStateToProps = (state: IState) => ({
|
||||||
photoswipe: selectModal(state).photoswipe,
|
photoswipe: selectModal(state).photoswipe,
|
||||||
|
@ -67,21 +68,7 @@ const PhotoSwipeUnconnected: FC<Props> = ({ photoswipe, modalSetShown }) => {
|
||||||
});
|
});
|
||||||
}, [photoswipe.images, photoswipe.index]);
|
}, [photoswipe.images, photoswipe.index]);
|
||||||
|
|
||||||
const closeOnHashChange = useCallback(() => {
|
useBlockBackButton(closeModal);
|
||||||
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 (
|
return (
|
||||||
<div className="pswp" tabIndex={-1} role="dialog" aria-hidden="true" ref={ref}>
|
<div className="pswp" tabIndex={-1} role="dialog" aria-hidden="true" ref={ref}>
|
||||||
|
|
24
src/utils/hooks/useBlockBackButton.ts
Normal file
24
src/utils/hooks/useBlockBackButton.ts
Normal 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]
|
||||||
|
);
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue