web: Escape in a dialog over the Format sheet closes only the dialog
Fable's follow-up E on #181. A modal dialog closes itself on Escape without preventDefault, so the document's Escape closed the sheet under it too. Escape from inside a dialog, or while one is open, is now left to the dialog. TestEscapeInADialogLeavesTheSheet opens a modal dialog over the sheet and presses Escape: the dialog closes, the sheet stays. It failed before (both closed).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+22
−3
internal/docs/brand/phone.js+8 −3internal/docs/phone_sheet_browser_test.go+14 −0
diff --git a/internal/docs/brand/phone.js b/internal/docs/brand/phone.js| index eb19424..852c675 100644 |
| --- a/internal/docs/brand/phone.js |
| +++ b/internal/docs/brand/phone.js |
| @@ -248,10 +248,15 @@ |
| sheet.addEventListener("keydown", function (e) { |
| if (e.key === "Escape" && sheetOpen() && !e.defaultPrevented) { e.preventDefault(); setSheet(false); } |
| }); |
| - // Escape anywhere closes it, unless something nearer (a picker's |
| - // list, a dialog) has used the key already. |
| + // Escape anywhere closes it, unless something nearer has used the |
| + // key: a picker's list says so with preventDefault; a modal dialog |
| + // closes itself on Escape without saying so, so Escape from inside |
| + // one, or while one is open, is the dialog's. |
| document.addEventListener("keydown", function (e) { |
| - if (e.key === "Escape" && sheetOpen() && !e.defaultPrevented) setSheet(false); |
| + if (e.key !== "Escape" || !sheetOpen() || e.defaultPrevented) return; |
| + var t = e.target; |
| + if ((t && t.closest && t.closest("dialog")) || document.querySelector("dialog[open]")) return; |
| + setSheet(false); |
| }); |
| // A swipe down on the grabber or the title closes it, when the |
| // finger lifts: a sheet that left under a finger still down would |
diff --git a/internal/docs/phone_sheet_browser_test.go b/internal/docs/phone_sheet_browser_test.go| index 8164c59..0c3bdc5 100644 |
| --- a/internal/docs/phone_sheet_browser_test.go |
| +++ b/internal/docs/phone_sheet_browser_test.go |
| @@ -500,3 +500,17 @@ func TestAResyncAfterACollaboratorsChange(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// Follow-up E on #181: Escape in a dialog opened over the sheet closes |
| +// the dialog, not the sheet too. |
| +func TestEscapeInADialogLeavesTheSheet(t *testing.T) { |
| + s := newSheetRig(t, "simple.bin", "") |
| + s.run("Aa, then a dialog", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle, |
| + do(`const d = document.createElement('dialog'); d.id = 'probe'; d.innerHTML = '<button>OK</button>'; document.body.append(d); d.showModal(); d.querySelector('button').focus()`), settle, |
| + chromedp.KeyEvent("\u001b"), settle) |
| + var got struct{ Dialog, Sheet bool } |
| + s.eval("after Escape", `({ Dialog: document.querySelector('#probe').open, Sheet: document.documentElement.getAttribute('data-docs-sheet') === 'open' })`, &got) |
| + if got.Dialog || !got.Sheet { |
| + t.Errorf("Escape in a dialog over the sheet: dialog open %v, sheet open %v; want the dialog closed and the sheet still open", got.Dialog, got.Sheet) |
| + } |
| +} |