From d814f194d42873cb977fafbcd7c6100eb91b1397 Mon Sep 17 00:00:00 2001 From: simonhaenisch Date: Fri, 7 Aug 2020 18:08:11 +0200 Subject: [PATCH] feat: add goToPreviousRoute --- readme.md | 1 + src/ionic/index.ts | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 2c2a097..f096b2f 100644 --- a/readme.md +++ b/readme.md @@ -81,6 +81,7 @@ setMomentReference(moment); * `showConfirmationAlert(question, options)`: Show a confirmation alert. * `viewportIsMin(size)`: Check whether the viewport is at least of specified size. * `goToRoute(url)`: Go to a route programmatically. +* `goToPreviousRoute()`: Go back to the previous route in the history. * `changeRouteHash(hash)`: Change the hash of the current route without adding a new state to the browser history. ### Strings (`@jitbug/helpers/strings`) diff --git a/src/ionic/index.ts b/src/ionic/index.ts index cba8270..d20ca64 100644 --- a/src/ionic/index.ts +++ b/src/ionic/index.ts @@ -195,6 +195,21 @@ const breakpoints = { * Go to a route programmatically. */ export const goToRoute: typeof HTMLIonRouterElement.prototype.push = async (...args) => { + const router = await waitForRouter(); + + return router.push(...args); +}; + +/** + * Go back to the previous route in `window.history`. + */ +export const goToPreviousRoute = async () => { + const router = await waitForRouter(); + + return router.back(); +}; + +const waitForRouter = async () => { let router: HTMLIonRouterElement | null = null; while (!router) { @@ -202,7 +217,7 @@ export const goToRoute: typeof HTMLIonRouterElement.prototype.push = async (...a await wait(10); } - return router.push(...args); + return router; }; /**