import { useEffect, useRef, useState } from "preact/hooks"; import type { ComponentChildren } from "preact"; import { Button, Input } from "@silverbulletmd/silverbullet/ui"; export function Prompt({ message, defaultValue, callback, }: { message: string; defaultValue?: string; darkMode: boolean | undefined; callback: (value?: string) => void; }) { const [text, setText] = useState(defaultValue || ""); const inputRef = useRef(null); useEffect(() => { const input = inputRef.current; if (input) { input.focus(); const end = input.value.length; input.setSelectionRange(end, end); // caret at end of default value } }, []); const returnEl = ( { callback(); }} >
setText(e.currentTarget.value)} onConfirm={(value) => callback(value)} onExit={() => callback()} />
); return returnEl; } export function Confirm({ message, destructive, callback, }: { message: string; destructive?: boolean; callback: (value: boolean) => void; }) { const okButtonRef = useRef(null); setTimeout(() => { okButtonRef.current?.focus(); }); const returnEl = ( { callback(false); }} >
); return returnEl; } export function AlwaysShownModal({ children, onCancel, }: { children: ComponentChildren; onCancel?: () => void; }) { const dialogRef = useRef(null); useEffect(() => { dialogRef.current?.showModal(); }, []); return ( { e.preventDefault(); onCancel?.(); }} onKeyDown={(e) => { e.stopPropagation(); }} ref={dialogRef} > {children} ); }