@modalor/vue
A modal state manager for Vue3.
Install
pnpm add @modalor/vue
Usage
- Render the Modalor in the root of your app.
<script lang="ts" setup>
import { Modalor } from '@modalor/vue'
</script>
<!-- App.vue -->
<template>
<!-- ... -->
<Modalor />
</template>
- create a modal with your own modal component
import { create } from '@modalor/vue'
import { h } from 'vue'
interface ModalProps {
title: string
}
export const createModal = create<ModalProps>(({ onCancel, onOk, open, isOkLoading, props: { renderChildren, ...rest } }) => (
h(YourModalComponent, { onCancel, onOk, open, isOkLoading, ...rest }, renderChildren)
))
- create a modal with a content component
import { createModal } from './../modal'
interface ModalContentProps {
username: string
}
interface ModalContentResolveValues {
newUsername: string
}
const userProfileModal = createModal<ModalContentProps, ModalContentResolveValues>(props => (
h(YourModalContentComponent, { ...props })
))
<script setup>
// 1. import your modal
import { userProfileModal } from '@/modals/userProfile'
async function showModal() {
// 2. show modal
const [isOk, result] = await userProfileModal.show({
username: 'John'
}, {
title: 'User Profile', // you can override modal props
})
// 3. handle result
if (isOk) {
console.log(result.newUsername) // will be your resolve values
}
}
</script>
<template>
<button @click="showModal">
Show Modal
</button>
</template>