vue-popup-plus 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,56 +1,115 @@
1
1
  import { App } from 'vue';
2
2
  import { Component } from 'vue';
3
+ import { ComputedRef } from 'vue';
3
4
  import { InjectionKey } from 'vue';
4
5
  import { version } from '../package.json';
5
6
 
7
+ declare type Animation_2 = IAnimations[keyof IAnimations];
8
+
6
9
  /**
7
10
  * 组件注入键类型
8
11
  */
9
12
  declare type ComponentInjectKeys = {
10
13
  /**
11
- * 当前组件所在弹出层的实例ID,可用于销毁当前弹出层
12
- * @example
14
+ * 当前组件所在弹出层的实例ID
15
+ * - 可用于销毁当前弹出层
16
+ * - 使用示例:
17
+ * ```ts
13
18
  * // 弹出层渲染的所有子代组件中
14
- * const instanceId = inject(POPUP_COMPONENT_INJECT_KEYS.INSTANCE_ID)
19
+ * import { inject } from 'vue'
20
+ * import { usePopup, POPUP_COMPONENT_INJECTS } from 'vue-popup-plus'
21
+ *
22
+ * // 获取弹出层控制器
23
+ * const popup = usePopup()
24
+ *
25
+ * // 获取当前组件所在弹出层的实例ID
26
+ * const instanceId = inject(POPUP_COMPONENT_INJECTS.INSTANCE_ID)
15
27
  *
16
28
  * // 销毁当前弹出层
17
- * this.$popup.destroy(instanceId)
29
+ * popup.destroy(instanceId)
30
+ * ```
18
31
  */
19
32
  INSTANCE_ID: InjectionKey<InstanceId>;
33
+ /**
34
+ * 弹出层视图样式
35
+ * - 可在弹出层内部组件内获取弹出层根级视图组件的样式
36
+ * - 使用示例:
37
+ * ```ts
38
+ * // 弹出层渲染的所有子代组件中
39
+ * import { inject } from 'vue'
40
+ * import { usePopup, POPUP_COMPONENT_INJECTS } from 'vue-popup-plus'
41
+ *
42
+ * // 获取弹出层控制器
43
+ * const popup = usePopup()
44
+ *
45
+ * // 获取当前组件所在弹出层的实例ID
46
+ * const instanceId = inject(POPUP_COMPONENT_INJECTS.INSTANCE_ID)
47
+ *
48
+ * // 获取弹出层根级视图组件的样式
49
+ * const computedViewStyle = inject(POPUP_COMPONENT_INJECTS.COMPUTED_VIEW_STYLE)
50
+ * ```
51
+ */
52
+ COMPUTED_VIEW_STYLE: InjectionKey<PopupViewStyle>;
53
+ };
54
+
55
+ declare type ControllerPrototype = Record<string, string | boolean | number | symbol | object | null | undefined | bigint | ControllerPrototypeFunctionValue> & {
56
+ render: '内置 render 方法';
57
+ update: '内置 update 方法';
58
+ destroy: '内置 destroy 方法';
59
+ install: '内置 install 方法';
60
+ use: '内置 use 方法';
20
61
  };
21
62
 
