@okyrychenko-dev/react-modal-manager 0.1.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/LICENSE +21 -0
- package/README.md +625 -0
- package/dist/index.cjs +532 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +123 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +523 -0
- package/dist/index.js.map +1 -0
- package/package.json +92 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type ConfirmModalVariant = "default" | "danger" | "warning" | "success";
|
|
4
|
+
type ConfirmationModalRejectReason = "cancel" | "dismiss";
|
|
5
|
+
interface ConfirmModalParams {
|
|
6
|
+
title: ReactNode;
|
|
7
|
+
description?: ReactNode;
|
|
8
|
+
confirmText?: string;
|
|
9
|
+
cancelText?: string;
|
|
10
|
+
variant?: ConfirmModalVariant;
|
|
11
|
+
dismissible?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ConfirmationModalConfirmedResult {
|
|
14
|
+
confirmed: true;
|
|
15
|
+
}
|
|
16
|
+
interface ConfirmationModalRejectedResult {
|
|
17
|
+
confirmed: false;
|
|
18
|
+
reason: ConfirmationModalRejectReason;
|
|
19
|
+
}
|
|
20
|
+
type ConfirmModalResult = ConfirmationModalConfirmedResult | ConfirmationModalRejectedResult;
|
|
21
|
+
|
|
22
|
+
declare const confirmModal: RegisteredModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
23
|
+
|
|
24
|
+
type ModalId = string;
|
|
25
|
+
type ModalInstanceId = string;
|
|
26
|
+
type ModalDismissReason = "dismiss" | "close-all" | "provider-unmount";
|
|
27
|
+
type ModalInstanceStatus = "open" | "closing";
|
|
28
|
+
interface ModalComponentProps<TInput, TResult> {
|
|
29
|
+
input: TInput;
|
|
30
|
+
instanceId: ModalInstanceId;
|
|
31
|
+
close: (result: TResult) => void;
|
|
32
|
+
dismiss: (reason?: ModalDismissReason) => void;
|
|
33
|
+
reject: (error: unknown) => void;
|
|
34
|
+
}
|
|
35
|
+
type ModalComponent<TInput, TResult> = (props: ModalComponentProps<TInput, TResult>) => ReactNode;
|
|
36
|
+
interface ModalDefinition<TInput, TResult> {
|
|
37
|
+
id: ModalId;
|
|
38
|
+
component: ModalComponent<TInput, TResult>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare class ModalDismissError extends Error {
|
|
42
|
+
readonly reason: ModalDismissReason;
|
|
43
|
+
constructor(reason: ModalDismissReason);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare class ModalRejectError extends Error {
|
|
47
|
+
readonly value: unknown;
|
|
48
|
+
constructor(value: unknown);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface ModalHandle<TResult> extends Promise<TResult> {
|
|
52
|
+
instanceId: ModalInstanceId;
|
|
53
|
+
dismiss: (reason?: ModalDismissReason) => void;
|
|
54
|
+
}
|
|
55
|
+
interface ModalManager {
|
|
56
|
+
open: <TInput, TResult>(modal: ModalDefinition<TInput, TResult>, input: TInput) => ModalHandle<TResult>;
|
|
57
|
+
confirm: (params: ConfirmModalParams) => Promise<ConfirmModalResult>;
|
|
58
|
+
dismiss: (instanceId: ModalInstanceId, reason?: ModalDismissReason) => void;
|
|
59
|
+
closeAll: (reason?: ModalDismissReason) => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare function useModalManager(): ModalManager;
|
|
63
|
+
|
|
64
|
+
interface ModalOptions<TInput, TResult> {
|
|
65
|
+
id?: ModalId;
|
|
66
|
+
component: ModalComponent<TInput, TResult>;
|
|
67
|
+
}
|
|
68
|
+
interface RegisteredModalDefinition<TInput, TResult> extends ModalDefinition<TInput, TResult> {
|
|
69
|
+
open(manager: ModalManager, input: TInput): ModalHandle<TResult>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare function createModal<TInput, TResult>(options: ModalOptions<TInput, TResult>): RegisteredModalDefinition<TInput, TResult>;
|
|
73
|
+
|
|
74
|
+
interface ModalRegistryEntry<TInput, TResult> {
|
|
75
|
+
open(manager: ModalManager, input: TInput): ModalHandle<TResult>;
|
|
76
|
+
}
|
|
77
|
+
type ModalRegistryDefinitions = Readonly<Record<string, ModalRegistryEntry<unknown, unknown>>>;
|
|
78
|
+
type ModalRegistryInput<TDefinition extends ModalRegistryEntry<unknown, unknown>> = Parameters<TDefinition["open"]>[1];
|
|
79
|
+
type ModalRegistryResult<TDefinition extends ModalRegistryEntry<unknown, unknown>> = Awaited<ReturnType<TDefinition["open"]>>;
|
|
80
|
+
interface ModalRegistryBinding {
|
|
81
|
+
isReady: () => boolean;
|
|
82
|
+
}
|
|
83
|
+
interface ModalRegistry<TDefinitions extends ModalRegistryDefinitions> extends ModalRegistryBinding {
|
|
84
|
+
closeAll: ModalManager["closeAll"];
|
|
85
|
+
confirm: ModalManager["confirm"];
|
|
86
|
+
dismiss: ModalManager["dismiss"];
|
|
87
|
+
open: <TKey extends keyof TDefinitions & string>(key: TKey, input: ModalRegistryInput<TDefinitions[TKey]>) => ReturnType<TDefinitions[TKey]["open"]>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
declare function createModalRegistry<const TDefinitions extends ModalRegistryDefinitions>(definitions: TDefinitions): ModalRegistry<TDefinitions>;
|
|
91
|
+
|
|
92
|
+
interface ModalView {
|
|
93
|
+
definitionId: ModalId;
|
|
94
|
+
instanceId: ModalInstanceId;
|
|
95
|
+
status: ModalInstanceStatus;
|
|
96
|
+
}
|
|
97
|
+
interface ModalRendererProps {
|
|
98
|
+
children: ReactNode;
|
|
99
|
+
modal: ModalView;
|
|
100
|
+
}
|
|
101
|
+
type ModalRenderer = (props: ModalRendererProps) => ReactNode;
|
|
102
|
+
interface ModalViewportProps {
|
|
103
|
+
renderer?: ModalRenderer;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
declare function ModalViewport({ renderer: Renderer, }: ModalViewportProps): ReactNode;
|
|
107
|
+
|
|
108
|
+
interface ModalProviderProps {
|
|
109
|
+
children: ReactNode;
|
|
110
|
+
closeDelayMs?: number;
|
|
111
|
+
confirmModal?: ModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
112
|
+
registry?: ModalRegistryBinding;
|
|
113
|
+
renderer?: ModalRenderer;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare function ModalProvider(props: ModalProviderProps): ReactNode;
|
|
117
|
+
|
|
118
|
+
interface ModalRuntimeConfig {
|
|
119
|
+
closeDelayMs: number;
|
|
120
|
+
confirmModal: ModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { type ConfirmModalParams, type ConfirmModalResult, type ConfirmModalVariant, type ModalComponent, type ModalComponentProps, type ModalDefinition, ModalDismissError, type ModalDismissReason, type ModalHandle, type ModalId, type ModalInstanceId, type ModalInstanceStatus, type ModalManager, type ModalOptions, ModalProvider, type ModalProviderProps, type ModalRegistry, type ModalRegistryDefinitions, type ModalRegistryEntry, type ModalRegistryInput, type ModalRegistryResult, ModalRejectError, type ModalRenderer, type ModalRendererProps, type ModalRuntimeConfig, type ModalView, ModalViewport, type ModalViewportProps, type RegisteredModalDefinition, confirmModal, createModal, createModalRegistry, useModalManager };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type ConfirmModalVariant = "default" | "danger" | "warning" | "success";
|
|
4
|
+
type ConfirmationModalRejectReason = "cancel" | "dismiss";
|
|
5
|
+
interface ConfirmModalParams {
|
|
6
|
+
title: ReactNode;
|
|
7
|
+
description?: ReactNode;
|
|
8
|
+
confirmText?: string;
|
|
9
|
+
cancelText?: string;
|
|
10
|
+
variant?: ConfirmModalVariant;
|
|
11
|
+
dismissible?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ConfirmationModalConfirmedResult {
|
|
14
|
+
confirmed: true;
|
|
15
|
+
}
|
|
16
|
+
interface ConfirmationModalRejectedResult {
|
|
17
|
+
confirmed: false;
|
|
18
|
+
reason: ConfirmationModalRejectReason;
|
|
19
|
+
}
|
|
20
|
+
type ConfirmModalResult = ConfirmationModalConfirmedResult | ConfirmationModalRejectedResult;
|
|
21
|
+
|
|
22
|
+
declare const confirmModal: RegisteredModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
23
|
+
|
|
24
|
+
type ModalId = string;
|
|
25
|
+
type ModalInstanceId = string;
|
|
26
|
+
type ModalDismissReason = "dismiss" | "close-all" | "provider-unmount";
|
|
27
|
+
type ModalInstanceStatus = "open" | "closing";
|
|
28
|
+
interface ModalComponentProps<TInput, TResult> {
|
|
29
|
+
input: TInput;
|
|
30
|
+
instanceId: ModalInstanceId;
|
|
31
|
+
close: (result: TResult) => void;
|
|
32
|
+
dismiss: (reason?: ModalDismissReason) => void;
|
|
33
|
+
reject: (error: unknown) => void;
|
|
34
|
+
}
|
|
35
|
+
type ModalComponent<TInput, TResult> = (props: ModalComponentProps<TInput, TResult>) => ReactNode;
|
|
36
|
+
interface ModalDefinition<TInput, TResult> {
|
|
37
|
+
id: ModalId;
|
|
38
|
+
component: ModalComponent<TInput, TResult>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare class ModalDismissError extends Error {
|
|
42
|
+
readonly reason: ModalDismissReason;
|
|
43
|
+
constructor(reason: ModalDismissReason);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare class ModalRejectError extends Error {
|
|
47
|
+
readonly value: unknown;
|
|
48
|
+
constructor(value: unknown);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface ModalHandle<TResult> extends Promise<TResult> {
|
|
52
|
+
instanceId: ModalInstanceId;
|
|
53
|
+
dismiss: (reason?: ModalDismissReason) => void;
|
|
54
|
+
}
|
|
55
|
+
interface ModalManager {
|
|
56
|
+
open: <TInput, TResult>(modal: ModalDefinition<TInput, TResult>, input: TInput) => ModalHandle<TResult>;
|
|
57
|
+
confirm: (params: ConfirmModalParams) => Promise<ConfirmModalResult>;
|
|
58
|
+
dismiss: (instanceId: ModalInstanceId, reason?: ModalDismissReason) => void;
|
|
59
|
+
closeAll: (reason?: ModalDismissReason) => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare function useModalManager(): ModalManager;
|
|
63
|
+
|
|
64
|
+
interface ModalOptions<TInput, TResult> {
|
|
65
|
+
id?: ModalId;
|
|
66
|
+
component: ModalComponent<TInput, TResult>;
|
|
67
|
+
}
|
|
68
|
+
interface RegisteredModalDefinition<TInput, TResult> extends ModalDefinition<TInput, TResult> {
|
|
69
|
+
open(manager: ModalManager, input: TInput): ModalHandle<TResult>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare function createModal<TInput, TResult>(options: ModalOptions<TInput, TResult>): RegisteredModalDefinition<TInput, TResult>;
|
|
73
|
+
|
|
74
|
+
interface ModalRegistryEntry<TInput, TResult> {
|
|
75
|
+
open(manager: ModalManager, input: TInput): ModalHandle<TResult>;
|
|
76
|
+
}
|
|
77
|
+
type ModalRegistryDefinitions = Readonly<Record<string, ModalRegistryEntry<unknown, unknown>>>;
|
|
78
|
+
type ModalRegistryInput<TDefinition extends ModalRegistryEntry<unknown, unknown>> = Parameters<TDefinition["open"]>[1];
|
|
79
|
+
type ModalRegistryResult<TDefinition extends ModalRegistryEntry<unknown, unknown>> = Awaited<ReturnType<TDefinition["open"]>>;
|
|
80
|
+
interface ModalRegistryBinding {
|
|
81
|
+
isReady: () => boolean;
|
|
82
|
+
}
|
|
83
|
+
interface ModalRegistry<TDefinitions extends ModalRegistryDefinitions> extends ModalRegistryBinding {
|
|
84
|
+
closeAll: ModalManager["closeAll"];
|
|
85
|
+
confirm: ModalManager["confirm"];
|
|
86
|
+
dismiss: ModalManager["dismiss"];
|
|
87
|
+
open: <TKey extends keyof TDefinitions & string>(key: TKey, input: ModalRegistryInput<TDefinitions[TKey]>) => ReturnType<TDefinitions[TKey]["open"]>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
declare function createModalRegistry<const TDefinitions extends ModalRegistryDefinitions>(definitions: TDefinitions): ModalRegistry<TDefinitions>;
|
|
91
|
+
|
|
92
|
+
interface ModalView {
|
|
93
|
+
definitionId: ModalId;
|
|
94
|
+
instanceId: ModalInstanceId;
|
|
95
|
+
status: ModalInstanceStatus;
|
|
96
|
+
}
|
|
97
|
+
interface ModalRendererProps {
|
|
98
|
+
children: ReactNode;
|
|
99
|
+
modal: ModalView;
|
|
100
|
+
}
|
|
101
|
+
type ModalRenderer = (props: ModalRendererProps) => ReactNode;
|
|
102
|
+
interface ModalViewportProps {
|
|
103
|
+
renderer?: ModalRenderer;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
declare function ModalViewport({ renderer: Renderer, }: ModalViewportProps): ReactNode;
|
|
107
|
+
|
|
108
|
+
interface ModalProviderProps {
|
|
109
|
+
children: ReactNode;
|
|
110
|
+
closeDelayMs?: number;
|
|
111
|
+
confirmModal?: ModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
112
|
+
registry?: ModalRegistryBinding;
|
|
113
|
+
renderer?: ModalRenderer;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
declare function ModalProvider(props: ModalProviderProps): ReactNode;
|
|
117
|
+
|
|
118
|
+
interface ModalRuntimeConfig {
|
|
119
|
+
closeDelayMs: number;
|
|
120
|
+
confirmModal: ModalDefinition<ConfirmModalParams, ConfirmModalResult>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { type ConfirmModalParams, type ConfirmModalResult, type ConfirmModalVariant, type ModalComponent, type ModalComponentProps, type ModalDefinition, ModalDismissError, type ModalDismissReason, type ModalHandle, type ModalId, type ModalInstanceId, type ModalInstanceStatus, type ModalManager, type ModalOptions, ModalProvider, type ModalProviderProps, type ModalRegistry, type ModalRegistryDefinitions, type ModalRegistryEntry, type ModalRegistryInput, type ModalRegistryResult, ModalRejectError, type ModalRenderer, type ModalRendererProps, type ModalRuntimeConfig, type ModalView, ModalViewport, type ModalViewportProps, type RegisteredModalDefinition, confirmModal, createModal, createModalRegistry, useModalManager };
|