UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

49 lines (45 loc) 1.52 kB
"use client"; import { utils_exports } from "../../utils/index.js"; import { useDisclosure } from "./use-disclosure.js"; import { useCallback, useRef } from "react"; //#region src/hooks/use-disclosure/use-promise-disclosure.ts /** * `usePromiseDisclosure` is a custom hook that helps handle common open/close or toggle scenarios with promises. * It can be used to control components such as `Modal`, `Dialog`, `Drawer`, etc. * * @see https://yamada-ui.com/docs/hooks/use-promise-disclosure */ const usePromiseDisclosure = ({ disableCloseOnSuccess = false, error,...rest } = {}) => { const { open, onClose: onInternalClose, onOpen: onInternalOpen, onToggle } = useDisclosure(rest); const rejectRef = useRef(void 0); const resolveRef = useRef(utils_exports.noop); const onOpen = useCallback(async (value) => { const promise = new Promise((resolve, reject) => { resolveRef.current = (async (value$1) => { rejectRef.current = void 0; resolve(value$1); if (!disableCloseOnSuccess) await onInternalClose(value$1); }); rejectRef.current = reject; }); await onInternalOpen(value); return await promise; }, [ onInternalOpen, onInternalClose, disableCloseOnSuccess ]); return { open, onClose: useCallback(async (value) => { rejectRef.current?.(error); await onInternalClose(value); }, [onInternalClose, error]), onOpen, onSuccess: resolveRef.current, onToggle }; }; //#endregion export { usePromiseDisclosure }; //# sourceMappingURL=use-promise-disclosure.js.map