-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodal-controller.ts
More file actions
49 lines (47 loc) · 1.1 KB
/
Copy pathmodal-controller.ts
File metadata and controls
49 lines (47 loc) · 1.1 KB
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
import type { ModalHandle } from "./factory/types";
import { useModalStore } from "./store";
import { createHandle } from "./utils";
const store = () => useModalStore.getState();
/**
* Utility helpers for managing modals.
*/
export const modalController = {
/**
* Close every modal at once.
* @example
* await modal.closeAll();
*/
closeAll(): Promise<void> {
return store().closeAll();
},
/**
* Get the topmost modal.
* @example
* const topModal = modal.getTop();
* if (topModal) {
* await topModal.close();
* }
*/
getTop<TResult = unknown>(): ModalHandle<TResult> | undefined {
const top = store().getTop();
return top ? createHandle<TResult>(top) : undefined;
},
/**
* Count how many modals are open (excluding closing).
* @example
* const count = modal.getCount();
*/
getCount(): number {
return store().modals.filter((m) => m.open && !m.closing).length;
},
/**
* Check whether any modal is open.
* @example
* if (modal.isOpen()) {
* console.log("A modal is open");
* }
*/
isOpen(): boolean {
return store().modals.some((m) => m.open && !m.closing);
},
};