useOverlay
Setup
useOverlay shows what it opens in KOverlayHost. Add it once, at the root of your app.
<template>
<NuxtPage />
<KOverlayHost />
</template>
<script setup lang="ts">
import { KOverlayHost } from '@kuina-ui/vue'
</script>
<template>
<RouterView />
<KOverlayHost />
</template>
The host sits inside your app, so what it opens can use your router, your stores and your translations.
Usage
Write the overlay
Write a component whose root is a Modal or a Drawer. Send close with the value to give back.
<script setup lang="ts">
import { ref } from 'vue'
const props = defineProps<{ budgetName: string }>()
const emit = defineEmits<{ close: [renamedBudget: string] }>()
const renamedBudget = ref(props.budgetName)
</script>
<template>
<KModal title="Rename budget" description="Pick a name you will recognize in your reports." size="sm">
<template #body>
<form id="rename-budget-form" @submit.prevent="emit('close', renamedBudget)">
<KTextField v-model="renamedBudget" label="Budget name" size="3xl" />
</form>
</template>
<template #footer="{ close }">
<KButton label="Cancel" variant="soft" color="gray" @click="close" />
<KButton label="Rename" type="submit" form="rename-budget-form" />
</template>
</KModal>
</template>
The modal needs no v-model:open and no element to open it: the host opens it, and closes it when it sends close.
Open it
Give the component to useOverlay, then call open with its props. open returns a promise that resolves with the value of close.
The budget is called Groceries.
<script setup lang="ts">
import { useOverlay } from '@kuina-ui/vue'
import { ref } from 'vue'
import RenameBudgetModal from './RenameBudgetModal.vue'
const renameBudgetModal = useOverlay(RenameBudgetModal)
const budgetName = ref('Groceries')
async function renameBudget() {
const renamedBudget = await renameBudgetModal.open({ budgetName: budgetName.value })
if (renamedBudget) budgetName.value = renamedBudget
}
</script>
<template>
<div class="flex flex-wrap items-center gap-3">
<KButton label="Rename budget" variant="outline" color="gray" @click="renameBudget" />
<p class="text-sm text-muted">The budget is called {{ budgetName }}.</p>
</div>
</template>
When the user closes the modal another way, with the close button, Esc, a click outside or close from a slot, the promise resolves with undefined. With the Nuxt module, useOverlay is auto-imported. In a Vue app, import it from @kuina-ui/vue.
Types
open takes the props of your component, minus the ones the host sets. It needs an argument only when your component has required props, and refuses props your component doesn't have. The promise has the type of the value sent with close, or undefined: here, Promise<string | undefined>.
Close from your code
close closes the last overlay opened by this useOverlay, and resolves its promise with the value you give. Here, the modal closes when the user leaves the page.
<script setup lang="ts">
import { useOverlay } from '@kuina-ui/vue'
import { onBeforeRouteLeave } from 'vue-router'
import RenameBudgetModal from './RenameBudgetModal.vue'
const renameBudgetModal = useOverlay(RenameBudgetModal)
onBeforeRouteLeave(() => renameBudgetModal.close())
</script>
<template>
<KButton label="Rename budget" @click="renameBudgetModal.open({ budgetName: 'Groceries' })" />
</template>
Each call to open shows a new copy of the component, and the host removes it once it has finished closing.
API
useOverlay(component)
| Returns | Description |
|---|---|
open(props?) | Shows the component with these props, and resolves with the value of its close, or undefined. |
close(value?) | Closes the last component opened by this open, and resolves its promise with value. |
KOverlayHost
Place it once in your app. It has no props. It shows every component opened with useOverlay or useConfirm, and passes them open, then listens to their update:open, close and after-close.