@yamada-ui/use-disclosure
Advanced tools
Comparing version 1.0.23-next-20241022143327 to 1.1.0-dev-20241023003806
@@ -1,7 +0,7 @@ | ||
interface UseDisclosureProps<T extends (...args: any[]) => any = () => void, K extends (...args: any[]) => any = () => void> { | ||
interface UseDisclosureProps<Y extends (...args: any[]) => Promise<void> | void = () => void, M extends (...args: any[]) => Promise<void> | void = () => void> { | ||
defaultIsOpen?: boolean; | ||
isOpen?: boolean; | ||
timing?: "after" | "before"; | ||
onClose?: K; | ||
onOpen?: T; | ||
onClose?: M; | ||
onOpen?: Y; | ||
} | ||
@@ -14,9 +14,27 @@ /** | ||
*/ | ||
declare const useDisclosure: <T extends (...args: any[]) => any = () => void, K extends (...args: any[]) => any = () => void>(props?: UseDisclosureProps<T, K>) => { | ||
declare const useDisclosure: <Y extends (...args: any[]) => Promise<void> | void = () => void, M extends (...args: any[]) => Promise<void> | void = () => void>(props?: UseDisclosureProps<Y, M>) => { | ||
isOpen: boolean; | ||
onClose: K; | ||
onOpen: T; | ||
onToggle: T | K; | ||
onClose: M; | ||
onOpen: Y; | ||
onToggle: Y | M; | ||
}; | ||
type UseDisclosureReturn = ReturnType<typeof useDisclosure>; | ||
interface UsePromiseDisclosureProps<Y extends (...args: any[]) => Promise<void> | void = () => void, M extends (...args: any[]) => Promise<void> | void = () => void> extends UseDisclosureProps<Y, M> { | ||
disableCloseOnSuccess?: boolean; | ||
error?: unknown; | ||
} | ||
/** | ||
* `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 Docs https://yamada-ui.com/hooks/use-promise-disclosure | ||
*/ | ||
declare const usePromiseDisclosure: <Y extends (...args: any[]) => Promise<void> | void = () => void, M extends (...args: any[]) => Promise<void> | void = () => void>({ disableCloseOnSuccess, error, ...rest }?: UsePromiseDisclosureProps<Y, M>) => { | ||
isOpen: boolean; | ||
onClose: M; | ||
onOpen: (...args: Parameters<Y>) => Promise<void>; | ||
onSuccess: M; | ||
onToggle: Y | M; | ||
}; | ||
type UsePromiseDisclosureReturn = ReturnType<typeof usePromiseDisclosure>; | ||
type LazyMode = "keepMounted" | "unmount"; | ||
@@ -31,2 +49,2 @@ interface UseLazyDisclosureProps { | ||
export { type LazyMode, type UseDisclosureProps, type UseDisclosureReturn, type UseLazyDisclosureProps, useDisclosure, useLazyDisclosure }; | ||
export { type LazyMode, type UseDisclosureProps, type UseDisclosureReturn, type UseLazyDisclosureProps, type UsePromiseDisclosureProps, type UsePromiseDisclosureReturn, useDisclosure, useLazyDisclosure, usePromiseDisclosure }; |
@@ -25,3 +25,4 @@ "use client" | ||
useDisclosure: () => useDisclosure, | ||
useLazyDisclosure: () => useLazyDisclosure | ||
useLazyDisclosure: () => useLazyDisclosure, | ||
usePromiseDisclosure: () => usePromiseDisclosure | ||
}); | ||
@@ -58,3 +59,3 @@ module.exports = __toCommonJS(src_exports); | ||
const onToggle = (0, import_react.useCallback)( | ||
(...args) => !isOpen ? onOpen(...args) : onClose(...args), | ||
async (...args) => !isOpen ? onOpen(...args) : onClose(...args), | ||
[isOpen, onOpen, onClose] | ||
@@ -64,2 +65,46 @@ ); | ||
}; | ||
var usePromiseDisclosure = ({ | ||
disableCloseOnSuccess = false, | ||
error, | ||
...rest | ||
} = {}) => { | ||
const { | ||
isOpen, | ||
onClose: onInternalClose, | ||
onOpen: onInternalOpen, | ||
onToggle | ||
} = useDisclosure(rest); | ||
const rejectRef = (0, import_react.useRef)(void 0); | ||
const resolveRef = (0, import_react.useRef)(import_utils.noop); | ||
const onOpen = (0, import_react.useCallback)( | ||
async (...args) => { | ||
const promise = new Promise((resolve, reject) => { | ||
resolveRef.current = async (...args2) => { | ||
rejectRef.current = void 0; | ||
resolve(); | ||
if (!disableCloseOnSuccess) await onInternalClose(...args2); | ||
}; | ||
rejectRef.current = reject; | ||
}); | ||
await onInternalOpen(...args); | ||
await promise; | ||
}, | ||
[onInternalOpen, onInternalClose, disableCloseOnSuccess] | ||
); | ||
const onClose = (0, import_react.useCallback)( | ||
async (...args) => { | ||
var _a; | ||
(_a = rejectRef.current) == null ? void 0 : _a.call(rejectRef, error); | ||
await onInternalClose(...args); | ||
}, | ||
[onInternalClose, error] | ||
); | ||
return { | ||
isOpen, | ||
onClose, | ||
onOpen, | ||
onSuccess: resolveRef.current, | ||
onToggle | ||
}; | ||
}; | ||
var useLazyDisclosure = ({ | ||
@@ -79,4 +124,5 @@ enabled, | ||
useDisclosure, | ||
useLazyDisclosure | ||
useLazyDisclosure, | ||
usePromiseDisclosure | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@yamada-ui/use-disclosure", | ||
"version": "1.0.23-next-20241022143327", | ||
"version": "1.1.0-dev-20241023003806", | ||
"description": "Yamada UI useDisclosure custom hook", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@yamada-ui/utils": "1.5.4-next-20241022143327" | ||
"@yamada-ui/utils": "1.5.4-dev-20241023003806" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28398
265
+ Added@yamada-ui/utils@1.5.4-dev-20241023003806(transitive)
- Removed@yamada-ui/utils@1.5.4-next-20241022143327(transitive)