@codeleap/web 3.10.7 → 3.11.1
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.
- package/package.json +1 -1
- package/src/components/SearchInput/index.tsx +1 -1
- package/src/index.ts +2 -1
- package/src/lib/OSAlert.tsx +71 -82
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,4 +6,5 @@ export * from './lib/useSearchParams'
|
|
|
6
6
|
export * from './lib/useBreakpointMatch'
|
|
7
7
|
export * from './lib/usePopState'
|
|
8
8
|
export { default as Toast } from './lib/Toast'
|
|
9
|
-
export { CreateOSAlert } from './lib/OSAlert'
|
|
9
|
+
export { CreateOSAlert, useGlobalAlertComponent, AlertOutlet } from './lib/OSAlert'
|
|
10
|
+
export type { GlobalAlertComponentProps, GlobalAlertType } from './lib/OSAlert'
|
package/src/lib/OSAlert.tsx
CHANGED
|
@@ -1,29 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { AnyFunction, onMount, onUpdate, useBooleanToggle, usePrevious } from '@codeleap/common'
|
|
3
3
|
import ReactDOM from 'react-dom'
|
|
4
|
-
|
|
4
|
+
import { v4 as uuid } from 'uuid'
|
|
5
5
|
type AlertButton = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
text: string
|
|
7
|
+
onPress: AnyFunction
|
|
8
|
+
variants?: any[]
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
type OSAlertArgs = {
|
|
12
12
|
title: string
|
|
13
13
|
body?: string
|
|
14
14
|
options?: AlertButton[]
|
|
15
|
+
onDismiss?: AnyFunction
|
|
16
|
+
onAction?: AnyFunction
|
|
15
17
|
}
|
|
16
18
|
type AlertEvent = AlertButton['onPress']
|
|
17
|
-
|
|
19
|
+
|
|
18
20
|
type NamedEvents<E extends string> = Partial<Record<E, AlertEvent>>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
|
|
22
|
+
export type GlobalAlertType = 'info' | 'error' | 'warn' | 'ask'
|
|
23
|
+
|
|
24
|
+
export type GlobalAlertComponentProps = {
|
|
25
|
+
args: OSAlertArgs
|
|
26
|
+
removeSelf: AnyFunction
|
|
27
|
+
type: GlobalAlertType
|
|
28
|
+
id: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useGlobalAlertComponent(props: GlobalAlertComponentProps) {
|
|
21
32
|
const [visible, toggle] = useBooleanToggle(false)
|
|
22
33
|
|
|
23
34
|
const previousVisible = usePrevious(visible)
|
|
24
35
|
|
|
25
|
-
const { title, body, options = [] } = props.args
|
|
26
|
-
|
|
27
36
|
onMount(() => {
|
|
28
37
|
toggle()
|
|
29
38
|
})
|
|
@@ -34,35 +43,28 @@ function RenderModal(props: {args:OSAlertArgs; contextProps:AlertContext ; remov
|
|
|
34
43
|
}
|
|
35
44
|
}, [visible])
|
|
36
45
|
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
{
|
|
47
|
-
options.map((o, idx) => <Button debugName={`OSAlert ${title}`} key={idx} {...o} onPress={() => {
|
|
48
|
-
o.onPress?.()
|
|
49
|
-
toggle()
|
|
50
|
-
|
|
51
|
-
}}/>)
|
|
52
|
-
}
|
|
53
|
-
</>}
|
|
54
|
-
>
|
|
55
|
-
<Text text={body || ''} />
|
|
56
|
-
|
|
57
|
-
</Modal>
|
|
58
|
-
</StyleProvider>
|
|
46
|
+
return {
|
|
47
|
+
visible,
|
|
48
|
+
toggle,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function RenderModal(props: GlobalAlertComponentProps) {
|
|
53
|
+
|
|
54
|
+
return null
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
const MODAL_ID = '__CODELEAP_MODAL__'
|
|
62
58
|
|
|
63
|
-
function OSAlert(props:OSAlertArgs & {
|
|
59
|
+
function OSAlert(props: OSAlertArgs & { type: GlobalAlertType }) {
|
|
64
60
|
if (!document) return
|
|
65
|
-
|
|
61
|
+
|
|
62
|
+
const modalId = MODAL_ID + '_' + uuid()
|
|
63
|
+
|
|
64
|
+
const modalsRootDiv = document.getElementById(MODAL_ID)
|
|
65
|
+
|
|
66
|
+
let modalDiv = document.getElementById(modalId)
|
|
67
|
+
|
|
66
68
|
const remove = () => {
|
|
67
69
|
ReactDOM.unmountComponentAtNode(modalDiv)
|
|
68
70
|
}
|
|
@@ -72,30 +74,39 @@ function OSAlert(props:OSAlertArgs & {context: AlertContext}) {
|
|
|
72
74
|
|
|
73
75
|
modalDiv.setAttribute('id', MODAL_ID)
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
remove()
|
|
77
|
+
modalsRootDiv.appendChild(modalDiv)
|
|
78
|
+
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
ReactDOM.render(
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
ReactDOM.render(<>
|
|
82
|
+
|
|
83
|
+
<OSAlert.Component
|
|
83
84
|
removeSelf={remove}
|
|
84
85
|
args={props}
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
type={props.type}
|
|
87
|
+
id={modalId}
|
|
88
|
+
/>
|
|
89
|
+
|
|
90
|
+
</>, modalDiv)
|
|
91
|
+
|
|
87
92
|
}
|
|
93
|
+
OSAlert.Component = RenderModal
|
|
94
|
+
|
|
95
|
+
export function CreateOSAlert(Component) {
|
|
96
|
+
|
|
97
|
+
OSAlert.Component = Component
|
|
88
98
|
|
|
89
|
-
export function CreateOSAlert(context: AlertContext) {
|
|
90
99
|
function ask({ title, body, options }: OSAlertArgs) {
|
|
91
100
|
if (!title) {
|
|
92
101
|
title = 'Quick quetion'
|
|
93
102
|
}
|
|
94
103
|
OSAlert({
|
|
95
|
-
context,
|
|
96
104
|
title,
|
|
97
105
|
body,
|
|
98
106
|
options,
|
|
107
|
+
type: 'ask',
|
|
108
|
+
onAction: null,
|
|
109
|
+
onDismiss: null,
|
|
99
110
|
})
|
|
100
111
|
}
|
|
101
112
|
|
|
@@ -113,17 +124,11 @@ export function CreateOSAlert(context: AlertContext) {
|
|
|
113
124
|
}
|
|
114
125
|
OSAlert({
|
|
115
126
|
title,
|
|
116
|
-
|
|
127
|
+
|
|
117
128
|
body,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
onPress: () => {
|
|
122
|
-
|
|
123
|
-
args?.onDismiss?.()
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
],
|
|
129
|
+
type: 'error',
|
|
130
|
+
onAction: args.onDismiss,
|
|
131
|
+
onDismiss: args.onDismiss,
|
|
127
132
|
})
|
|
128
133
|
}
|
|
129
134
|
|
|
@@ -136,25 +141,13 @@ export function CreateOSAlert(context: AlertContext) {
|
|
|
136
141
|
} = args
|
|
137
142
|
|
|
138
143
|
OSAlert({
|
|
139
|
-
|
|
144
|
+
|
|
140
145
|
title,
|
|
141
146
|
body,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
onReject()
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
text: 'OK',
|
|
152
|
-
onPress: () => {
|
|
153
|
-
|
|
154
|
-
onAccept?.()
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
],
|
|
147
|
+
type: 'warn',
|
|
148
|
+
onAction: onAccept,
|
|
149
|
+
onDismiss: onReject,
|
|
150
|
+
|
|
158
151
|
})
|
|
159
152
|
}
|
|
160
153
|
|
|
@@ -167,18 +160,10 @@ export function CreateOSAlert(context: AlertContext) {
|
|
|
167
160
|
} = args
|
|
168
161
|
|
|
169
162
|
OSAlert({
|
|
170
|
-
context,
|
|
171
163
|
title,
|
|
172
164
|
body,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
text: 'OK',
|
|
176
|
-
onPress: () => {
|
|
177
|
-
|
|
178
|
-
onDismiss?.()
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
],
|
|
165
|
+
type: 'info',
|
|
166
|
+
onDismiss,
|
|
182
167
|
})
|
|
183
168
|
}
|
|
184
169
|
return {
|
|
@@ -188,3 +173,7 @@ export function CreateOSAlert(context: AlertContext) {
|
|
|
188
173
|
error: OSError,
|
|
189
174
|
}
|
|
190
175
|
}
|
|
176
|
+
|
|
177
|
+
export const AlertOutlet = () => {
|
|
178
|
+
return <div id={MODAL_ID} />
|
|
179
|
+
}
|