You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/3.api/2.composables/1.use-modal.md
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,97 @@ modalInstance.options // the state of the dynamic modal
55
55
modalInstance.patchOptions({})
56
56
```
57
57
58
+
## Edge Usage
59
+
60
+
::alert{type=danger}
61
+
❗️ If you really need to call `useModal()`{lang=ts} outside of `<script setup>`, there is a way to approach it but you must use it carefully.
62
+
::
63
+
64
+
There is a optional property that is `options.context`{lang=ts}, it accept a vfm instance. If given a vfm instance to `options.context`{lang=ts}, `useModal()`{lang=ts} will use the vfm instance internally instead of the injected vfm instance. This will allow you to use `useModal()`{lang=ts} outside out `<script setup>`.
65
+
66
+
You have to manually calling `modalInstance.destroy()`{lang=ts} whenever you no longer need the dynamic modal instance anymore. Here are some key points that you need to know about `modalInstance.destroy()`{lang=ts} and `modalInstance.close()`{lang=ts}:
67
+
68
+
- Calling `modalInstance.destroy()`{lang=ts} will delete the modal instance inside of `vfm.dynamicModals`. So if you have an opened modal, the modal will be closed if you calling `modalInstance.destroy()` without calling `modal.close()`{lang=ts}.
69
+
- Calling `modal.close()`{lang=ts} will not delete the modal instance inside of `vfm.dynamicModals`. So if you keep calling the `useModal()`{lang=ts}, the length of `vfm.dynamicModals` will keep growing until you have memory leak (see [#277](https://github.com/vue-final/vue-final-modal/issues/277)). This is the reason why you have to manually call `modalInstance.destroy()`{lang=ts} whenever you no longer need the dynamic modal instance anymore (usually in `onClosed` hook).
70
+
- Without calling `modalInstance.destroy()`{lang=ts} correctly, the memory leak might occur both on server side (Nuxt 3) and on client side (Vue 3 SPA).
71
+
72
+
### Vue 3
73
+
74
+
- You have to create a plugin that export the `vfm` instance:
75
+
```ts [@/plugins/vue-final-modal.ts]
76
+
import { createVfm } from'vue-final-modal'
77
+
78
+
exportconst vfm =createVfm()
79
+
```
80
+
- Then you have to import the `vfm` instance in your `main.ts` file and register it as a Vue plugin:
81
+
```ts [main.ts]
82
+
import { createApp } from'vue'
83
+
importAppfrom'./App.vue'
84
+
import { vfm } from'@/plugins/vue-final-modal'
85
+
86
+
const app =createApp(App)
87
+
88
+
app.use(vfm).mount('#app')
89
+
```
90
+
- Then you can using `useModal()`{lang=ts} everywhere by given the same `vfm` instance to `options.context`{lang=ts}:
Copy file name to clipboardExpand all lines: docs/content/3.api/2.composables/2.use-vfm.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,21 +26,23 @@ const {
26
26
## Global state
27
27
28
28
-`vfm.modals`{lang=ts}: All modals including opened and closed modals.
29
-
-`vfm.openedModals`{lang=ts}: All opened modals.
29
+
-`vfm.openedModals`{lang=ts}: All opened modals including opened `vfm.modals` and opened `vfm.dynamicModals`.
30
30
-`vfm.dynamicModals`{lang=ts}: All opened dynamic modals that create by [useModal()](/api/composables/use-modal).
31
31
32
32
## Functions
33
33
34
+
`vfm` instance provides some utility functions that allows you to operate a modal (see [modalId](/api/components/vue-final-modal#modalid-optional)) or modals.
35
+
34
36
-`vfm.get(modalId)`{lang=ts}: Get a modal instance by given a modalId
35
37
-`vfm.toggle(modalId)`{lang=ts}: Toggle a modal by given a modalId
36
38
```ts
37
-
vfm.close(modalId).then(() => {
39
+
vfm.toggle(modalId).then(() => {
38
40
// Do something after the modal opened or closed
39
41
})
40
42
```
41
43
-`vfm.open(modalId)`{lang=ts}: Open a modal by given a modalId
0 commit comments