@saas-ui/modals 1.5.5 → 2.0.0-next.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/CHANGELOG.md CHANGED
@@ -1,11 +1,21 @@
1
1
  # @saas-ui/modals
2
2
 
3
- ## 1.5.5
3
+ ## 2.0.0-next.0
4
+
5
+ ### Major Changes
6
+
7
+ - f1e99198: Restructured packages.
4
8
 
5
9
  ### Patch Changes
6
10
 
7
- - Updated dependencies [382e095]
8
- - @saas-ui/forms@1.5.3
11
+ - f1e99198: Migrated from microbundle to tsup for builds
12
+ - Updated dependencies [d7c87a31]
13
+ - Updated dependencies [f1e99198]
14
+ - Updated dependencies [8b82d945]
15
+ - Updated dependencies [f1e99198]
16
+ - @saas-ui/forms@2.0.0-next.0
17
+ - @saas-ui/hooks@2.0.0-next.0
18
+ - @saas-ui/react-utils@2.0.0-next.0
9
19
 
10
20
  ## 1.5.4
11
21
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,218 @@
1
- export * from './dialog';
2
- export * from './drawer';
3
- export * from './modal';
4
- export * from './menu';
5
- export * from './form';
6
- export * from './provider';
7
- //# sourceMappingURL=index.d.ts.map
1
+ import * as React from 'react';
2
+ import { AlertDialogProps, ButtonProps, ButtonGroupProps, DrawerProps as DrawerProps$1, ModalProps, MenuListProps } from '@chakra-ui/react';
3
+ import * as _chakra_ui_system_dist_system_types from '@chakra-ui/system/dist/system.types';
4
+ import { FieldValues, FormProps, FieldResolver } from '@saas-ui/forms';
5
+
6
+ interface ConfirmDialogProps extends Omit<AlertDialogProps, 'leastDestructiveRef'> {
7
+ /**
8
+ * The dialog title
9
+ */
10
+ title?: React.ReactNode;
11
+ /**
12
+ * The cancel button label
13
+ */
14
+ cancelLabel?: React.ReactNode;
15
+ /**
16
+ * The confirm button label
17
+ */
18
+ confirmLabel?: React.ReactNode;
19
+ /**
20
+ * The cancel button props
21
+ */
22
+ cancelProps?: ButtonProps;
23
+ /**
24
+ * The confirm button props
25
+ */
26
+ confirmProps?: ButtonProps;
27
+ /**
28
+ * The button group props
29
+ */
30
+ buttonGroupProps?: ButtonGroupProps;
31
+ /**
32
+ * Close the dialog on cancel
33
+ * @default true
34
+ */
35
+ closeOnCancel?: boolean;
36
+ /**
37
+ * Close the dialog on confirm
38
+ * @default true
39
+ */
40
+ closeOnConfirm?: boolean;
41
+ /**
42
+ * Defines which button gets initial focus
43
+ * https://www.w3.org/TR/wai-aria-practices/#alertdialog
44
+ */
45
+ leastDestructiveFocus?: 'cancel' | 'confirm';
46
+ /**
47
+ * Function that's called when cancel is clicked
48
+ */
49
+ onCancel?: () => void;
50
+ /**
51
+ * Function that's called when confirm is clicked
52
+ */
53
+ onConfirm?: () => void;
54
+ }
55
+ declare const ConfirmDialog: React.FC<ConfirmDialogProps>;
56
+
57
+ interface BaseDrawerProps extends Omit<DrawerProps$1, 'children'> {
58
+ /**
59
+ * The drawer title
60
+ */
61
+ title: React.ReactNode;
62
+ /**
63
+ * Hide the close button
64
+ */
65
+ hideCloseButton?: boolean;
66
+ /**
67
+ * Hide the overflow
68
+ */
69
+ hideOverlay?: boolean;
70
+ children?: React.ReactNode;
71
+ }
72
+ declare const BaseDrawer: React.FC<BaseDrawerProps>;
73
+ interface DrawerProps extends BaseDrawerProps {
74
+ /**
75
+ * Drawer footer content, wrapped with `DrawerFooter`
76
+ */
77
+ footer?: React.ReactNode;
78
+ }
79
+ declare const Drawer: React.FC<DrawerProps>;
80
+
81
+ interface BaseModalProps extends ModalProps {
82
+ /**
83
+ * The modal title
84
+ */
85
+ title?: React.ReactNode;
86
+ /**
87
+ * The modal footer
88
+ */
89
+ footer?: React.ReactNode;
90
+ /**
91
+ * Hide the close button
92
+ */
93
+ hideCloseButton?: boolean;
94
+ /**
95
+ * Hide the overlay
96
+ */
97
+ hideOverlay?: boolean;
98
+ }
99
+ declare const BaseModal: React.FC<BaseModalProps>;
100
+ declare const Modal: React.FC<BaseModalProps>;
101
+
102
+ interface MenuDialogProps extends BaseModalProps {
103
+ /**
104
+ * The modal footer, wrapped with `ModalFooter`
105
+ */
106
+ footer?: React.ReactNode;
107
+ }
108
+ declare const MenuDialog: React.FC<MenuDialogProps>;
109
+ interface MenuDialogListProps extends Omit<BaseModalProps, 'isOpen' | 'onClose' | 'children' | 'scrollBehavior'>, Omit<MenuListProps, 'title'> {
110
+ }
111
+ declare const MenuDialogList: _chakra_ui_system_dist_system_types.ComponentWithAs<"div", MenuDialogListProps>;
112
+
113
+ interface FormDialogProps<TFieldValues extends FieldValues = FieldValues, TContext extends object = object> extends Omit<BaseModalProps, 'children'>, Pick<FormProps<TFieldValues, TContext>, 'schema' | 'defaultValues' | 'values' | 'context' | 'onChange' | 'onSubmit' | 'onError' | 'resolver' | 'mode' | 'reValidateMode' | 'shouldFocusError' | 'shouldUnregister' | 'shouldUseNativeValidation' | 'criteriaMode' | 'delayError'> {
114
+ /**
115
+ * The modal footer, will be wrapped with `ModalFooter`.
116
+ * Defaults to a cancel and submit button.
117
+ */
118
+ footer?: React.ReactNode;
119
+ /**
120
+ * The cancel button label
121
+ * @default "Cancel"
122
+ */
123
+ cancelLabel?: React.ReactNode;
124
+ /**
125
+ * The submit button label
126
+ * @default "Submit"
127
+ */
128
+ submitLabel?: React.ReactNode;
129
+ /**
130
+ * If no children are passed, this will auto render fields based on the supplied schema.
131
+ */
132
+ children?: React.ReactNode;
133
+ /**
134
+ * A schema field resolver used to auto generate form fields.
135
+ */
136
+ fieldResolver?: FieldResolver;
137
+ }
138
+ declare const FormDialog: <TFieldValues extends FieldValues>(props: FormDialogProps<TFieldValues, object> & {
139
+ ref?: React.ForwardedRef<HTMLFormElement> | undefined;
140
+ }) => React.ReactElement;
141
+
142
+ interface ModalsContextValue {
143
+ open: (options: OpenOptions) => ModalId;
144
+ drawer: (options: DrawerOptions) => ModalId;
145
+ alert: (options: ConfirmDialogOptions) => ModalId;
146
+ confirm: (options: ConfirmDialogOptions) => ModalId;
147
+ menu: (options: MenuDialogOptions) => ModalId;
148
+ form: (options: FormDialogOptions) => ModalId;
149
+ close: (id: ModalId) => void;
150
+ closeAll: () => void;
151
+ }
152
+ declare const ModalsContext: React.Context<ModalsContextValue | null>;
153
+ interface ModalsProviderProps {
154
+ children: React.ReactNode;
155
+ modals?: Record<string, React.FC<any>>;
156
+ }
157
+ type ModalId = string | number;
158
+ interface ModalOptions extends Omit<BaseModalProps, 'onClose' | 'isOpen' | 'children'> {
159
+ onClose?: (args: {
160
+ force?: boolean;
161
+ }) => Promise<boolean | undefined> | void;
162
+ body?: React.ReactNode;
163
+ children?: React.ReactNode;
164
+ [key: string]: any;
165
+ }
166
+ interface DrawerOptions extends ModalOptions, Omit<DrawerProps, 'onClose' | 'isOpen' | 'children' | 'title' | 'size'> {
167
+ }
168
+ interface ConfirmDialogOptions extends ModalOptions, Omit<ConfirmDialogProps, 'onClose' | 'isOpen' | 'children'> {
169
+ }
170
+ interface MenuDialogOptions extends ModalOptions, Omit<MenuDialogProps, 'onClose' | 'isOpen' | 'children'> {
171
+ }
172
+ interface FormDialogOptions extends ModalOptions, Omit<FormDialogProps, 'onClose' | 'isOpen' | 'children'> {
173
+ }
174
+ interface OpenOptions extends ModalOptions {
175
+ type?: ModalTypes;
176
+ scope?: ModalScopes;
177
+ }
178
+ type ModalScopes = 'modal' | 'alert';
179
+ type ModalTypes = 'modal' | 'drawer' | 'alert' | 'confirm' | 'menu' | string;
180
+ interface ModalConfig<TModalOptions extends ModalOptions = ModalOptions> {
181
+ /**
182
+ * The modal id, autogenerated when not set.
183
+ * Can be used to close modals.
184
+ */
185
+ id?: ModalId | null;
186
+ /**
187
+ * The modal props
188
+ */
189
+ props?: TModalOptions | null;
190
+ /**
191
+ * The modal scope
192
+ * Modals can only have one level per scope.
193
+ * The default scopes are 'modal' and 'alert', alerts can be openend above modals.
194
+ */
195
+ scope?: ModalScopes | string;
196
+ /**
197
+ * The modal type to open.
198
+ * Build in types are 'modal', 'drawer', 'alert', 'confirm'
199
+ *
200
+ * Custom types can be configured using the `modals` prop of `ModalProvider`
201
+ */
202
+ type?: ModalTypes;
203
+ /**
204
+ * Render a custom modal component.
205
+ * This will ignore the `type` param.
206
+ */
207
+ component?: React.FC<BaseModalProps>;
208
+ /**
209
+ * Whether the modal is open or not.
210
+ * This is used internally to keep track of the modal state.
211
+ */
212
+ isOpen?: boolean;
213
+ }
214
+ declare function ModalsProvider({ children, modals }: ModalsProviderProps): JSX.Element;
215
+ declare const useModalsContext: () => ModalsContextValue;
216
+ declare const useModals: () => ModalsContextValue;
217
+
218
+ export { BaseDrawer, BaseDrawerProps, BaseModal, BaseModalProps, ConfirmDialog, ConfirmDialogOptions, ConfirmDialogProps, Drawer, DrawerOptions, DrawerProps, FormDialog, FormDialogOptions, FormDialogProps, MenuDialog, MenuDialogList, MenuDialogListProps, MenuDialogOptions, MenuDialogProps, Modal, ModalConfig, ModalId, ModalScopes, ModalTypes, ModalsContext, ModalsContextValue, ModalsProvider, OpenOptions, useModals, useModalsContext };
package/dist/index.js CHANGED
@@ -1,2 +1,433 @@
1
- var e=require("react"),n=require("@chakra-ui/react"),o=require("@saas-ui/button"),r=require("@chakra-ui/utils"),t=require("@saas-ui/forms");function l(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach(function(o){if("default"!==o){var r=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(n,o,r.get?r:{enumerable:!0,get:function(){return e[o]}})}}),n.default=e,n}var i=/*#__PURE__*/l(e);function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var o=arguments[n];for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r])}return e},a.apply(this,arguments)}function u(e,n){if(null==e)return{};var o,r,t={},l=Object.keys(e);for(r=0;r<l.length;r++)n.indexOf(o=l[r])>=0||(t[o]=e[o]);return t}var c=["title","cancelLabel","confirmLabel","cancelProps","confirmProps","buttonGroupProps","isOpen","closeOnCancel","closeOnConfirm","leastDestructiveFocus","onClose","onCancel","onConfirm","children"],s=function(e){var r=e.title,t=e.cancelLabel,l=void 0===t?"Cancel":t,s=e.confirmLabel,d=void 0===s?"Confirm":s,f=e.cancelProps,m=e.confirmProps,p=e.buttonGroupProps,v=e.isOpen,C=e.closeOnCancel,h=void 0===C||C,E=e.closeOnConfirm,y=void 0===E||E,O=e.leastDestructiveFocus,b=void 0===O?"cancel":O,M=e.onClose,g=e.onCancel,D=e.onConfirm,x=e.children,F=u(e,c),B=i.useRef(null),P=i.useRef(null);/*#__PURE__*/return i.createElement(n.AlertDialog,a({isOpen:v,onClose:M},F,{leastDestructiveRef:"cancel"===b?B:P}),/*#__PURE__*/i.createElement(n.AlertDialogOverlay,null,/*#__PURE__*/i.createElement(n.AlertDialogContent,null,/*#__PURE__*/i.createElement(n.AlertDialogHeader,null,r),/*#__PURE__*/i.createElement(n.AlertDialogBody,null,x),/*#__PURE__*/i.createElement(n.AlertDialogFooter,null,/*#__PURE__*/i.createElement(o.ButtonGroup,p,/*#__PURE__*/i.createElement(o.Button,a({ref:B},f,{onClick:function(){null==g||g(),h&&M()}}),(null==f?void 0:f.children)||(null==f?void 0:f.label)||l),/*#__PURE__*/i.createElement(o.Button,a({ref:P},m,{onClick:function(){null==D||D(),y&&M()}}),(null==m?void 0:m.children)||(null==m?void 0:m.label)||d))))))},d=["title","children","isOpen","onClose","hideCloseButton","hideOverlay"],f=["footer","children"],m=function(e){var o=e.title,r=e.children,t=e.isOpen,l=e.onClose,c=e.hideCloseButton,s=e.hideOverlay,f=u(e,d);/*#__PURE__*/return i.createElement(n.Drawer,a({isOpen:t,onClose:l},f),!s&&/*#__PURE__*/i.createElement(n.DrawerOverlay,null),/*#__PURE__*/i.createElement(n.DrawerContent,null,/*#__PURE__*/i.createElement(n.DrawerHeader,null,o),!c&&/*#__PURE__*/i.createElement(n.DrawerCloseButton,null),r))},p=function(e){var o=e.footer,r=e.children,t=u(e,f);/*#__PURE__*/return i.createElement(m,t,/*#__PURE__*/i.createElement(n.DrawerBody,null,r),o&&/*#__PURE__*/i.createElement(n.DrawerFooter,null,o))},v=["title","footer","children","isOpen","onClose","hideCloseButton","hideOverlay"],C=["children"],h=function(e){var o=e.title,r=e.footer,t=e.children,l=e.isOpen,c=e.onClose,s=e.hideCloseButton,d=e.hideOverlay,f=u(e,v);/*#__PURE__*/return i.createElement(n.Modal,a({isOpen:l,onClose:c},f),!d&&/*#__PURE__*/i.createElement(n.ModalOverlay,null),/*#__PURE__*/i.createElement(n.ModalContent,null,o&&/*#__PURE__*/i.createElement(n.ModalHeader,null,o),!s&&/*#__PURE__*/i.createElement(n.ModalCloseButton,null),t,r&&/*#__PURE__*/i.createElement(n.ModalFooter,null,r)))},E=function(e){var o=e.children,r=u(e,C);/*#__PURE__*/return i.createElement(h,r,/*#__PURE__*/i.createElement(n.ModalBody,null,o))},y=["onClose","onCloseComplete"],O=["rootProps","title","footer","initialFocusRef","hideCloseButton","motionPreset"],b=["ref"],M=n.createStylesContext("MenuDialog")[0],g=function(e){var o=e.onClose,r=e.onCloseComplete,t=u(e,y);/*#__PURE__*/return i.createElement(n.Menu,a({variant:"dialog",onClose:function(){null==o||o(),null==r||r()}},t))},D=n.forwardRef(function(e,o){var r=e.title,t=e.footer,l=e.initialFocusRef,c=e.hideCloseButton,s=e.motionPreset,d=u(e,O),f=n.useMenuContext(),m=f.isOpen,p=f.onClose,v=f.menuRef,C=n.useMenuList(d,o),E=C.ref,y=u(C,b),g=n.useMultiStyleConfig("Menu",e);/*#__PURE__*/return i.createElement(h,{isOpen:m,onClose:p,initialFocusRef:l||v,title:r,hideCloseButton:c,motionPreset:s},/*#__PURE__*/i.createElement(M,{value:g},/*#__PURE__*/i.createElement(n.chakra.div,a({},y,{ref:E,__css:a({outline:0,maxHeight:"80vh",overflowY:"auto"},g.list,{boxShadow:"none",border:0})}))),t&&/*#__PURE__*/i.createElement(n.ModalFooter,null,t))}),x=["children","schema","resolver","fieldResolver","defaultValues","onChange","onSubmit","onError","mode","reValidateMode","shouldFocusError","shouldUnregister","shouldUseNativeValidation","criteriaMode","delayError","cancelLabel","submitLabel","footer","isOpen","onClose"],F=n.forwardRef(function(e,o){var l=e.children,c=e.schema,s=e.resolver,d=e.fieldResolver,f=e.defaultValues,m=e.onChange,p=e.onSubmit,v=e.onError,C=e.mode,E=e.reValidateMode,y=e.shouldFocusError,O=void 0===y||y,b=e.shouldUnregister,M=e.shouldUseNativeValidation,g=e.criteriaMode,D=e.delayError,F=void 0===D?100:D,B=e.cancelLabel,P=e.submitLabel,w=e.footer,R=e.isOpen,j=e.onClose,L=u(e,x),S={ref:o,schema:c,resolver:s,defaultValues:f,onChange:m,onSubmit:p,onError:v,mode:C,reValidateMode:E,shouldFocusError:O,shouldUnregister:b,shouldUseNativeValidation:M,criteriaMode:g,delayError:F};/*#__PURE__*/return i.createElement(h,a({isOpen:R,onClose:j},L),/*#__PURE__*/i.createElement(t.Form,S,function(e){/*#__PURE__*/return i.createElement(i.Fragment,null,/*#__PURE__*/i.createElement(n.ModalBody,null,r.runIfFn(l,e)||/*#__PURE__*/i.createElement(t.Fields,{schema:c,fieldResolver:d,focusFirstField:!0})),w||/*#__PURE__*/i.createElement(n.ModalFooter,null,/*#__PURE__*/i.createElement(n.Button,{variant:"ghost",mr:3,onClick:j},B||"Cancel"),/*#__PURE__*/i.createElement(t.SubmitButton,null,P||"Submit")))}))}),B=["id","type","scope","component"],P=["title","body","children"],w=i.createContext(null),R={id:null,props:null,type:"modal"},j={alert:s,confirm:s,drawer:p,modal:E,menu:g,form:F},L=function(){return i.useContext(w)};exports.BaseDrawer=m,exports.BaseModal=h,exports.ConfirmDialog=s,exports.Drawer=p,exports.FormDialog=F,exports.MenuDialog=g,exports.MenuDialogList=D,exports.Modal=E,exports.ModalsContext=w,exports.ModalsProvider=function(e){var n=e.children,o=e.modals,r=i.useMemo(function(){return new Set},[]),t=i.useState({modal:R}),l=t[0],c=t[1],s=i.useMemo(function(){var e=a({},j,o);return function(n){return void 0===n&&(n="modal"),e[n]||e.modal}},[o]),d=function(e,n){if(!n)return c({modal:e});c(function(o){var r;return a({},o,((r={})[n]=e,r))})},f=function(e){"function"==typeof e&&(e={component:e});var n=e.id,o=void 0===n?r.size+1:n,t=e.type,l=void 0===t?"modal":t,i=e.scope,a=void 0===i?"modal":i,c=e.component,s={id:o,props:u(e,B),type:l,scope:a,component:c,isOpen:!0};return r.add(s),d(s,a),o},m=function(e,n){try{var o,t=[].concat(Array.from(r)),l=t.filter(function(n){return n.id===e})[0];return l?Promise.resolve(null==(o=l.props)||null==o.onClose?void 0:o.onClose({force:n})).then(function(e){if(!1!==e){var n=t.filter(function(e){return e.scope===l.scope});d(1===n.length?a({},l,{isOpen:!1}):n.length>1?n[n.length-2]:{id:null,props:null,type:l.type},l.scope)}}):Promise.resolve()}catch(e){return Promise.reject(e)}},p={open:f,drawer:function(e){return f(a({},e,{type:"drawer"}))},alert:function(e){return f(a({},e,{scope:"alert",type:"alert",cancelProps:{display:"none"},confirmProps:{label:"OK"},leastDestructiveFocus:"confirm"}))},confirm:function(e){return f(a({},e,{scope:"alert",type:"confirm"}))},menu:function(e){return f(a({},e,{type:"menu"}))},form:function(e){return f(a({},e,{type:"form"}))},close:m,closeAll:function(){r.forEach(function(e){var n;return null==(n=e.props)||null==n.onClose?void 0:n.onClose({force:!0})}),r.clear(),d(R)}},v=i.useMemo(function(){return Object.entries(l).map(function(e){var n=e[0],o=e[1],t=o.component||s(o.type),l=o.props||{},c=l.title,f=l.body,p=l.children,v=u(l,P);/*#__PURE__*/return i.createElement(t,a({key:n,title:c,children:f||p},v,{isOpen:!!o.isOpen,onClose:function(){return m(o.id)},onCloseComplete:function(){return function(e){var n=[].concat(Array.from(r)),o=n.filter(function(n){return n.id===e})[0];r.delete(o),1===n.filter(function(e){return e.scope===o.scope}).length&&d(R,o.scope)}(o.id)}}))})},[l]);/*#__PURE__*/return i.createElement(w.Provider,{value:p},v,n)},exports.useModals=function(){return L()},exports.useModalsContext=L;
2
- //# sourceMappingURL=index.js.map
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var react = require('@chakra-ui/react');
5
+ var reactUtils = require('@saas-ui/react-utils');
6
+ var forms = require('@saas-ui/forms');
7
+
8
+ function _interopNamespaceDefault(e) {
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
26
+
27
+ // src/dialog.tsx
28
+ var ConfirmDialog = (props) => {
29
+ const {
30
+ title,
31
+ cancelLabel = "Cancel",
32
+ confirmLabel = "Confirm",
33
+ cancelProps,
34
+ confirmProps,
35
+ buttonGroupProps,
36
+ isOpen,
37
+ closeOnCancel = true,
38
+ closeOnConfirm = true,
39
+ leastDestructiveFocus = "cancel",
40
+ onClose,
41
+ onCancel,
42
+ onConfirm,
43
+ children,
44
+ ...rest
45
+ } = props;
46
+ const cancelRef = React__namespace.useRef(null);
47
+ const confirmRef = React__namespace.useRef(null);
48
+ return /* @__PURE__ */ React__namespace.createElement(react.AlertDialog, {
49
+ isOpen,
50
+ onClose,
51
+ ...rest,
52
+ leastDestructiveRef: leastDestructiveFocus === "cancel" ? cancelRef : confirmRef
53
+ }, /* @__PURE__ */ React__namespace.createElement(react.AlertDialogOverlay, null, /* @__PURE__ */ React__namespace.createElement(react.AlertDialogContent, null, /* @__PURE__ */ React__namespace.createElement(react.AlertDialogHeader, null, title), /* @__PURE__ */ React__namespace.createElement(react.AlertDialogBody, null, children), /* @__PURE__ */ React__namespace.createElement(react.AlertDialogFooter, null, /* @__PURE__ */ React__namespace.createElement(react.ButtonGroup, {
54
+ ...buttonGroupProps
55
+ }, /* @__PURE__ */ React__namespace.createElement(react.Button, {
56
+ ref: cancelRef,
57
+ ...cancelProps,
58
+ onClick: () => {
59
+ onCancel == null ? void 0 : onCancel();
60
+ closeOnCancel && onClose();
61
+ }
62
+ }, (cancelProps == null ? void 0 : cancelProps.children) || cancelLabel), /* @__PURE__ */ React__namespace.createElement(react.Button, {
63
+ ref: confirmRef,
64
+ ...confirmProps,
65
+ onClick: () => {
66
+ onConfirm == null ? void 0 : onConfirm();
67
+ closeOnConfirm && onClose();
68
+ }
69
+ }, (confirmProps == null ? void 0 : confirmProps.children) || confirmLabel))))));
70
+ };
71
+ var BaseDrawer = (props) => {
72
+ const {
73
+ title,
74
+ children,
75
+ isOpen,
76
+ onClose,
77
+ hideCloseButton,
78
+ hideOverlay,
79
+ ...rest
80
+ } = props;
81
+ return /* @__PURE__ */ React__namespace.createElement(react.Drawer, {
82
+ isOpen,
83
+ onClose,
84
+ ...rest
85
+ }, !hideOverlay && /* @__PURE__ */ React__namespace.createElement(react.DrawerOverlay, null), /* @__PURE__ */ React__namespace.createElement(react.DrawerContent, null, /* @__PURE__ */ React__namespace.createElement(react.DrawerHeader, null, title), !hideCloseButton && /* @__PURE__ */ React__namespace.createElement(react.DrawerCloseButton, null), children));
86
+ };
87
+ var Drawer = (props) => {
88
+ const { footer, children, ...rest } = props;
89
+ return /* @__PURE__ */ React__namespace.createElement(BaseDrawer, {
90
+ ...rest
91
+ }, /* @__PURE__ */ React__namespace.createElement(react.DrawerBody, null, children), footer && /* @__PURE__ */ React__namespace.createElement(react.DrawerFooter, null, footer));
92
+ };
93
+ var BaseModal = (props) => {
94
+ const {
95
+ title,
96
+ footer,
97
+ children,
98
+ isOpen,
99
+ onClose,
100
+ hideCloseButton,
101
+ hideOverlay,
102
+ ...rest
103
+ } = props;
104
+ return /* @__PURE__ */ React__namespace.createElement(react.Modal, {
105
+ isOpen,
106
+ onClose,
107
+ ...rest
108
+ }, !hideOverlay && /* @__PURE__ */ React__namespace.createElement(react.ModalOverlay, null), /* @__PURE__ */ React__namespace.createElement(react.ModalContent, null, title && /* @__PURE__ */ React__namespace.createElement(react.ModalHeader, null, title), !hideCloseButton && /* @__PURE__ */ React__namespace.createElement(react.ModalCloseButton, null), children, footer && /* @__PURE__ */ React__namespace.createElement(react.ModalFooter, null, footer)));
109
+ };
110
+ var Modal = (props) => {
111
+ const { children, ...rest } = props;
112
+ return /* @__PURE__ */ React__namespace.createElement(BaseModal, {
113
+ ...rest
114
+ }, /* @__PURE__ */ React__namespace.createElement(react.ModalBody, null, children));
115
+ };
116
+ var [StylesProvider] = react.createStylesContext("SuiMenuDialog");
117
+ var MenuDialog = (props) => {
118
+ const { onClose, onCloseComplete, ...rest } = props;
119
+ return /* @__PURE__ */ React__namespace.createElement(react.Menu, {
120
+ variant: "dialog",
121
+ onClose: () => {
122
+ onClose == null ? void 0 : onClose();
123
+ onCloseComplete == null ? void 0 : onCloseComplete();
124
+ },
125
+ ...rest
126
+ });
127
+ };
128
+ var MenuDialogList = react.forwardRef(
129
+ (props, forwardedRef) => {
130
+ const {
131
+ rootProps,
132
+ title,
133
+ footer,
134
+ initialFocusRef,
135
+ hideCloseButton,
136
+ motionPreset,
137
+ ...rest
138
+ } = props;
139
+ const { isOpen, onClose, menuRef } = react.useMenuContext();
140
+ const { ref, ...ownProps } = react.useMenuList(rest, forwardedRef);
141
+ const styles = react.useMultiStyleConfig("Menu", props);
142
+ return /* @__PURE__ */ React__namespace.createElement(BaseModal, {
143
+ isOpen,
144
+ onClose,
145
+ initialFocusRef: initialFocusRef || menuRef,
146
+ title,
147
+ hideCloseButton,
148
+ motionPreset
149
+ }, /* @__PURE__ */ React__namespace.createElement(StylesProvider, {
150
+ value: styles
151
+ }, /* @__PURE__ */ React__namespace.createElement(react.chakra.div, {
152
+ ...ownProps,
153
+ ref,
154
+ __css: {
155
+ outline: 0,
156
+ maxHeight: "80vh",
157
+ overflowY: "auto",
158
+ ...styles.list,
159
+ boxShadow: "none",
160
+ border: 0
161
+ }
162
+ })), footer && /* @__PURE__ */ React__namespace.createElement(react.ModalFooter, null, footer));
163
+ }
164
+ );
165
+ var FormDialog = react.forwardRef(
166
+ (props, ref) => {
167
+ const {
168
+ children,
169
+ schema,
170
+ resolver,
171
+ fieldResolver,
172
+ defaultValues,
173
+ values,
174
+ context,
175
+ onChange,
176
+ onSubmit,
177
+ onError,
178
+ mode,
179
+ reValidateMode,
180
+ shouldFocusError = true,
181
+ shouldUnregister,
182
+ shouldUseNativeValidation,
183
+ criteriaMode,
184
+ delayError = 100,
185
+ cancelLabel = "Cancel",
186
+ submitLabel = "Submit",
187
+ footer,
188
+ isOpen,
189
+ onClose,
190
+ ...rest
191
+ } = props;
192
+ const formProps = {
193
+ ref,
194
+ schema,
195
+ resolver,
196
+ defaultValues,
197
+ values,
198
+ context,
199
+ onChange,
200
+ onSubmit,
201
+ onError,
202
+ mode,
203
+ reValidateMode,
204
+ shouldFocusError,
205
+ shouldUnregister,
206
+ shouldUseNativeValidation,
207
+ criteriaMode,
208
+ delayError
209
+ };
210
+ return /* @__PURE__ */ React__namespace.createElement(BaseModal, {
211
+ isOpen,
212
+ onClose,
213
+ ...rest
214
+ }, /* @__PURE__ */ React__namespace.createElement(forms.Form, {
215
+ ...formProps,
216
+ ref
217
+ }, (form) => /* @__PURE__ */ React__namespace.createElement(React__namespace.Fragment, null, /* @__PURE__ */ React__namespace.createElement(react.ModalBody, null, reactUtils.runIfFn(children, form) || /* @__PURE__ */ React__namespace.createElement(forms.Fields, {
218
+ schema,
219
+ fieldResolver,
220
+ focusFirstField: true
221
+ })), footer || /* @__PURE__ */ React__namespace.createElement(react.ModalFooter, null, /* @__PURE__ */ React__namespace.createElement(react.Button, {
222
+ variant: "ghost",
223
+ mr: 3,
224
+ onClick: onClose
225
+ }, cancelLabel), /* @__PURE__ */ React__namespace.createElement(forms.SubmitButton, null, submitLabel)))));
226
+ }
227
+ );
228
+ var ModalsContext = React__namespace.createContext(
229
+ null
230
+ );
231
+ var initialModalState = {
232
+ id: null,
233
+ props: null,
234
+ type: "modal"
235
+ };
236
+ var defaultModals = {
237
+ alert: ConfirmDialog,
238
+ confirm: ConfirmDialog,
239
+ drawer: Drawer,
240
+ modal: Modal,
241
+ menu: MenuDialog,
242
+ form: FormDialog
243
+ };
244
+ function ModalsProvider({ children, modals }) {
245
+ const _instances = React__namespace.useMemo(() => /* @__PURE__ */ new Set(), []);
246
+ const [activeModals, setActiveModals] = React__namespace.useState({
247
+ modal: initialModalState
248
+ });
249
+ const getModalComponent = React__namespace.useMemo(() => {
250
+ const _modals = {
251
+ ...defaultModals,
252
+ ...modals
253
+ };
254
+ return (type = "modal") => {
255
+ const component = _modals[type] || _modals.modal;
256
+ return component;
257
+ };
258
+ }, [modals]);
259
+ const setActiveModal = (modal, scope) => {
260
+ if (!scope) {
261
+ return setActiveModals({
262
+ modal
263
+ });
264
+ }
265
+ setActiveModals((prevState) => ({
266
+ ...prevState,
267
+ [scope]: modal
268
+ }));
269
+ };
270
+ const open = (options) => {
271
+ if (typeof options === "function") {
272
+ const component2 = options;
273
+ options = {
274
+ component: component2
275
+ };
276
+ }
277
+ const {
278
+ id = _instances.size + 1,
279
+ type = "modal",
280
+ scope = "modal",
281
+ component,
282
+ ...props
283
+ } = options;
284
+ const modal = {
285
+ id,
286
+ props,
287
+ type,
288
+ scope,
289
+ component,
290
+ isOpen: true
291
+ };
292
+ _instances.add(modal);
293
+ setActiveModal(modal, scope);
294
+ return id;
295
+ };
296
+ const drawer = (options) => {
297
+ return open({
298
+ ...options,
299
+ type: "drawer"
300
+ });
301
+ };
302
+ const alert = (options) => {
303
+ return open({
304
+ ...options,
305
+ scope: "alert",
306
+ type: "alert",
307
+ cancelProps: {
308
+ display: "none"
309
+ },
310
+ confirmProps: {
311
+ label: "OK"
312
+ },
313
+ leastDestructiveFocus: "confirm"
314
+ });
315
+ };
316
+ const confirm = (options) => {
317
+ return open({
318
+ ...options,
319
+ scope: "alert",
320
+ type: "confirm"
321
+ });
322
+ };
323
+ const menu = (options) => {
324
+ return open({
325
+ ...options,
326
+ type: "menu"
327
+ });
328
+ };
329
+ const form = (options) => {
330
+ return open({
331
+ ...options,
332
+ type: "form"
333
+ });
334
+ };
335
+ const close = async (id, force) => {
336
+ var _a, _b;
337
+ const modals2 = [...Array.from(_instances)];
338
+ const modal = modals2.filter((modal2) => modal2.id === id)[0];
339
+ if (!modal) {
340
+ return;
341
+ }
342
+ const shouldClose = await ((_b = (_a = modal.props) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a, { force }));
343
+ if (shouldClose === false) {
344
+ return;
345
+ }
346
+ const scoped = modals2.filter(({ scope }) => scope === modal.scope);
347
+ if (scoped.length === 1) {
348
+ setActiveModal(
349
+ {
350
+ ...modal,
351
+ isOpen: false
352
+ },
353
+ modal.scope
354
+ );
355
+ } else if (scoped.length > 1) {
356
+ setActiveModal(scoped[scoped.length - 2], modal.scope);
357
+ } else {
358
+ setActiveModal(
359
+ {
360
+ id: null,
361
+ props: null,
362
+ type: modal.type
363
+ },
364
+ modal.scope
365
+ );
366
+ }
367
+ };
368
+ const closeComplete = (id) => {
369
+ const modals2 = [...Array.from(_instances)];
370
+ const modal = modals2.filter((modal2) => modal2.id === id)[0];
371
+ _instances.delete(modal);
372
+ const scoped = modals2.filter(({ scope }) => scope === modal.scope);
373
+ if (scoped.length === 1) {
374
+ setActiveModal(initialModalState, modal.scope);
375
+ }
376
+ };
377
+ const closeAll = () => {
378
+ _instances.forEach((modal) => {
379
+ var _a, _b;
380
+ return (_b = (_a = modal.props) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a, { force: true });
381
+ });
382
+ _instances.clear();
383
+ setActiveModal(initialModalState);
384
+ };
385
+ const context = {
386
+ open,
387
+ drawer,
388
+ alert,
389
+ confirm,
390
+ menu,
391
+ form,
392
+ close,
393
+ closeAll
394
+ };
395
+ const content = React__namespace.useMemo(
396
+ () => Object.entries(activeModals).map(([scope, config]) => {
397
+ const Component = config.component || getModalComponent(config.type);
398
+ const { title, body, children: children2, ...props } = config.props || {};
399
+ return /* @__PURE__ */ React__namespace.createElement(Component, {
400
+ key: scope,
401
+ title,
402
+ children: body || children2,
403
+ ...props,
404
+ isOpen: !!config.isOpen,
405
+ onClose: () => close(config.id),
406
+ onCloseComplete: () => closeComplete(config.id)
407
+ });
408
+ }),
409
+ [activeModals]
410
+ );
411
+ return /* @__PURE__ */ React__namespace.createElement(ModalsContext.Provider, {
412
+ value: context
413
+ }, content, children);
414
+ }
415
+ var useModalsContext = () => React__namespace.useContext(ModalsContext);
416
+ var useModals = () => {
417
+ return useModalsContext();
418
+ };
419
+
420
+ exports.BaseDrawer = BaseDrawer;
421
+ exports.BaseModal = BaseModal;
422
+ exports.ConfirmDialog = ConfirmDialog;
423
+ exports.Drawer = Drawer;
424
+ exports.FormDialog = FormDialog;
425
+ exports.MenuDialog = MenuDialog;
426
+ exports.MenuDialogList = MenuDialogList;
427
+ exports.Modal = Modal;
428
+ exports.ModalsContext = ModalsContext;
429
+ exports.ModalsProvider = ModalsProvider;
430
+ exports.useModals = useModals;
431
+ exports.useModalsContext = useModalsContext;
432
+ //# sourceMappingURL=out.js.map
433
+ //# sourceMappingURL=index.js.map