@mantine/modals
Version:
Modals manager based on Mantine components
1 lines • 9.57 kB
Source Map (JSON)
{"version":3,"file":"ModalsProvider.cjs","names":["modalsReducer","ConfirmModal","ModalsContext","Modal"],"sources":["../src/ModalsProvider.tsx"],"sourcesContent":["import { useCallback, useReducer, useRef } from 'react';\nimport { getDefaultZIndex, Modal } from '@mantine/core';\nimport { randomId } from '@mantine/hooks';\nimport { ConfirmModal } from './ConfirmModal';\nimport {\n ConfirmLabels,\n ContextModalProps,\n ModalsContext,\n ModalsContextProps,\n ModalSettings,\n OpenConfirmModal,\n OpenContextModal,\n} from './context';\nimport { useModalsEvents } from './events';\nimport { handleCloseModal, modalsReducer } from './reducer';\n\nexport interface ModalsProviderProps {\n /** Your app */\n children?: React.ReactNode;\n\n /** Predefined modals */\n modals?: Record<string, React.FC<ContextModalProps<any>>>;\n\n /** Shared Modal component props, applied for every modal */\n modalProps?: ModalSettings;\n\n /** Confirm modal labels */\n labels?: ConfirmLabels;\n}\n\nfunction separateConfirmModalProps(props: OpenConfirmModal) {\n if (!props) {\n return { confirmProps: {}, modalProps: {} };\n }\n\n const {\n id,\n children,\n onCancel,\n onConfirm,\n closeOnConfirm,\n closeOnCancel,\n cancelProps,\n confirmProps,\n groupProps,\n labels,\n ...others\n } = props;\n\n return {\n confirmProps: {\n id,\n children,\n onCancel,\n onConfirm,\n closeOnConfirm,\n closeOnCancel,\n cancelProps,\n confirmProps,\n groupProps,\n labels,\n },\n modalProps: {\n id,\n ...others,\n },\n };\n}\n\nexport function ModalsProvider({ children, modalProps, labels, modals }: ModalsProviderProps) {\n const [state, dispatch] = useReducer(modalsReducer, { modals: [], current: null });\n const stateRef = useRef(state);\n stateRef.current = state;\n const closingRef = useRef(false);\n\n const closeAll = useCallback(\n (canceled?: boolean) => {\n if (!closingRef.current) {\n closingRef.current = true;\n stateRef.current.modals\n .concat()\n .reverse()\n .forEach((modal) => {\n handleCloseModal(modal, canceled);\n });\n closingRef.current = false;\n }\n dispatch({ type: 'CLOSE_ALL', canceled });\n },\n [stateRef, dispatch]\n );\n\n const openModal = useCallback(\n ({ modalId, ...props }: ModalSettings) => {\n const id = modalId || randomId();\n\n dispatch({\n type: 'OPEN',\n modal: {\n id,\n type: 'content',\n props,\n },\n });\n return id;\n },\n [dispatch]\n );\n\n const openConfirmModal = useCallback(\n ({ modalId, ...props }: OpenConfirmModal) => {\n const id = modalId || randomId();\n dispatch({\n type: 'OPEN',\n modal: {\n id,\n type: 'confirm',\n props,\n },\n });\n return id;\n },\n [dispatch]\n );\n\n const openContextModal = useCallback(\n (modal: string, { modalId, ...props }: OpenContextModal) => {\n const id = modalId || randomId();\n dispatch({\n type: 'OPEN',\n modal: {\n id,\n type: 'context',\n props,\n ctx: modal,\n },\n });\n return id;\n },\n [dispatch]\n );\n\n const closeModal = useCallback(\n (id: string, canceled?: boolean) => {\n if (!closingRef.current) {\n const modal = stateRef.current.modals.find((m) => m.id === id);\n if (modal) {\n closingRef.current = true;\n handleCloseModal(modal, canceled);\n closingRef.current = false;\n }\n }\n dispatch({ type: 'CLOSE', modalId: id, canceled });\n },\n [stateRef, dispatch]\n );\n\n const updateModal = useCallback(\n ({ modalId, ...newProps }: Partial<ModalSettings> & { modalId: string }) => {\n dispatch({\n type: 'UPDATE',\n modalId,\n newProps,\n });\n },\n [dispatch]\n );\n\n const updateContextModal = useCallback(\n ({ modalId, ...newProps }: { modalId: string } & Partial<OpenContextModal<any>>) => {\n dispatch({ type: 'UPDATE', modalId, newProps });\n },\n [dispatch]\n );\n\n useModalsEvents({\n openModal,\n openConfirmModal,\n openContextModal: ({ modal, ...payload }: any) => openContextModal(modal, payload),\n closeModal,\n closeContextModal: closeModal,\n closeAllModals: closeAll,\n updateModal,\n updateContextModal,\n });\n\n const ctx: ModalsContextProps = {\n modalProps: modalProps || {},\n modals: state.modals,\n openModal,\n openConfirmModal,\n openContextModal,\n closeModal,\n closeContextModal: closeModal,\n closeAll,\n updateModal,\n updateContextModal,\n };\n\n const getCurrentModal = () => {\n const currentModal = stateRef.current.current;\n switch (currentModal?.type) {\n case 'context': {\n const { innerProps, ...rest } = currentModal.props;\n const ContextModal = modals![currentModal.ctx];\n\n return {\n modalProps: rest,\n content: <ContextModal innerProps={innerProps} context={ctx} id={currentModal.id} />,\n };\n }\n case 'confirm': {\n const { modalProps: separatedModalProps, confirmProps: separatedConfirmProps } =\n separateConfirmModalProps(currentModal.props);\n\n return {\n modalProps: separatedModalProps,\n content: (\n <ConfirmModal\n {...separatedConfirmProps}\n id={currentModal.id}\n labels={currentModal.props.labels || labels}\n />\n ),\n };\n }\n case 'content': {\n const { children: currentModalChildren, ...rest } = currentModal.props;\n\n return {\n modalProps: rest,\n content: currentModalChildren,\n };\n }\n default: {\n return {\n modalProps: {},\n content: null,\n };\n }\n }\n };\n\n const { modalProps: currentModalProps, content } = getCurrentModal();\n\n return (\n <ModalsContext value={ctx}>\n <Modal\n zIndex={getDefaultZIndex('modal') + 1}\n {...modalProps}\n {...currentModalProps}\n opened={state.modals.length > 0}\n onClose={() => closeModal(state.current?.id as any)}\n >\n {content}\n </Modal>\n\n {children}\n </ModalsContext>\n );\n}\n"],"mappings":";;;;;;;;;;AA8BA,SAAS,0BAA0B,OAAyB;CAC1D,IAAI,CAAC,OACH,OAAO;EAAE,cAAc,CAAC;EAAG,YAAY,CAAC;CAAE;CAG5C,MAAM,EACJ,IACA,UACA,UACA,WACA,gBACA,eACA,aACA,cACA,YACA,QACA,GAAG,WACD;CAEJ,OAAO;EACL,cAAc;GACZ;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACF;EACA,YAAY;GACV;GACA,GAAG;EACL;CACF;AACF;AAEA,SAAgB,eAAe,EAAE,UAAU,YAAY,QAAQ,UAA+B;CAC5F,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,YAAuBA,gBAAAA,eAAe;EAAE,QAAQ,CAAC;EAAG,SAAS;CAAK,CAAC;CACjF,MAAM,YAAA,GAAA,MAAA,QAAkB,KAAK;CAC7B,SAAS,U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