Files
yaak/src-web/hooks/usePrompt.ts
2024-12-20 23:49:15 -08:00

39 lines
1.0 KiB
TypeScript

import type { DialogProps } from '../components/core/Dialog';
import type { PromptProps } from './Prompt';
import { Prompt } from './Prompt';
import {useDialog} from "./useDialog";
type Props = Pick<DialogProps, 'title' | 'description'> &
Omit<PromptProps, 'onClose' | 'onCancel' | 'onResult'> & { id: string };
export function usePrompt() {
const dialog = useDialog();
return ({ id, title, description, ...props }: Props) =>
new Promise((resolve: PromptProps['onResult']) => {
dialog.show({
id,
title,
description,
hideX: true,
size: 'sm',
onClose: () => {
// Click backdrop, close, or escape
resolve(null);
},
render: ({ hide }) =>
Prompt({
onCancel: () => {
// Click cancel button within dialog
resolve(null);
hide();
},
onResult: (v) => {
resolve(v);
hide();
},
...props,
}),
});
});
}