Gathering detailed insights and metrics for @qaq-public/vue-nice-modal
Gathering detailed insights and metrics for @qaq-public/vue-nice-modal
Gathering detailed insights and metrics for @qaq-public/vue-nice-modal
Gathering detailed insights and metrics for @qaq-public/vue-nice-modal
Vue Nice Modal is a utility library that converts Vue.js modal components into a Promise-based API, making it easy to integrate with other UI libraries. Inspired by @ebay/nice-modal-react and vant.
npm install @qaq-public/vue-nice-modal
Typescript
Module System
Node Version
NPM Version
55.8
Supply Chain
98.9
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (96.36%)
JavaScript (2.35%)
Shell (1.3%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
647
Last Day
1
Last Week
7
Last Month
25
Last Year
343
29 Commits
1 Branches
1 Contributors
Updated on Jun 28, 2024
Latest Version
1.2.2
Package Id
@qaq-public/vue-nice-modal@1.2.2
Unpacked Size
55.84 kB
Size
11.95 kB
File Count
16
NPM Version
10.2.4
Node Version
20.11.0
Published on
Jun 28, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
7
Compared to previous week
Last Month
92.3%
25
Compared to previous month
Last Year
12.8%
343
Compared to previous year
1
5
vue-nice-modal 是一个工具库,可以将 Vue.js 的 modal 组件转换为基于 Promise 的 API。
灵感来源于 @ebay/nice-modal-react 和 vant。
支持 Vue 2.x,通过 vue-demi。
English | 简体中文
你可以在 examples/* 文件夹中查看示例。
1npm install vue-nice-modal 2# or 3yarn add vue-nice-modal 4# or 5pnpm add vue-nice-modal
1import { create } from 'vue-nice-modal'; 2import MyModal from './MyModal.vue'; 3 4const myModal = create(MyModal); 5 6myModal 7 .show({ 8 title: 'Hello, world!', 9 content: 'This is a nice modal.', 10 }) 11 .then((result) => { 12 console.log('Confirmed! Result:', result); 13 }) 14 .catch((error) => { 15 console.error('Rejected! Error:', error); 16 });
1<script setup lang="ts"> 2import { Dialog } from 'vant'; 3import { INiceModalHandlers } from 'vue-nice-modal'; 4// inject hide/remove/callback methods by vue-nice-modal 5interface IProps extends INiceModalHandlers<number> { 6 visible: boolean; 7 // props you need 8 title: string; 9 content: string; 10} 11 12interface IEmits { 13 (e: 'update:visible', visible: boolean): void; 14} 15 16const props = defineProps<IProps>(); 17 18// @ts-ignore 19const emit = defineEmits<IEmits>(); 20 21const handleCancel = () => { 22 props.hide(); // or emit('update:visible', false) 23 props.callback('cancel'); // reject the promise 24}; 25 26const handleConfirm = async () => { 27 // mock async function call 28 const sleep = (ms: number): Promise<number> => 29 new Promise((res) => 30 setTimeout(() => { 31 res(ms); 32 }, ms) 33 ); 34 35 const payload = await sleep(1000); 36 37 // resolve the promise with payload 38 props.callback('confirm', payload); 39}; 40</script> 41 42<template> 43 <Dialog 44 :show="visible" 45 @update:show="$emit('update:visible', false)" 46 @cancel="handleCancel" 47 @confirm="handleConfirm" 48 @closed="remove" 49 :title="title" 50 :content="content" 51 show-cancel-button 52 class="demo-dialog" 53 > 54 <div>Hello,Vue Nice Modal</div> 55 </Dialog> 56</template>
本节提供了一个使用 vue-nice-modal 库创建自定义 modal 组件的示例。该示例使用 vant UI 库的 Dialog 组件作为示例,但您可以使用任何自定义 modal 组件。
要创建自己的 modal 组件,您需要定义一个继承 INiceModalHandlers 接口的接口。该接口应包括与您的 modal 组件相关的任何属性,例如标题属性和内容属性。您还可以包括任何其他需要的属性或方法。
在示例中,visible 属性和 update:visible 事件由 vue-nice-modal 注入到自定义 modal 组件中。这些用于控制 modal 组件的可见性。visible 属性应是一个布尔值,用于确定 modal 是可见的还是不可见,update:visible 事件应在 modal 的可见性改变时触发。
hide()、remove() 和 callback() 方法也由 vue-nice-modal 注入到自定义 modal 组件中。这些方法用于隐藏或删除 modal 组件,以及处理用户确认或取消 modal 操作。
一旦您定义了自己的自定义 modal 组件,您可以使用 vue-nice-modal 提供的 create() 函数来创建一个 Modal 对象,该对象公开 show()、hide() 和 remove() 方法。然后,您可以使用 show() 方法显示自定义 modal 组件,并使用 vue-nice-modal 提供的基于 Promise 的 API 处理用户确认或取消 modal 操作。
1import { createApp } from 'vue'; 2import { VueNiceModalPluginForVue3 } from 'vue-nice-modal'; 3import App from './App.vue'; 4 5const app = createApp(App); 6 7app.use(VueNiceModalPluginForVue3); 8 9app.mount('#app');
Vue Nice Modal 在内部创建了一个新的 Vue 应用程序实例,并将用户创建的组件挂载到该实例中。这使得它可以在模态框中正常运行,而不会与主应用程序的状态和逻辑发生冲突。
然而,如果您需要在模态框中访问主应用程序中的数据或方法,您可以使用该插件实现共享上下文。
你可以通过在插件选项中传递 appKey 的形式来区分多个应用程序,并在创建模态框实例时将其传递。
1app.use(VueNiceModalPluginForVue3, { appKey: 'another app key' }); 2 3create(MyModal, 'another app key');
在调用 app.use() 时,通过将选项对象传递给插件来传递 app 的 key(例如,{ appKey: 'another app key' })。然后,在创建模态框实例时,需要将 appKey 作为选项传递给 create() 方法(例如,create(MyModal, 'another app key'))。这样就可以在多个应用程序中确保每个模态框都可以访问正确的上下文。
create 函数接受 Vue.js 组件并返回一个带有以下方法的 Modal 对象:
显示 modal 组件并返回一个 Promise,如果用户确认 modal 则 resolve,如果用户取消则 reject。
options 参数是一个对象,包含与 modal 组件相关的属性(除去 vue-nice-modal 注入的通用属性与方法,仅包含用户自定义的所需 props)。ComponentProps 和 INiceModalHandlers 类型用于确保 options 对象的类型正确,并在编译时捕获与属性使用相关的任何错误。
以下是 show 方法的类型提示实现:
1type ComponentProps<C extends Component> = C extends new (...args: any) => any 2 ? Omit< 3 InstanceType<C>['$props'], 4 keyof VNodeProps | keyof AllowedComponentProps 5 > 6 : never; 7 8type ExtractOptions<T extends Record<string, any>> = Omit< 9 T, 10 keyof INiceModalHandlers | 'visible' | 'onUpdate:visible' 11>; 12 13export function create<C extends Component>(Comp: C) { 14 // ... 15 16 const show = (options: ExtractOptions<ComponentProps<C>>) => { 17 // ... 18 }; 19 20 return { 21 show, 22 // ... 23 }; 24}
隐藏 modal 组件。
从 DOM 中删除 modal 组件。
vue-nice-modal 提供了一些 TypeScript 类型定义:
Modal 接口定义了 create 返回的对象的方法。
1interface Modal { 2 show: (options: ExtractOptions<ComponentProps<C>>) => Promise<any>; 3 hide: () => void; 4 remove: () => void; 5}
ComponentProps 工具泛型用于获取 Vue 组件的属性。
1type ComponentProps<C extends Component> = C extends new (...args: any) => any 2 ? Omit< 3 InstanceType<C>['$props'], 4 keyof VNodeData | keyof AllowedComponentProps 5 > 6 : never;
INiceModalHandlers 接口定义了用于处理用户确认或取消 modal 的方法。
1export interface INiceModalHandlers<T = any> { 2 callback: (action: 'confirm' | 'cancel', payload?: T) => void; 3 remove: () => void; 4 hide: () => void; 5}
这些方法以及 visible 和 update:visible 事件将被注入用户的自定义 modal 组件中,即使不使用基于 Promise 的函数调用,相关属性也可以通过 v-model(visible 和 update:visible)传递从而控制组件的可见性。这允许用户按自己喜欢的方式控制 modal 组件的显示和隐藏,同时也确保了 vue-nice-modal 库的灵活性。
ExtractOptions 类型用于提取与 modal 组件相关的选项(除去 vue-nice-modal 注入的通用属性与方法)。
1type ExtractOptions<T extends Record<string, any>> = Omit< 2 T, 3 keyof INiceModalHandlers | 'visible' | 'onUpdate:visible' 4>;
No vulnerabilities found.