promise-portal 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/README.md +141 -1
  2. package/package.json +2 -1
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # promise-portal
2
2
 
3
+ let you use react portal in vue, and with promise
4
+
3
5
  ## Install
4
6
 
5
7
  ```bash
@@ -15,7 +17,145 @@ yarn add promise-portal --D
15
17
 
16
18
  ## Use
17
19
 
18
- ## Reference
20
+ ### install in the entry file
21
+
22
+ ```ts
23
+ // ./main.ts
24
+ import { createPromisePortal } from 'promise-portal'
25
+
26
+ const app = createApp(App)
27
+ app.use(createPromisePortal())
28
+ ```
29
+
30
+ ### in component, use `usePortalContext` to get portal context
31
+
32
+ ```ts
33
+ // ./components/name.vue
34
+ import { usePortalContext } from 'promise-portal'
35
+
36
+ const { resolve } = usePortalContext<Output>()
37
+ const onClose = () => {
38
+ resolve({ confirm: false, fullName: '' })
39
+ }
40
+ ```
41
+
42
+ ### define portal, use it like a promise-style function
43
+
44
+ ```ts
45
+ // ./App.vue
46
+ import { definePortal } from 'promise-portal'
47
+ import Comp, { Input, Output } from './components/name.vue'
48
+
49
+ const func = definePortal<Output, Input>(Comp)
50
+ const onClick = async () => {
51
+ const data = await func({ firstName: 'joe', lastName: 'watson' })
52
+ if (!data.confirm) {
53
+ return
54
+ }
55
+ console.log(data)
56
+ }
57
+ ```
58
+
59
+ ## API Reference
60
+
61
+ - createPromisePortal
62
+
63
+ create promise-portal instance
64
+
65
+ ```ts
66
+ const instance = createPromisePortal()
67
+ app.use(instance) // vue instance
68
+ ```
69
+
70
+ - getActiveInstance
71
+
72
+ get active promise-portal instance
73
+
74
+ ```ts
75
+ const instance = getActiveInstance()
76
+ ```
77
+
78
+ - setActiveInstance
79
+
80
+ set promise-portal instance to be active
81
+
82
+ ```ts
83
+ setActiveInstance(instance)
84
+ ```
85
+
86
+ - usePortalContext
87
+
88
+ a vue composition api, use in portal component to get context of portal
89
+
90
+ ```ts
91
+ const { resolve, reject, el, vNode } = usePortalContext()
92
+ // resolve: promise resolve handler
93
+ // reject: promise reject handler
94
+ // el: portal base element, injecting to body element
95
+ // vNode: portal base vue vnode
96
+ ```
97
+
98
+ you can use typescript generic types
99
+
100
+ ```ts
101
+ const { resolve } = usePortalContext<Output>()
102
+ resolve({ ... }) // an object of type Output
103
+ ```
104
+
105
+ - definePortal
106
+
107
+ define a portal, return a portal function
108
+
109
+ ```ts
110
+ import Comp from './component.vue'
111
+ const portal = definePortal(Comp)
112
+ portal() // return a promise
113
+ ```
114
+
115
+ you can define generic types to check input object and output object
116
+
117
+ ```ts
118
+ // component.vue
119
+ export interface Input {
120
+ firstName: string
121
+ lastName: string
122
+ }
123
+
124
+ export interface Output {
125
+ fullName: string
126
+ confirm: boolean
127
+ }
128
+
129
+ const props = defineProps<Input>()
130
+ const { resolve } = usePortalContext<Output>()
131
+
132
+ // App.vue
133
+ import Comp, { Input, Output } from './component.vue'
134
+ const portal = definePortal<Output, Input>(Comp)
135
+ const output = await portal({
136
+ firstName: 'joe',
137
+ lastName: 'watson',
138
+ })
139
+ ```
140
+
141
+ how to define a portal with empty parameter
142
+
143
+ ```ts
144
+ // component.vue
145
+ export interface Output {
146
+ fullName: string
147
+ confirm: boolean
148
+ }
149
+
150
+ const { resolve } = usePortalContext<Output>()
151
+
152
+ // App.vue
153
+ import Comp, { Output } from './component.vue'
154
+ const portal = definePortal<Output, void>(Comp)
155
+ const output = await portal() // only allow empty parameter
156
+ ```
157
+
158
+ ## Link
19
159
 
20
160
  [@filez/portal](https://github.com/lenovo-filez/portal)
21
161
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "promise-portal",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "a vite plugin to load svg icon for element-plus",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -39,6 +39,7 @@
39
39
  "license": "MIT",
40
40
  "keywords": [
41
41
  "vue",
42
+ "promise",
42
43
  "portal"
43
44
  ],
44
45
  "scripts": {