Files
yaak-mountain-loop/src-web/hooks/usePrompt.ts
Gregory Schier 7e62bb6b68 Fix prompt
2024-10-02 12:19:43 -07:00

36 lines
1007 B
TypeScript

import type { DialogProps } from '../components/core/Dialog';
import { useDialog } from '../components/DialogContext';
import type { PromptProps } from './Prompt';
import { Prompt } from './Prompt';
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: resolve,
...props,
}),
});
});
}