63
+ declare type ControllerPrototypeFunctionValue = (this: IController, ...args: any[]) => any;
64
+
65
+ declare type CoreConfig = Required<CoreOptions>;
66
+
22
67
  declare type CoreOptions = {
23
68
  /**
24
- * 弹出层 zIndex 基础值,默认为1000,每次生成弹出层时,除非 render() 方法传入 zIndex,否则使用此基础值,每次使用后会自动递增
69
+ * 弹出层 zIndex 基础值
70
+ * - 默认为1000,每次生成弹出层时,除非 render() 方法传入 zIndex,否则使用此基础值,每次使用后会自动递增
25
71
  */
26
72
  zIndex?: number;
27
73
  /**
28
- * 弹出层控制器挂载在 Vue 实例上的属性名,默认为 $popup ,这在使用选项式API时可以在组件内通过this.$popup 访问控制器实例,如需自定义属性名,请参考如下代码:
74
+ * 是否自动禁用滚动
75
+ * - 默认为 true
76
+ * - 开启后,弹出层显示时会自动禁用页面滚动
77
+ */
78
+ autoDisableScroll?: boolean;
79
+ /**
80
+ * 弹出层控制器挂载在 Vue 实例上的属性名
81
+ * - 默认为 $popup ,这在使用选项式API时可以在组件内通过this.$popup 访问控制器实例,可以使用该属性自定义挂载属性名
82
+ * - 使用示例:
83
+ * ```ts
84
+ * // main.ts
85
+ * import { createPopup } from 'vue-popup-plus'
29
86
  *
30
- * @example
31
- * // 初始化插件
32
- * createPopup({
87
+ * const popup = createPopup({
33
88
  * prototypeName: '$customPopup'
34
89
  * })
35
90
  *
36
- * // 在组件内访问
91
+ * // 组件内
37
92
  * this.$customPopup.render({
38
93
  * component: Demo,
39
94
  * })
40
- * @end
41
- * @important 注意,如果你使用 TypeScript,则自定义属性名称需要手动同步添加类型扩展,扩展代码可以放在一个 .ts 文件,或是一个影响整个项目的 *.d.ts 文件中。
42
- * @example
43
- * // 添加导出,没有该代码将无法有效扩展
44
- * export {}
45
- *
46
- * // 自定义扩展
95
+ * ```
96
+ * - 注意,如果你使用 TypeScript,则自定义属性名称需要手动同步添加类型扩展,扩展代码可以放在一个 .ts 文件,或是一个影响整个项目的 *.d.ts 文件中。
97
+ * - 扩展代码示例:
98
+ * ```ts
99
+ * // 扩展自定义属性名类型
47
100
  * declare module 'vue' {
48
101
  * interface ComponentCustomProperties {
49
- * $customPopup: VuePopupPlusController
102
+ * $customPopup: typeof popup
50
103
  * }
51
104
  * }
52
105
  */
53
106
  prototypeName?: string;
107
+ /**
108
+ * 开启调试模式
109
+ * - 默认为 false
110
+ * - 开启后,会提供开发调试功能
111
+ */
112
+ debugMode?: boolean;
54
113
  };
55
114
 
56
115
  /**
@@ -58,64 +117,58 @@ declare type CoreOptions = {
58
117
  * @param options 弹出层核心配置
59
118
  * @returns 弹出层控制器实例
60
119
  */
61
- export declare function createPopup(options?: CoreOptions): PopupController;
120
+ export declare function createPopup(options?: CoreOptions): IController;
62
121
 
63
- declare class InstanceId implements PopupInstanceId {
64
- #private;
65
- get seed(): number;
66
- get name(): string;
67
- constructor(seed: number);
68
- }
69
-
70
- /**
71
- * 动画类型集合
72
- */
73
- export declare const POPUP_ANIMATIONS: Readonly<PopupAnimationCollection>;
74
-
75
- /**
76
- * 弹出层内部组件注入属性,在弹出层内部渲染的所有子代组件中,都可以通过 inject 注入弹出层所提供的相关参数
77
- */
78
- export declare const POPUP_COMPONENT_INJECT_KEYS: Readonly<ComponentInjectKeys>;
122
+ export declare const definePlugin: IDefinePlugin;
79
123
 
