@codeleap/web 3.10.6 → 3.11.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/web",
3
- "version": "3.10.6",
3
+ "version": "3.11.0",
4
4
  "main": "src/index.ts",
5
5
  "repository": {
6
6
  "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
@@ -30,7 +30,7 @@ export const SearchInput: ComponentWithDefaultProps<SearchInputProps> = (props)
30
30
  debounce,
31
31
  value,
32
32
  onValueChange,
33
- defaultValue,
33
+ defaultValue = '',
34
34
  ...rest
35
35
  } = {
36
36
  ...SearchInput.defaultProps,
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'
@@ -1,29 +1,38 @@
1
- import { AnyFunction, onMount, onUpdate, StyleContextProps, StyleProvider, useBooleanToggle, usePrevious } from '@codeleap/common'
2
- import { Modal, Text, Button } from '../components/components'
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
- text: string
7
- onPress: AnyFunction
8
- variants?: any[]
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
- // type OSAlertType = 'info' | 'error' | 'warn' | 'ask'
19
+
18
20
  type NamedEvents<E extends string> = Partial<Record<E, AlertEvent>>
19
- type AlertContext = StyleContextProps<any, any>
20
- function RenderModal(props: {args:OSAlertArgs; contextProps:AlertContext ; removeSelf:AnyFunction}) {
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 <StyleProvider {...props.contextProps}>
38
- <Modal
39
- visible={visible}
40
- toggle={toggle}
41
- title={title}
42
- showClose={false}
43
- closable={false}
44
-
45
- footer={<>
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 & {context: AlertContext}) {
59
+ function OSAlert(props: OSAlertArgs & { type: GlobalAlertType }) {
64
60
  if (!document) return
65
- let modalDiv = document.getElementById(MODAL_ID)
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
- document.body.appendChild(modalDiv)
76
- } else {
77
- remove()
77
+ modalsRootDiv.appendChild(modalDiv)
78
+
78
79
  }
79
80
 
80
- ReactDOM.render(
81
- <RenderModal
82
- contextProps={props.context}
81
+ ReactDOM.render(<>
82
+
83
+ <OSAlert.Component
83
84
  removeSelf={remove}
84
85
  args={props}
85
- />, modalDiv,
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
- context,
127
+
117
128
  body,
118
- options: [
119
- {
120
- text: 'OK',
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
- context,
144
+
140
145
  title,
141
146
  body,
142
- options: [
143
- {
144
- text: 'Cancel',
145
- onPress: () => {
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
- options: [
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
+ }