Skip to content

Commit 605e83f

Browse files
committed
docs: update docs
1 parent 47b8914 commit 605e83f

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

docs/content/3.api/2.composables/1.use-modal.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,97 @@ modalInstance.options // the state of the dynamic modal
5555
modalInstance.patchOptions({})
5656
```
5757

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+
export const 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+
import App from './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}:
91+
```ts
92+
// Anywhere outside out script setup
93+
import { markRaw } from 'vue'
94+
import { VueFinalModal, useModal } from 'vue-final-modal'
95+
import { vfm } from '@/plugins/vue-final-modal'
96+
97+
function openConfirmModal() {
98+
const modalInstance = useModal({
99+
context: vfm,
100+
component: markRaw(MyConfirmModal),
101+
attrs: {
102+
onClosed() {
103+
modalInstance.destroy()
104+
},
105+
},
106+
slots: { defaults: '<p>The content of the modal</p>' }
107+
})
108+
109+
modalInstance.open()
110+
}
111+
```
112+
113+
### Nuxt 3
114+
115+
- You have to create a plugin that export the `vfm` instance and register it as a Vue plugin:
116+
```ts [@/plugins/vue-final-modal.ts]
117+
import { createVfm } from 'vue-final-modal'
118+
import { defineNuxtPlugin } from '#app'
119+
120+
export const vfm = createVfm() as any
121+
122+
export default defineNuxtPlugin((nuxtApp) => {
123+
nuxtApp.vueApp.use(vfm)
124+
})
125+
```
126+
- Then you can using `useModal()`{lang=ts} everywhere by given the same `vfm` instance to `options.context`{lang=ts}:
127+
```ts
128+
// Anywhere outside out script setup
129+
import { markRaw } from 'vue'
130+
import { VueFinalModal, useModal } from 'vue-final-modal'
131+
import { vfm } from '@/plugins/vue-final-modal'
132+
133+
function openConfirmModal() {
134+
const modalInstance = useModal({
135+
context: vfm,
136+
component: markRaw(MyConfirmModal),
137+
attrs: {
138+
onClosed() {
139+
modalInstance.destroy()
140+
},
141+
},
142+
slots: { defaults: '<p>The content of the modal</p>' }
143+
})
144+
145+
modalInstance.open()
146+
}
147+
```
148+
58149
## Type Declarations
59150

60151
::alert{type=info}

docs/content/3.api/2.composables/2.use-vfm.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ const {
2626
## Global state
2727

2828
- `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`.
3030
- `vfm.dynamicModals`{lang=ts}: All opened dynamic modals that create by [useModal()](/api/composables/use-modal).
3131

3232
## Functions
3333

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+
3436
- `vfm.get(modalId)`{lang=ts}: Get a modal instance by given a modalId
3537
- `vfm.toggle(modalId)`{lang=ts}: Toggle a modal by given a modalId
3638
```ts
37-
vfm.close(modalId).then(() => {
39+
vfm.toggle(modalId).then(() => {
3840
// Do something after the modal opened or closed
3941
})
4042
```
4143
- `vfm.open(modalId)`{lang=ts}: Open a modal by given a modalId
4244
```ts
43-
vfm.close(modalId).then(() => {
45+
vfm.open(modalId).then(() => {
4446
// Do something after the modal opened
4547
})
4648
```

0 commit comments

Comments
 (0)