80
- /**
81
- * 动画类型集合类型
82
- */
83
- declare interface PopupAnimationCollection {
124
+ declare interface IAnimations extends PopupCustomAnimations {
84
125
  /**
85
126
  * 无动画
86
127
  */
87
- NONE: symbol;
128
+ readonly NONE: 'none';
88
129
  /**
89
130
  * 淡入淡出
90
131
  */
91
- FADE: symbol;
132
+ readonly FADE: 'fade';
92
133
  /**
93
134
  * 缩放放大
94
135
  */
95
- SCALE_ENLARGE: symbol;
136
+ readonly SCALE_ENLARGE: 'scale-enlarge';
96
137
  /**
97
138
  * 缩放缩小
98
139
  */
99
- SCALE_SHRINK: symbol;
140
+ readonly SCALE_SHRINK: 'scale-shrink';
100
141
  /**
101
142
  * 顶部飞入
102
143
  */
103
- FLY_TOP: symbol;
144
+ readonly FLY_TOP: 'fly-top';
104
145
  /**
105
146
  * 左侧飞入
106
147
  */
107
- FLY_LEFT: symbol;
148
+ readonly FLY_LEFT: 'fly-left';
108
149
  /**
109
150
  * 右侧飞入
110
151
  */
111
- FLY_RIGHT: symbol;
152
+ readonly FLY_RIGHT: 'fly-right';
112
153
  /**
113
154
  * 底部飞入
114
155
  */
115
- FLY_BOTTOM: symbol;
156
+ readonly FLY_BOTTOM: 'fly-bottom';
116
157
  }
117
158
 
118
- declare interface PopupController extends PopupCustomController {
159
+ declare interface IController extends PopupCustomProperties {
160
+ /**
161
+ * 安装插件
162
+ * @param {App} app - Vue应用实例
163
+ * @returns {void}
164
+ */
165
+ install(app: App): void;
166
+ /**
167
+ * 安装插件
168
+ * - 可安装使用 `definePlugin` 方法定义的插件
169
+ * - 具体请参考{@link IDefinePlugin}
170
+ */
171
+ use(plugin: Plugin_2): void;
119
172
  /**
120
173
  * 渲染弹出层,返回弹出层实例id,可调用destroy(id)方法销毁弹出层
121
174
  * @param {RenderOptions} options - 渲染参数
@@ -135,21 +188,41 @@ declare interface PopupController extends PopupCustomController {
135
188
  * @returns {Promise<void>}
136
189
  */
137
190
  destroy(instanceId: InstanceId, payload?: any): void;
138
- /**
139
- * 安装插件
140
- * @param {App} app - Vue应用实例
141
- * @returns {void}
142
- */
143
- install(app: App): void;
144
191
  }
145
192
 
146
- declare interface PopupCustomController {
193
+ declare interface IDefinePlugin {
194
+ /**
195
+ * 定义插件
196
+ * - 插件是一个对象,包含插件名称和安装方法
197
+ * - 安装方法接收一个可扩展自定义属性的控制器实例作为参数,用于扩展弹出层插件的原型属性
198
+ * - 使用示例:
199
+ * ```ts
200
+ * import { createPopup, definePlugin } from 'vue-popup-plus'
201
+ * const popup = createPopup()
202
+ * const plugin = definePlugin({
203
+ * // 插件名称,必须唯一
204
+ * name: 'test',
205
+ * // 插件安装时的回调函数,会将控制器实例作为参数传入
206
+ * install(popup) {
207
+ * popup.customProperties.test = function (message) {
208
+ * this.render({
209
+ * component: () => import('path/Demo.vue'),
210
+ * componentProps: {
211
+ * message,
212
+ * },
213
+ * })
214
+ * }
215
+ * },
216
+ * })
217
+ * ```
218
+ */
219
+ (options: Plugin_2): Plugin_2;
147
220
  }
148
221
 
149
222
  /**
150
223
  * 实例 id 接口
151
224
  */
152
- declare interface PopupInstanceId {
225
+ declare interface IInstanceId {
153
226
  /* Excluded from this release type: seed */
154
227
  /**
155
228
  * 实例 id 名称
@@ -157,6 +230,90 @@ declare interface PopupInstanceId {
157
230
  name: Readonly<string>;
158
231
  }
159
232
 
233
+ declare class InstanceId implements IInstanceId {
234
+ #private;
235
+ get seed(): number;
236
+ get name(): string;
237
+ constructor(seed: number);
238
+ }
239
+
240
+ declare interface IPluginWrappedController extends IController {
241
+ /**
242
+ * 原型属性
243
+ * - 可在插件的 `install` 方法中扩展方法或属性
244
+ * - 该属性为只读属性,只允许扩展,并且内置方法不能被覆盖
245
+ * - 使用示例:
246
+ * ```ts
247
+ * // 插件中扩展方法
248
+ * import { definePlugin } from 'vue-popup-plus'
249
+ *
250
+ * const plugin = definePlugin({
251
+ * name: 'test',
252
+ * install(popup) {
253
+ * popup.customProperties.test = function (message: string) {
254
+ * this.render({
255
+ * component: () => import('./views/Demo.vue'),
256
+ * componentProps: {
257
+ * message,
258
+ * },
259
+ * })
260
+ * }
261
+ * },
262
+ * })
263
+ *
264
+ * // 调用
265
+ * popup.test('hello world')
266
+ * ```
267
+ */
268
+ readonly customProperties: ControllerPrototype;
269
+ /**
270
+ * 自定义动画类型
271
+ * - 可在插件的 `install` 方法中扩展动画类型
272
+ * - 该属性为只读属性,只允许扩展,并且内置动画类型不能被覆盖
273
+ * - 使用示例:
274
+ * ```ts
275
+ * // 插件中扩展动画类型
276
+ * import { definePlugin } from 'vue-popup-plus'
277
+ *
278
+ * const plugin = definePlugin({
279
+ * name: 'test',
280
+ * install(popup) {
281
+ * popup.customAnimations.CUSTOM = 'CUSTOM'
282
+ * },
283
+ * })
284
+ * ```
285
+ */
286
+ readonly customAnimations: PopupCustomAnimations;
287
+ }
288
+
289
+ declare type Plugin_2 = {
290
+ name: string;
291
+ install: PluginInstall;
292
+ };
293
+
294
+ declare type PluginInstall = (controller: IPluginWrappedController, config: Readonly<CoreConfig>) => void;
295
+
296
+ export declare const POPUP_ANIMATIONS: IAnimations;
297
+
298
+ /**
299
+ * 弹出层内部组件注入属性,在弹出层内部渲染的所有子代组件中,都可以通过 inject 注入弹出层所提供的相关参数
300
+ */
301
+ export declare const POPUP_COMPONENT_INJECTS: Readonly<ComponentInjectKeys>;
302
+
303
+ export declare interface PopupCustomAnimations {
304
+ }
305
+
306
+ export declare interface PopupCustomProperties {
307
+ }
308
+
309
+ declare type PopupViewStyle = ComputedRef<{
310
+ width: number;
311
+ height: number;
312
+ zIndex: number;
313
+ translateX: number;
314
+ translateY: number;
315
+ }>;
316
+
160
317
  declare type RenderComponentOptions = {
161
318
  /**
162
319
  * 弹出层渲染的组件,想要创建一个弹出层,这个唯一必要的参数。它的值可以是一个同步组件,也可以是一个异步组件的 import() 函数。
@@ -184,7 +341,7 @@ declare type RenderComponentOptions = {
184
341
  /**
185
342
  * 弹出层关闭之后的回调,触发时会将destroy() 方法的负载参数 payload 作为参数传入
186
343
  */
187
- onUnmounted?: <T>(payload?: T) => void;
344
+ onUnmounted?: (payload?: any) => void;
188
345
  };
189
346
 
190
347
  declare type RenderElement = HTMLElement | string;
@@ -325,18 +482,30 @@ declare type RenderStyleOptions = {
325
482
  * minHeight: 300,
326
483
  */
327
484
  minHeight?: string | number;
485
+ /**
486
+ * 弹出层视图水平偏移量,默认为 0 ,单位为 px
487
+ */
488
+ viewTranslateX?: number;
489
+ /**
490
+ * 弹出层视图垂直偏移量,默认为 0 ,单位为 px
491
+ */
492
+ viewTranslateY?: number;
493
+ /**
494
+ * 弹出层视图是否允许超出窗口边界,默认为 false
495
+ */
496
+ viewTranslateOverflow?: boolean;
328
497
  /**
329
498
  * 弹出层动画时长,默认为 100 ,单位为 毫秒
330
499
  */
331
500
  animationDuration?: number;
332
501
  /**
333
- * 遮罩层动画类型,默认为 POPUP_ANIMATIONS.FADE ,即淡入淡出,更多动画类型请查看 {@link PopupAnimationCollection}
502
+ * 遮罩层动画类型,默认为 POPUP_ANIMATIONS.FADE ,即淡入淡出,更多动画类型请查看 {@link IAnimations}
334
503
  */
335
- maskAnimation?: symbol;
504
+ maskAnimation?: Animation_2;
336
505
  /**
337
- * 视图层动画类型,默认为 POPUP_ANIMATIONS.FADE ,即淡入淡出,更多动画类型请查看 {@link PopupAnimationCollection}
506
+ * 视图层动画类型,默认为 POPUP_ANIMATIONS.FADE ,即淡入淡出,更多动画类型请查看 {@link IAnimations}
338
507
  */
339
- viewAnimation?: symbol;
508
+ viewAnimation?: Animation_2;
340
509
  /**
341
510
  * 弹出层 zIndex ,若不设置,则使用全局递增的 zIndex 值
342
511
  */
@@ -349,7 +518,7 @@ declare type UpdateOptions = Partial<Omit<RenderOptions, 'component' | 'el' | 'a
349
518
  * 获取弹出层控制器实例
350
519
  * @returns 弹出层控制器实例
351
520
  */
352
- export declare function usePopup(): PopupController;
521
+ export declare function usePopup(): IController;
353
522
 
354
523
  export { version }
355
524
 
@@ -358,6 +527,6 @@ export { }
358
527
 
359
528
  declare module 'vue' {
360
529
  interface ComponentCustomProperties {
361
- $popup: PopupController;
530
+ $popup: IController;
362
531
  }
363
532
  }