Skip to content

Commit

Permalink
Pop
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Jun 12, 2024
1 parent 0659eef commit cd2dd54
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/elements/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,19 @@
{
"kind": "method",
"name": "currentPathname"
},
{
"kind": "method",
"name": "pop",
"parameters": [
{
"name": "parentPath",
"default": "''",
"type": {
"text": "string"
}
}
]
}
],
"superclass": {
Expand Down Expand Up @@ -508,6 +521,19 @@
}
}
]
},
{
"kind": "method",
"name": "pop",
"parameters": [
{
"name": "parentPath",
"default": "''",
"type": {
"text": "string"
}
}
]
}
],
"superclass": {
Expand Down
2 changes: 1 addition & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@holochain-open-dev/elements",
"version": "0.300.0-rc.2",
"version": "0.300.0-rc.3",
"description": "Common utilities and elements to build Holochain web applications",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down
25 changes: 25 additions & 0 deletions packages/elements/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export class Routes extends NativeRoutes {

return pathname.split(prefix)[1];
}

// Goes back, and if there is no page saved in the history of the app,
// goes back up to the root of the previous level of the pathname
pop(parentPath: string = '') {
const previousPathname = this.currentPathname();
window.history.back();

setTimeout(() => {
if (this.currentPathname() === previousPathname) {
this.goto(this.link(parentPath));
}
});
}
}

export class Router extends NativeRouter {
Expand All @@ -48,4 +61,16 @@ export class Router extends NativeRouter {
go(delta: number | undefined) {
window.history.go(delta);
}
// Goes back, and if there is no page saved in the history of the app,
// goes back up to the root of the previous level of the pathname
pop(parentPath: string = '') {
const previousPathname = this.currentPathname();
window.history.back();

setTimeout(() => {
if (this.currentPathname() === previousPathname) {
this.goto(this.link(parentPath));
}
});
}
}

0 comments on commit cd2dd54

Please sign in to comment.