Skip to content

Commit

Permalink
Merge pull request #81 from Jenesius/issue_77
Browse files Browse the repository at this point in the history
docs: update information about closing modal with route.
  • Loading branch information
Jenesius committed Apr 9, 2023
2 parents fa1126c + 00e21b4 commit 911f10d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function sidebar() {
{ text: 'Introduction', link: '/guide/integration-introduction'},
{ text: 'Installation', link: '/guide/integration-installation'},
{ text: 'Practical', link: '/guide/integration-practical'},

{ text: 'Closing Modal with Router', link: '/vue-router-integration/close-after-route-changed' }
]
}
]
Expand Down
33 changes: 33 additions & 0 deletions docs/vue-router-integration/close-after-route-changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Closing modal with vue-router

By default, the modal doesn't close when the path changes (vue-router). This was done on purpose, because
your project may not have this library or any other one for working with routes. Also this
the function is not heavy even for an entry-level programmer.

## Vue Router
So that when closing a modal window using the **vue-router** library, you need to register the following
hook:

```ts
import {getCurrentModal, closeModal} from "jenesius-vue-modal";

const router = new VueRouter({...})

router.beforeEach(async (to, from, next) => {
// There's some opened modal
if (getCurrentModal()) { // (1)
try {
await closeModal(); // (2)
next(); // (3)
} catch (e) {
// ...
next(false); // (4)
}
} else next();
})
```

Let's see what's going on here:
- (1) - If the current modal window is not *undefined* then we need to close all modal windows (2).
- (3) - If the closing was successful, then we confirm the transition to the next route.
- (4) - We disable the transition if the *closeModal* threw an error (the modal window remained open).

0 comments on commit 911f10d

Please sign in to comment.