packages/core/createTemplatePromise/index.md
Template as Promise. Useful for constructing custom Dialogs, Modals, Toasts, etc.
<script setup lang="ts">
import { createTemplatePromise } from '@vueuse/core'
const TemplatePromise = createTemplatePromise<ReturnType>()
async function open() {
const result = await TemplatePromise.start()
// button is clicked, result is 'ok'
}
</script>
<template>
<TemplatePromise v-slot="{ promise, resolve, reject, args }">
<!-- your UI -->
<button @click="resolve('ok')">
OK
</button>
</TemplatePromise>
</template>
This function is migrated from vue-template-promise
createTemplatePromise returns a Vue Component that you can directly use in your template with <script setup>
import { createTemplatePromise } from '@vueuse/core'
const TemplatePromise = createTemplatePromise()
const MyPromise = createTemplatePromise<boolean>() // with generic type
In template, use v-slot to access the promise and resolve functions.
<template>
<TemplatePromise v-slot="{ promise, resolve, reject, args }">
<!-- you can have anything -->
<button @click="resolve('ok')">
OK
</button>
</TemplatePromise>
<MyPromise v-slot="{ promise, resolve, reject, args }">
<!-- another one -->
</MyPromise>
</template>
The slot will not be rendered initially (similar to v-if="false"), until you call the start method from the component.
// @include: main
// ---cut---
const result = await TemplatePromise.start()
Once resolve or reject is called in the template, the promise will be resolved or rejected, returning the value you passed in. Once resolved, the slot will be removed automatically.
You can pass arguments to the start with arguments.
import { createTemplatePromise } from '@vueuse/core'
const TemplatePromise = createTemplatePromise<boolean, [string, number]>()
// @include: passing-arguments
// ---cut---
const result = await TemplatePromise.start('hello', 123) // Pr
And in the template slot, you can access the arguments via args property.
<template>
<TemplatePromise v-slot="{ args, resolve }">
<div>{{ args[0] }}</div>
<!-- hello -->
<div>{{ args[1] }}</div>
<!-- 123 -->
<button @click="resolve(true)">
OK
</button>
</TemplatePromise>
</template>
Use the singleton option to ensure only one instance of the promise can be active at a time. If start is called while a promise is already active, it will return the existing promise instead of creating a new one.
import { createTemplatePromise } from '@vueuse/core'
const TemplatePromise = createTemplatePromise<boolean>({
singleton: true,
})
// These will return the same promise if called in quick succession
const result1 = TemplatePromise.start()
const result2 = TemplatePromise.start() // returns the same promise as result1
You can use transition to animate the slot.
<script setup lang="ts">
const TemplatePromise = createTemplatePromise<ReturnType>({
transition: {
name: 'fade',
appear: true,
},
})
</script>
<template>
<TemplatePromise v-slot="{ resolve }">
<!-- your UI -->
<button @click="resolve('ok')">
OK
</button>
</TemplatePromise>
</template>
<style scoped>
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>
Learn more about Vue Transition.
The slot provides the following props:
| Prop | Type | Description |
|---|---|---|
promise | Promise<Return> | undefined | The current promise instance |
resolve | (v: Return | Promise<Return>) => void | Resolve the promise with a value |
reject | (v: any) => void | Reject the promise |
args | Args | Arguments passed to start() |
isResolving | boolean | true when resolving another promise passed to resolve |
key | number | Unique key for list rendering |
<template>
<TemplatePromise v-slot="{ promise, resolve, reject, args, isResolving }">
<div v-if="isResolving">
Loading...
</div>
<div v-else>
<button @click="resolve('ok')">
OK
</button>
<button @click="reject('cancelled')">
Cancel
</button>
</div>
</TemplatePromise>
</template>
The common approach to call a dialog or a modal programmatically would be like this:
const dialog = useDialog()
const result = await dialog.open({
title: 'Hello',
content: 'World',
})
This would work by sending these information to the top-level component and let it render the dialog. However, it limits the flexibility you could express in the UI. For example, you could want the title to be red, or have extra buttons, etc. You would end up with a lot of options like:
const result = await dialog.open({
title: 'Hello',
titleClass: 'text-red',
content: 'World',
contentClass: 'text-blue text-sm',
buttons: [
{ text: 'OK', class: 'bg-red', onClick: () => {} },
{ text: 'Cancel', class: 'bg-blue', onClick: () => {} },
],
// ...
})
Even this is not flexible enough. If you want more, you might end up with manual render function.
const result = await dialog.open({
title: 'Hello',
contentSlot: () => h(MyComponent, { content }),
})
This is like reinventing a new DSL in the script to express the UI template.
So this function allows expressing the UI in templates instead of scripts, where it is supposed to be, while still being able to be manipulated programmatically.