-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathevent.ts
56 lines (49 loc) · 1.54 KB
/
event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { type OverlayAsyncControllerComponent, type OverlayControllerComponent } from './context/provider';
import { type OverlayItem, dispatchOverlay } from './context/store';
import { randomId } from './utils';
type OpenOverlayOptions = {
overlayId?: OverlayItem['id'];
};
function open(controller: OverlayControllerComponent, options?: OpenOverlayOptions) {
const overlayId = options?.overlayId ?? randomId();
dispatchOverlay({
type: 'ADD',
overlay: {
id: overlayId,
isOpen: false,
controller: controller,
},
});
return overlayId;
}
async function openAsync<T>(controller: OverlayAsyncControllerComponent<T>, options?: OpenOverlayOptions) {
return new Promise<T>((resolve) => {
open((overlayProps, ...deprecatedLegacyContext) => {
/**
* @description close the overlay with resolve
*/
const close = (param: T) => {
resolve(param as T);
overlayProps.close();
};
/**
* @description Passing overridden methods
*/
const props = { ...overlayProps, close };
return controller(props, ...deprecatedLegacyContext);
}, options);
});
}
function close(overlayId: OverlayItem['id']) {
dispatchOverlay({ type: 'CLOSE', overlayId });
}
function unmount(overlayId: OverlayItem['id']) {
dispatchOverlay({ type: 'REMOVE', overlayId });
}
function closeAll() {
dispatchOverlay({ type: 'CLOSE_ALL' });
}
function unmountAll() {
dispatchOverlay({ type: 'REMOVE_ALL' });
}
export const overlay = { open, close, unmount, closeAll, unmountAll, openAsync };