Skip to content

Commit

Permalink
fix(vue): only remount with delta values
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi committed Sep 21, 2021
1 parent 53d64c1 commit 623cdd7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/vue-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export const createIonRouter = (opts: IonicVueRouterOptions, router: Router) =>
}

routeInfo.position = currentHistoryPosition;
routeInfo.delta = delta;
const historySize = locationHistory.size();
const historyDiff = currentHistoryPosition - initialHistoryPosition;

Expand Down
1 change: 1 addition & 0 deletions packages/vue-router/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RouteInfo {
pushedByRoute?: string;
tab?: string;
position?: number;
delta?: number;
}

export interface RouteParams {
Expand Down
21 changes: 20 additions & 1 deletion packages/vue-router/src/viewStacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,34 @@ export const createViewStacks = (router: Router) => {
* developers to step forward over multiple views.
* The intermediary views need to be remounted so that
* swipe to go back works properly.
* We need to account for the delta value here too because
* we do not want to remount an unrelated view.
* Example:
* /home --> /page2 --> router.back() --> /page3
* Going to /page3 would remount /page2 since we do
* not prune /page2 from the stack. However, /page2
* needs to remain in the stack.
* Example:
* /home --> /page2 --> /page3 --> router.go(-2) --> router.go(2)
* We would end up on /page3, but users need to be able to swipe
* to go back to /page2 and /home, so we need both pages mounted
* in the DOM.
*/
const mountIntermediaryViews = (outletId: number, enteringViewItem: ViewItem, leavingViewItem: ViewItem) => {
const mountIntermediaryViews = (outletId: number, enteringViewItem: ViewItem, leavingViewItem: ViewItem, delta: number = 1) => {
const viewStack = viewStacks[outletId];
if (!viewStack) return;

const { enteringIndex: endIndex, leavingIndex: startIndex } = findViewIndex(viewStack, enteringViewItem, leavingViewItem);
let mountDiff = delta - 1;

for (let i = startIndex + 1; i < endIndex; i++) {
viewStack[i].mount = true;

mountDiff -= 1;

if (mountDiff === 0) {
return;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/components/IonRouterOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const IonRouterOutlet = defineComponent({

const handlePageTransition = async () => {
const routeInfo = ionRouter.getCurrentRouteInfo();
const { routerDirection, routerAction, routerAnimation, prevRouteLastPathname } = routeInfo;
const { routerDirection, routerAction, routerAnimation, prevRouteLastPathname, delta } = routeInfo;

const enteringViewItem = viewStacks.findViewItemByRouteInfo(routeInfo, id, usingDeprecatedRouteSetup);
let leavingViewItem = viewStacks.findLeavingViewItemByRouteInfo(routeInfo, id, true, usingDeprecatedRouteSetup);
Expand Down Expand Up @@ -289,7 +289,7 @@ See https://ionicframework.com/docs/vue/navigation#ionpage for more information.
viewStacks.unmountLeavingViews(id, enteringViewItem, leavingViewItem);
}
} else {
viewStacks.mountIntermediaryViews(id, enteringViewItem, leavingViewItem);
viewStacks.mountIntermediaryViews(id, enteringViewItem, leavingViewItem, delta);
}

fireLifecycle(leavingViewItem.vueComponent, leavingViewItem.vueComponentRef, LIFECYCLE_DID_LEAVE);
Expand Down

0 comments on commit 623cdd7

Please sign in to comment.