Skip to content

Commit

Permalink
feat: add goToPreviousRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Aug 7, 2020
1 parent 9d76109 commit d814f19
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
17 changes: 16 additions & 1 deletion src/ionic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,29 @@ 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) {
router = document.querySelector('ion-router');
await wait(10);
}

return router.push(...args);
return router;
};

/**
Expand Down

0 comments on commit d814f19

Please sign in to comment.