feat(editor): default focus confirm button in danger dialog
This commit is contained in:
1 parent
ce57e8d9c0
commit
db150dfcab
2 files changed
+128
-8
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref, watch } from "vue";
|
import { computed, ref, watch, type ComponentPublicInstance } from "vue";
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
import { AlertTriangle, Check, Copy, Loader2, TextWrap } from "@lucide/vue";
|
import { AlertTriangle, Check, Copy, Loader2, TextWrap } from "@lucide/vue";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -42,6 +42,13 @@ const props = withDefaults(
|
|||||||
cancelRunningLoading?: boolean;
|
cancelRunningLoading?: boolean;
|
||||||
/** Holds the confirm button back until the caller's own precondition is met (e.g. the operator typed the target name). */
|
/** Holds the confirm button back until the caller's own precondition is met (e.g. the operator typed the target name). */
|
||||||
confirmDisabled?: boolean;
|
confirmDisabled?: boolean;
|
||||||
|
/**
|
||||||
|
* Controls which element should receive focus when the dialog opens.
|
||||||
|
* - "confirm": Focus the confirm button (default) if not disabled or loading.
|
||||||
|
* - "cancel": Focus the cancel button.
|
||||||
|
* - "default": Do not override default focus behavior (Reka UI will focus the first tabbable element).
|
||||||
|
*/
|
||||||
|
initialFocus?: "confirm" | "cancel" | "default";
|
||||||
}>(),
|
}>(),
|
||||||
{
|
{
|
||||||
sql: "",
|
sql: "",
|
||||||
@@ -57,6 +64,7 @@ const props = withDefaults(
|
|||||||
cancelable: false,
|
cancelable: false,
|
||||||
cancelRunningLoading: false,
|
cancelRunningLoading: false,
|
||||||
confirmDisabled: false,
|
confirmDisabled: false,
|
||||||
|
initialFocus: "confirm",
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -91,6 +99,27 @@ watch(
|
|||||||
{ immediate: true },
|
{ immediate: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const confirmButtonRef = ref<ComponentPublicInstance | HTMLButtonElement | null>(null);
|
||||||
|
const cancelButtonRef = ref<ComponentPublicInstance | HTMLButtonElement | null>(null);
|
||||||
|
|
||||||
|
function focusButton(buttonRef: ComponentPublicInstance | HTMLButtonElement | null) {
|
||||||
|
const el = buttonRef instanceof HTMLElement ? buttonRef : (buttonRef?.$el as HTMLElement | null);
|
||||||
|
el?.focus?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onDangerDialogOpenAutoFocus(event: Event) {
|
||||||
|
if (props.initialFocus === "default") return;
|
||||||
|
if (props.initialFocus === "cancel") {
|
||||||
|
event.preventDefault();
|
||||||
|
focusButton(cancelButtonRef.value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!props.confirmDisabled && !props.loading) {
|
||||||
|
event.preventDefault();
|
||||||
|
focusButton(confirmButtonRef.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restores focus through CodeMirror's own `EditorView.focus()` instead of the browser default.
|
* Restores focus through CodeMirror's own `EditorView.focus()` instead of the browser default.
|
||||||
*
|
*
|
||||||
@@ -136,7 +165,7 @@ async function copyFullCode() {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Dialog v-model:open="dialogOpen">
|
<Dialog v-model:open="dialogOpen">
|
||||||
<DialogContent class="sm:max-w-[480px]" @close-auto-focus="onDangerDialogCloseAutoFocus">
|
<DialogContent class="sm:max-w-[480px]" @open-auto-focus="onDangerDialogOpenAutoFocus" @close-auto-focus="onDangerDialogCloseAutoFocus">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle class="flex items-center gap-2 text-destructive">
|
<DialogTitle class="flex items-center gap-2 text-destructive">
|
||||||
<AlertTriangle class="h-5 w-5" />
|
<AlertTriangle class="h-5 w-5" />
|
||||||
@@ -173,12 +202,12 @@ async function copyFullCode() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button v-if="loading && cancelable" variant="outline" :disabled="cancelRunningLoading" @click="$emit('cancel-running')">
|
<Button v-if="loading && cancelable" ref="cancelButtonRef" variant="outline" :disabled="cancelRunningLoading" @click="$emit('cancel-running')">
|
||||||
<Loader2 v-if="cancelRunningLoading" class="h-3.5 w-3.5 animate-spin" />
|
<Loader2 v-if="cancelRunningLoading" class="h-3.5 w-3.5 animate-spin" />
|
||||||
{{ t("dangerDialog.cancelRunning") }}
|
{{ t("dangerDialog.cancelRunning") }}
|
||||||
</Button>
|
</Button>
|
||||||
<Button v-else variant="outline" :disabled="loading" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
<Button v-else ref="cancelButtonRef" variant="outline" :disabled="loading" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
||||||
<Button variant="destructive" class="gap-1.5" :disabled="loading || confirmDisabled" @click="onConfirm">
|
<Button ref="confirmButtonRef" variant="destructive" class="gap-1.5" :disabled="loading || confirmDisabled" @click="onConfirm">
|
||||||
<Loader2 v-if="loading" class="h-3.5 w-3.5 animate-spin" />
|
<Loader2 v-if="loading" class="h-3.5 w-3.5 animate-spin" />
|
||||||
{{ confirmLabel || t("dangerDialog.confirm") }}
|
{{ confirmLabel || t("dangerDialog.confirm") }}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
+94
-3
@@ -13,10 +13,16 @@ vi.mock("@/composables/useSqlHighlighter", () => ({
|
|||||||
|
|
||||||
const dialogHarness = vi.hoisted(() => {
|
const dialogHarness = vi.hoisted(() => {
|
||||||
const pendingCloseAutoFocus: Array<() => Event> = [];
|
const pendingCloseAutoFocus: Array<() => Event> = [];
|
||||||
|
const pendingOpenAutoFocus: Array<() => Event> = [];
|
||||||
return {
|
return {
|
||||||
pendingCloseAutoFocus,
|
pendingCloseAutoFocus,
|
||||||
|
pendingOpenAutoFocus,
|
||||||
flushCloseAutoFocus: () => pendingCloseAutoFocus.shift()?.(),
|
flushCloseAutoFocus: () => pendingCloseAutoFocus.shift()?.(),
|
||||||
reset: () => pendingCloseAutoFocus.splice(0),
|
flushOpenAutoFocus: () => pendingOpenAutoFocus.shift()?.(),
|
||||||
|
reset: () => {
|
||||||
|
pendingCloseAutoFocus.splice(0);
|
||||||
|
pendingOpenAutoFocus.splice(0);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -44,12 +50,28 @@ vi.mock("@/components/ui/dialog", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const DialogContent = defineComponent({
|
const DialogContent = defineComponent({
|
||||||
emits: ["closeAutoFocus"],
|
emits: ["openAutoFocus", "closeAutoFocus"],
|
||||||
setup(_, { emit, slots }) {
|
setup(_, { emit, slots }) {
|
||||||
const context = inject<DialogContext>(dialogContextKey);
|
const context = inject<DialogContext>(dialogContextKey);
|
||||||
if (!context) throw new Error("DialogContent must be nested in Dialog");
|
if (!context) throw new Error("DialogContent must be nested in Dialog");
|
||||||
|
|
||||||
|
const queueOpenAutoFocus = () => {
|
||||||
|
dialogHarness.pendingOpenAutoFocus.push(() => {
|
||||||
|
const event = new Event("open-auto-focus", { cancelable: true });
|
||||||
|
emit("openAutoFocus", event);
|
||||||
|
return event;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (context.open.value) {
|
||||||
|
queueOpenAutoFocus();
|
||||||
|
}
|
||||||
|
|
||||||
watch(context.open, (isOpen, wasOpen) => {
|
watch(context.open, (isOpen, wasOpen) => {
|
||||||
|
if (isOpen && !wasOpen) {
|
||||||
|
queueOpenAutoFocus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (isOpen || !wasOpen) return;
|
if (isOpen || !wasOpen) return;
|
||||||
dialogHarness.pendingCloseAutoFocus.push(() => {
|
dialogHarness.pendingCloseAutoFocus.push(() => {
|
||||||
const event = new Event("close-auto-focus", { cancelable: true });
|
const event = new Event("close-auto-focus", { cancelable: true });
|
||||||
@@ -85,7 +107,7 @@ afterEach(() => {
|
|||||||
document.body.innerHTML = "";
|
document.body.innerHTML = "";
|
||||||
});
|
});
|
||||||
|
|
||||||
function mountDialog(state: { open: boolean }) {
|
function mountDialog(state: { open: boolean }, extraProps: Record<string, unknown> = {}) {
|
||||||
const dialogHost = document.createElement("div");
|
const dialogHost = document.createElement("div");
|
||||||
document.body.append(dialogHost);
|
document.body.append(dialogHost);
|
||||||
const app = createApp(
|
const app = createApp(
|
||||||
@@ -95,6 +117,7 @@ function mountDialog(state: { open: boolean }) {
|
|||||||
open: state.open,
|
open: state.open,
|
||||||
sql: "UPDATE users SET active = 0;",
|
sql: "UPDATE users SET active = 0;",
|
||||||
confirmLabel: "Execute",
|
confirmLabel: "Execute",
|
||||||
|
...extraProps,
|
||||||
"onUpdate:open": (open: boolean) => {
|
"onUpdate:open": (open: boolean) => {
|
||||||
state.open = open;
|
state.open = open;
|
||||||
},
|
},
|
||||||
@@ -107,6 +130,12 @@ function mountDialog(state: { open: boolean }) {
|
|||||||
return dialogHost;
|
return dialogHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findCancelButton(host: HTMLElement) {
|
||||||
|
const button = [...host.querySelectorAll<HTMLButtonElement>("button")].find((candidate) => candidate.textContent?.includes("Cancel"));
|
||||||
|
if (!button) throw new Error("Cancel button not found");
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
function createEditor() {
|
function createEditor() {
|
||||||
const editorHost = document.createElement("div");
|
const editorHost = document.createElement("div");
|
||||||
document.body.append(editorHost);
|
document.body.append(editorHost);
|
||||||
@@ -194,3 +223,65 @@ describe("DangerConfirmDialog focus restoration", () => {
|
|||||||
expect(view.state.selection.main.head).toBe(20);
|
expect(view.state.selection.main.head).toBe(20);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("DangerConfirmDialog initial focus on open", () => {
|
||||||
|
it("defaults to focusing the confirm button on open", async () => {
|
||||||
|
const state = reactive({ open: true });
|
||||||
|
const dialogHost = mountDialog(state);
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const openEvent = dialogHarness.flushOpenAutoFocus();
|
||||||
|
expect(openEvent?.defaultPrevented).toBe(true);
|
||||||
|
|
||||||
|
const confirm = findConfirmButton(dialogHost);
|
||||||
|
expect(document.activeElement).toBe(confirm);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("focuses the cancel button on open when initialFocus is 'cancel'", async () => {
|
||||||
|
const state = reactive({ open: true });
|
||||||
|
const dialogHost = mountDialog(state, { initialFocus: "cancel" });
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const openEvent = dialogHarness.flushOpenAutoFocus();
|
||||||
|
expect(openEvent?.defaultPrevented).toBe(true);
|
||||||
|
|
||||||
|
const cancel = findCancelButton(dialogHost);
|
||||||
|
expect(document.activeElement).toBe(cancel);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not override default focus when initialFocus is 'default'", async () => {
|
||||||
|
const state = reactive({ open: true });
|
||||||
|
const dialogHost = mountDialog(state, { initialFocus: "default" });
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const openEvent = dialogHarness.flushOpenAutoFocus();
|
||||||
|
expect(openEvent?.defaultPrevented).toBe(false);
|
||||||
|
|
||||||
|
const confirm = findConfirmButton(dialogHost);
|
||||||
|
expect(document.activeElement).not.toBe(confirm);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not override default focus when confirmDisabled is true", async () => {
|
||||||
|
const state = reactive({ open: true });
|
||||||
|
const dialogHost = mountDialog(state, { confirmDisabled: true });
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const openEvent = dialogHarness.flushOpenAutoFocus();
|
||||||
|
expect(openEvent?.defaultPrevented).toBe(false);
|
||||||
|
|
||||||
|
const confirm = findConfirmButton(dialogHost);
|
||||||
|
expect(document.activeElement).not.toBe(confirm);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not override default focus when loading is true", async () => {
|
||||||
|
const state = reactive({ open: true });
|
||||||
|
const dialogHost = mountDialog(state, { loading: true });
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const openEvent = dialogHarness.flushOpenAutoFocus();
|
||||||
|
expect(openEvent?.defaultPrevented).toBe(false);
|
||||||
|
|
||||||
|
const confirm = findConfirmButton(dialogHost);
|
||||||
|
expect(document.activeElement).not.toBe(confirm);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user