Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(v5): pr6325 #6393

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/vkui/src/components/View/View.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ describe('View', () => {
expect(document.getElementById('p1')).toBeTruthy();
expect(document.getElementById('p2')).toBeNull();
});
it('restores scroll after swipeBack', () => {

it('restores scroll after cancelled swipeBack (mouse up during the move)', () => {
let y = 101;
scrollsCache['scroll']['p1'] = 22;
const [MockScroll, scrollTo] = mockScrollContext(() => y);
Expand All @@ -268,7 +269,21 @@ describe('View', () => {
});
fireEvent.mouseUp(view);
rerender(<SwipeBack activePanel="p1" history={['p1']} />);
expect(scrollTo).toBeCalledWith(0, 22);
expect(scrollTo).toHaveBeenCalledWith(0, 22);
});

it('restores scroll when swipeBack cancelled because user moves panel back to starting point', () => {
const currentScrollPosition = 22;
const startPosition = { clientX: 0, clientY: 100 };
const [MockScroll, scrollTo] = mockScrollContext(() => currentScrollPosition);
const { view, rerender, SwipeBack } = setupSwipeBack({ Wrapper: MockScroll });
fireEvent.mouseDown(view, startPosition);
fireEvent.mouseMove(view, { clientX: SWIPE_BACK_SHIFT_THRESHOLD, clientY: 100 });
fireEvent.mouseMove(view, startPosition);
fireEvent.mouseUp(view);

rerender(<SwipeBack activePanel="p2" history={['p2']} />);
expect(scrollTo).toHaveBeenCalledWith(0, currentScrollPosition);
});

describe('horizontal scrollable elements', () => {
Expand Down
33 changes: 28 additions & 5 deletions packages/vkui/src/components/View/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const View = ({
const prevSwipingBack = usePrevious(swipingBack);
const prevBrowserSwipe = usePrevious(browserSwipe);
const prevSwipeBackResult = usePrevious(swipeBackResult);
const prevSwipeBackShift = usePrevious(swipeBackShift);
const prevSwipeBackPrevPanel = usePrevious(swipeBackPrevPanel);
const prevOnTransition = usePrevious(onTransition);

Expand Down Expand Up @@ -455,11 +456,6 @@ export const View = ({
);
}

// Если свайп назад отменился (когда пользователь недостаточно сильно свайпнул)
if (prevSwipeBackResult === SwipeBackResults.fail && !swipeBackResult && activePanel !== null) {
scroll?.scrollTo(0, scrolls.current[activePanel]);
}

// Закончился Safari свайп
if (prevActivePanel !== activePanelProp && browserSwipe) {
setBrowserSwipe(false);
Expand Down Expand Up @@ -492,6 +488,33 @@ export const View = ({
waitTransitionFinish,
]);

React.useEffect(
function restoreScrollPositionWhenSwipeBackIsCancelled() {
// Если свайп назад отменился (когда пользователь недостаточно сильно свайпнул)
const swipeBackCancelledInTheMiddleOfAction =
prevSwipeBackResult === SwipeBackResults.fail && !swipeBackResult;
const swipeBackCancelledByMovingPanelBackToInitialPoint =
prevSwipingBack && !swipingBack && prevSwipeBackShift === 0;

if (
(swipeBackCancelledInTheMiddleOfAction ||
swipeBackCancelledByMovingPanelBackToInitialPoint) &&
activePanel !== null
) {
scroll?.scrollTo(0, scrolls.current[activePanel]);
}
},
[
prevSwipeBackResult,
swipeBackResult,
prevSwipingBack,
swipingBack,
prevSwipeBackShift,
activePanel,
scroll,
],
);

return (
<NavViewIdContext.Provider value={id}>
<Touch
Expand Down
Loading