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:
1 parent
88d9c8e097
commit
ad1fb5338f
24 files changed
+314
-39
No files matched your search
@@ -0,0 +1,69 @@
|
||||
// In-app replacements for window.confirm() and window.prompt(). Call sites just await them, the same way they used to
|
||||
// call the browser's — a single <DialogHost /> (mounted once, in App) draws whichever is asked for. Several asked for
|
||||
// at once are shown one after another.
|
||||
|
||||
export interface ConfirmOptions {
|
||||
title?: string;
|
||||
message: string;
|
||||
/** What the main button says — name the action ("Delete", "Stop") rather than leaving it as "OK". */
|
||||
confirmLabel?: string;
|
||||
cancelLabel?: string;
|
||||
/** Destructive or hard to undo: red button, and the safe choice gets the keyboard focus. */
|
||||
danger?: boolean;
|
||||
}
|
||||
|
||||
export interface PromptOptions {
|
||||
title?: string;
|
||||
message: string;
|
||||
/** Label for the text box itself, when the message above it isn't enough. */
|
||||
label?: string;
|
||||
defaultValue?: string;
|
||||
placeholder?: string;
|
||||
confirmLabel?: string;
|
||||
cancelLabel?: string;
|
||||
/** Disable the main button until something has been typed. */
|
||||
required?: boolean;
|
||||
}
|
||||
|
||||
export type DialogRequest =
|
||||
| ({ kind: "confirm"; id: number; resolve: (accepted: boolean) => void } & ConfirmOptions)
|
||||
| ({ kind: "prompt"; id: number; resolve: (value: string | null) => void } & PromptOptions);
|
||||
|
||||
let nextId = 1;
|
||||
let queue: DialogRequest[] = [];
|
||||
let listener: ((current: DialogRequest | null) => void) | null = null;
|
||||
|
||||
function emit() {
|
||||
listener?.(queue[0] ?? null);
|
||||
}
|
||||
|
||||
/** For the host: called with whatever should be on screen now (and straight away with what already is). */
|
||||
export function subscribe(next: (current: DialogRequest | null) => void): () => void {
|
||||
listener = next;
|
||||
emit();
|
||||
return () => {
|
||||
if (listener === next) listener = null;
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolves the dialog on screen. A cancelled confirm is false and a cancelled prompt is null — as with the browser's own. */
|
||||
export function settle(request: DialogRequest, accepted: boolean, value = ""): void {
|
||||
queue = queue.filter((r) => r !== request);
|
||||
if (request.kind === "confirm") request.resolve(accepted);
|
||||
else request.resolve(accepted ? value : null);
|
||||
emit();
|
||||
}
|
||||
|
||||
export function confirmDialog(options: ConfirmOptions): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
queue.push({ ...options, kind: "confirm", id: nextId++, resolve });
|
||||
emit();
|
||||
});
|
||||
}
|
||||
|
||||
export function promptDialog(options: PromptOptions): Promise<string | null> {
|
||||
return new Promise((resolve) => {
|
||||
queue.push({ ...options, kind: "prompt", id: nextId++, resolve });
|
||||
emit();
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user