1 parent
7a18682b0e
commit
24fecc90f3
107 files changed
+14935
-1919
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { Modal } from './Modal';
|
||||
import { useStore } from '../lib/store';
|
||||
import { dialogStore, resolveConfirm, resolvePrompt } from '../lib/dialog';
|
||||
import { t } from '../i18n';
|
||||
|
||||
// Renders the active custom dialog (confirm/prompt). Mounted once at the app
|
||||
// root; see lib/dialog.ts for the confirmDialog/promptDialog helpers.
|
||||
export function DialogHost() {
|
||||
const { current } = useStore(dialogStore);
|
||||
const [text, setText] = useState('');
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// Seed the prompt field with its initial value and focus it each time a
|
||||
// new prompt opens.
|
||||
useEffect(() => {
|
||||
if (current && current.kind === 'prompt') {
|
||||
setText(current.initial || '');
|
||||
// Focus after the modal mounts.
|
||||
setTimeout(() => inputRef.current?.focus(), 0);
|
||||
}
|
||||
}, [current]);
|
||||
|
||||
if (!current) return null;
|
||||
|
||||
if (current.kind === 'confirm') {
|
||||
return (
|
||||
<Modal
|
||||
title={current.title}
|
||||
onClose={() => resolveConfirm(false)}
|
||||
footer={
|
||||
<>
|
||||
<button
|
||||
className="btn btn-ghost"
|
||||
onClick={() => resolveConfirm(false)}
|
||||
>
|
||||
{current.cancelLabel || t('Cancel')}
|
||||
</button>
|
||||
<button
|
||||
className={
|
||||
'btn ' +
|
||||
(current.danger ? 'btn-danger' : 'btn-accent')
|
||||
}
|
||||
onClick={() => resolveConfirm(true)}
|
||||
>
|
||||
{current.confirmLabel || t('Confirm')}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<p>{current.message}</p>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={current.title}
|
||||
onClose={() => resolvePrompt(null)}
|
||||
footer={
|
||||
<>
|
||||
<button
|
||||
className="btn btn-ghost"
|
||||
onClick={() => resolvePrompt(null)}
|
||||
>
|
||||
{current.cancelLabel || t('Cancel')}
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-accent"
|
||||
onClick={() => resolvePrompt(text.trim() || null)}
|
||||
>
|
||||
{current.confirmLabel || t('OK')}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
resolvePrompt(text.trim() || null);
|
||||
}}
|
||||
>
|
||||
{current.message && <p>{current.message}</p>}
|
||||
<input
|
||||
ref={inputRef}
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
placeholder={current.placeholder}
|
||||
autoComplete="off"
|
||||
/>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user