1
0
silverbullet/web/components/basic_modals.tsx
Zef Hemel a2dbf7b3db
PlugOS refactor and other tweaks (#631)
* Prep for in-process plug loading (e.g. for CF workers, Deno Deploy)
* Prototype of fixed in-process loading plugs
* Fix: buttons not to scroll with content
* Better positioning of modal especially on mobile
* Move query caching outside query
* Fix annoying mouse behavior when filter box appears
* Page navigator search tweaks
2024-01-15 16:43:12 +01:00

122 lines
2.7 KiB
TypeScript

import type { CompletionContext, CompletionResult } from "../deps.ts";
import { useRef, useState } from "../deps.ts";
import { MiniEditor } from "./mini_editor.tsx";
export function Prompt({
message,
defaultValue,
vimMode,
darkMode,
completer,
callback,
}: {
message: string;
defaultValue?: string;
vimMode: boolean;
darkMode: boolean;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
callback: (value?: string) => void;
}) {
const [text, setText] = useState(defaultValue || "");
const returnEl = (
<div className="sb-modal-box">
<div className="sb-prompt">
<label>{message}</label>
<MiniEditor
text={defaultValue || ""}
vimMode={vimMode}
vimStartInInsertMode={true}
focus={true}
darkMode={darkMode}
completer={completer}
onEnter={(text) => {
callback(text);
return true;
}}
onEscape={() => {
callback();
}}
onChange={(text) => {
setText(text);
}}
/>
<button
onClick={() => {
callback(text);
}}
>
Ok
</button>
<button
onClick={() => {
callback();
}}
>
Cancel
</button>
</div>
</div>
);
return returnEl;
}
export function Confirm({
message,
callback,
}: {
message: string;
callback: (value: boolean) => void;
}) {
const okButtonRef = useRef<HTMLButtonElement>(null);
setTimeout(() => {
okButtonRef.current?.focus();
});
const returnEl = (
<div className="sb-modal-wrapper">
<div className="sb-modal-box">
<div
className="sb-prompt"
onKeyDown={(e) => {
e.stopPropagation();
e.preventDefault();
switch (e.key) {
case "Enter":
callback(true);
break;
case "Escape":
callback(false);
break;
}
}}
>
<label>{message}</label>
<div>
<button
ref={okButtonRef}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
callback(true);
}}
>
Ok
</button>
<button
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
callback(false);
}}
>
Cancel
</button>
</div>
</div>
</div>
</div>
);
return returnEl;
}