Replace the browser's confirm() and prompt() pop-ups with in-app dialogs

All 31 native dialogs (27 confirms, 4 text prompts: ignore reason, two
"exclude a range" boxes, tag rename) now use the app's own Tabler-styled
modals, which follow the light/dark theme.

A small promise-based API (utils/dialogs.ts: confirmDialog, promptDialog)
and a single DialogHost mounted once in App mean call sites just await
it in place of the browser call - no hooks or per-page modal state. Every
call site was already in an async function, so each is a one-line swap.

Each dialog now has a title and a main button that names the action
("Delete", "Stop now", "Run now") instead of "OK", with destructive ones
in red. Escape cancels, Enter confirms, Tab stays inside the dialog, the
page behind stops scrolling, and focus returns to the button that was
clicked. Destructive dialogs start with focus on Cancel so a stray Enter
can't delete anything. Text prompts pre-select their default and disable
the main button until something is typed where it's required, and still
tell cancelling (null) apart from confirming an empty box (""). Clicking
the backdrop cancels, but releasing a text selection over it doesn't.
Dialogs asked for together appear one after another.

Verified in a browser on a test page using the real component and the
app's real stylesheet: Escape, Enter, Tab trapping, focus handling,
required and optional prompts, backdrop clicks, queuing, scroll lock and
dark mode. The 31 call sites themselves were checked by search and
typecheck rather than clicked through in the logged-in app.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-02 23:07:43 +02:00
1 parent 88d9c8e097
commit ad1fb5338f
24 files changed
+314 -39

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
import { useEffect, useId, useRef, useState, type FormEvent, type KeyboardEvent } from "react";
import { createPortal } from "react-dom";
import { settle, subscribe, type DialogRequest } from "../utils/dialogs";
const FOCUSABLE = "button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), [href]";
/** Draws the confirm/prompt dialogs asked for through utils/dialogs. Mount once, near the root. */
export default function DialogHost() {
const [current, setCurrent] = useState<DialogRequest | null>(null);
useEffect(() => subscribe(setCurrent), []);
if (!current) return null;
// Keyed, so back-to-back dialogs each start fresh (an empty text box, the right button focused).
return createPortal(<DialogView key={current.id} request={current} />, document.body);
}
function DialogView({ request }: { request: DialogRequest }) {
const titleId = useId();
const inputId = useId();
const modalRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const cancelRef = useRef<HTMLButtonElement>(null);
const confirmRef = useRef<HTMLButtonElement>(null);
const isPrompt = request.kind === "prompt";
const danger = request.kind === "confirm" && !!request.danger;
const [value, setValue] = useState(request.kind === "prompt" ? (request.defaultValue ?? "") : "");
const cancel = () => settle(request, false);
const blocked = request.kind === "prompt" && !!request.required && !value.trim();
// On open: stop the page behind from scrolling (without the scrollbar vanishing and shifting the layout), put the
// keyboard focus where it's useful, and on close hand it back to whatever had it — the button that was clicked.
useEffect(() => {
const previouslyFocused = document.activeElement as HTMLElement | null;
const { overflow, paddingRight } = document.body.style;
const scrollbar = window.innerWidth - document.documentElement.clientWidth;
document.body.style.overflow = "hidden";
if (scrollbar > 0) document.body.style.paddingRight = `${scrollbar}px`;
if (isPrompt) {
inputRef.current?.focus();
inputRef.current?.select();
} else if (danger) {
cancelRef.current?.focus(); // the safe answer, so a stray Enter doesn't delete anything
} else {
confirmRef.current?.focus();
}
return () => {
document.body.style.overflow = overflow;
document.body.style.paddingRight = paddingRight;
previouslyFocused?.focus?.();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
e.stopPropagation();
cancel();
return;
}
if (e.key !== "Tab") return;
// Keep Tab inside the dialog.
const items = Array.from(modalRef.current?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? []);
if (items.length === 0) return;
const first = items[0];
const last = items[items.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
}
function onSubmit(e: FormEvent) {
e.preventDefault();
if (blocked) return;
settle(request, true, value);
}
const title = request.title ?? (isPrompt ? "Enter a value" : "Please confirm");
return (
<>
<div
ref={modalRef}
className="modal modal-blur fade show"
style={{ display: "block" }}
role="dialog"
aria-modal="true"
aria-labelledby={titleId}
tabIndex={-1}
onKeyDown={onKeyDown}
// mousedown rather than click, so selecting text in the box and letting go outside it doesn't close the dialog
onMouseDown={(e) => {
if (e.target === e.currentTarget) cancel();
}}
>
<div className="modal-dialog modal-dialog-centered" role="document">
<form className="modal-content" onSubmit={onSubmit}>
{danger && <div className="modal-status bg-danger" />}
<div className="modal-header">
<h5 className="modal-title" id={titleId}>
{title}
</h5>
<button type="button" className="btn-close" aria-label="Close" onClick={cancel} />
</div>
<div className="modal-body">
<div style={{ whiteSpace: "pre-line" }}>{request.message}</div>
{request.kind === "prompt" && (
<div className="mt-3">
{request.label && (
<label className="form-label" htmlFor={inputId}>
{request.label}
</label>
)}
<input
id={inputId}
ref={inputRef}
className="form-control"
value={value}
placeholder={request.placeholder}
onChange={(e) => setValue(e.target.value)}
autoComplete="off"
spellCheck={false}
/>
</div>
)}
</div>
<div className="modal-footer">
<button type="button" className="btn me-auto" ref={cancelRef} onClick={cancel}>
{request.cancelLabel ?? "Cancel"}
</button>
<button type="submit" className={`btn ${danger ? "btn-danger" : "btn-primary"}`} ref={confirmRef} disabled={blocked}>
{request.confirmLabel ?? (isPrompt ? "OK" : "Confirm")}
</button>
</div>
</form>
</div>
</div>
<div className="modal-backdrop fade show" />
</>
);
}
+3 -1
View File
@@ -7,6 +7,7 @@ import { downloadCsv } from "../utils/csv";
import { readableError } from "../utils/errors";
import Pagination from "./Pagination";
import SortableTh from "./SortableTh";
import { confirmDialog } from "../utils/dialogs";
const PRESETS: { key: string; label: string; from: number; to: number }[] = [
{ key: "well-known", label: "1–1024 (well-known)", from: 1, to: 1024 },
@@ -188,7 +189,8 @@ export default function ServerPorts({
async function removeNote(entry: PortEntry) {
if (entry.id === null) return;
const what = entry.state === "reserved" ? `Remove the reservation for ${entry.protocol}/${entry.port}?` : `Clear the note on ${entry.protocol}/${entry.port}?`;
if (!confirm(what)) return;
const reserved = entry.state === "reserved";
if (!(await confirmDialog({ title: reserved ? "Remove reservation" : "Clear note", message: what, confirmLabel: reserved ? "Remove" : "Clear", danger: true }))) return;
try {
await api.servers.ports.remove(serverId, entry.id);
setData(await api.servers.ports.list(serverId));