Skip to content

Commit 9706194

Browse files
committed
fix(ui): stop modal autofocus steal after the open animation
1 parent f4da74a commit 9706194

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎ui/src/components/modal-dialog.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ describe("openclaw-modal-dialog", () => {
7777
expect(document.activeElement).toBe(container.querySelector("#autofocus-target"));
7878
});
7979

80+
it("keeps focus on a field the user selected when the show animation settles", async () => {
81+
render(
82+
html`<openclaw-modal-dialog label="Edit">
83+
<input id="autofocus-target" autofocus />
84+
<textarea id="notes-field"></textarea>
85+
</openclaw-modal-dialog>`,
86+
container,
87+
);
88+
const { webAwesomeDialog } = await getRenderedModalDialog(container);
89+
const notes = container.querySelector<HTMLTextAreaElement>("#notes-field");
90+
notes?.focus();
91+
92+
webAwesomeDialog.dispatchEvent(new Event("wa-after-show"));
93+
94+
expect(document.activeElement).toBe(notes);
95+
});
96+
8097
it("delegates native modality and light dismissal to Web Awesome", async () => {
8198
const { webAwesomeDialog, dialog } = await renderModal();
8299

‎ui/src/components/modal-dialog.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ export class OpenClawModalDialog extends OpenClawLitElement {
173173
if (!this.isConnected) {
174174
return;
175175
}
176+
// Both the scheduled show hook and wa-after-show land here, and the second
177+
// arrives after the open animation. If focus already moved to a slotted
178+
// field (user click, autofill, e2e input), refocusing the autofocus target
179+
// would steal it mid-typing; `this` means focus sits on dialog chrome.
180+
const active = document.activeElement;
181+
if (active instanceof HTMLElement && active !== this && this.contains(active)) {
182+
return;
183+
}
176184
const autofocusTarget = this.querySelector<HTMLElement>("[autofocus]");
177185
autofocusTarget?.focus({ preventScroll: true });
178186
};

0 commit comments

Comments
 (0)