UNPKG

@audira/carbon-react-native

Version:

Build React Native apps with component and shared patterns using Carbon

70 lines (69 loc) 2.05 kB
"use strict"; import { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'; import { DialogContext } from "../../contexts/dialog/index.js"; import { Controller } from "./_controller/index.js"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; export const DialogProvider = /*#__PURE__*/forwardRef(function DialogProvider({ children, animationConfig, modalProps, overlayProps, overlayTouchDismiss }, ref) { const controllerRef = useRef(null), dialogDataQueue = useRef(null), [mountController, setMountController] = useState(false), setControllerRef = useCallback(ref => { controllerRef.current = ref; if (controllerRef.current && dialogDataQueue.current) { controllerRef.current.show({ ...dialogDataQueue.current }); dialogDataQueue.current = null; } }, []), show = useCallback(data => { if (!controllerRef.current) { dialogDataQueue.current = { ...data }; setMountController(true); } else { controllerRef.current?.show(data); } }, []), dismiss = useCallback(async () => { return controllerRef.current?.dismiss(); }, []), dismissAll = useCallback(async () => { return controllerRef.current?.dismissAll(); }, []), onEmpty = useCallback(() => { if (!dialogDataQueue.current) { setMountController(false); } }, []); useImperativeHandle(ref, () => { return { show, dismiss, dismissAll }; }, [show, dismiss, dismissAll]); return /*#__PURE__*/_jsxs(DialogContext.Provider, { value: { show, dismiss, dismissAll }, children: [children, mountController && /*#__PURE__*/_jsx(Controller, { ref: setControllerRef, animationConfig: animationConfig, modalProps: modalProps, overlayProps: overlayProps, overlayTouchDismiss: overlayTouchDismiss, onEmpty: onEmpty })] }); }); //# sourceMappingURL=DialogProvider.js.map