diff --git a/.changeset/ui-kit-combobox-blur-close.md b/.changeset/ui-kit-combobox-blur-close.md new file mode 100644 index 0000000000..bb7940eeaf --- /dev/null +++ b/.changeset/ui-kit-combobox-blur-close.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +`Combobox` and `Autocomplete` in `effect/ui/kit` now emit `RequestedClose` with reason `"focus-out"` when focus leaves the widget even if no other element receives focus, such as after calling `blur()`. diff --git a/packages/effect/src/ui/kit/internal/combobox.ts b/packages/effect/src/ui/kit/internal/combobox.ts index b99d8a1192..9d1630b14a 100644 --- a/packages/effect/src/ui/kit/internal/combobox.ts +++ b/packages/effect/src/ui/kit/internal/combobox.ts @@ -991,7 +991,7 @@ const watch = ( node.contains(focused) if ( current.inputInPopup && current.finalFocus && - (lost || !current.focusLeft) + (!current.focusLeft || (lost && document.hasFocus())) ) { focusFinal( document, @@ -1072,11 +1072,13 @@ const watch = ( return Effect.acquireRelease( Effect.sync(() => { + const focusedOutside = () => send(Message.FocusedOutside()) listenOutside(node, signal, { open: () => current.open, inside, pressed: (pointerType) => send(Message.PressedOutside({ pointerType })), - focused: () => send(Message.FocusedOutside()) + focused: focusedOutside, + left: focusedOutside }) document.addEventListener("mouseup", onMouseUp, { capture: true, diff --git a/packages/effect/src/ui/kit/internal/outside.ts b/packages/effect/src/ui/kit/internal/outside.ts index 00aa6ead2f..ce358e3877 100644 --- a/packages/effect/src/ui/kit/internal/outside.ts +++ b/packages/effect/src/ui/kit/internal/outside.ts @@ -8,8 +8,8 @@ export interface Outside { readonly inside: (target: Node) => boolean readonly pressed: (pointerType: string) => void readonly focused?: (() => void) | undefined - // Focus moved from inside to anywhere else, including and out of - // the document, which fire no focusin. + // Focus left the inside region, even without a focusin event. + // Skipped when `focused` already reported the move. readonly left?: (() => void) | undefined } @@ -39,10 +39,12 @@ export const listenOutside = ( outside.pressed("touch") } } + let settle: ReturnType | undefined const focused = outside.focused const onFocusIn = (event: FocusEvent) => { const target = event.target as Node | null if (outside.open() && target !== null && !outside.inside(target)) { + clearTimeout(settle) focused?.() } } @@ -52,10 +54,9 @@ export const listenOutside = ( if (focused !== undefined) listenTrees(node, "focusin", onFocusIn, captured) const left = outside.left if (left === undefined) return - let settle: ReturnType | undefined const isInside = (target: Node | null) => target !== null && outside.inside(target) const onFocusOut = (event: FocusEvent) => { - if (!isInside(event.target as Node | null)) return + if (!outside.open() || !isInside(event.target as Node | null)) return clearTimeout(settle) settle = setTimeout(() => { if (outside.open() && !isInside(focusedOf(node))) left() diff --git a/packages/effect/test/ui/kit/ComboboxDocument.test.ts b/packages/effect/test/ui/kit/ComboboxDocument.test.ts index 1e56de0937..914a0ba065 100644 --- a/packages/effect/test/ui/kit/ComboboxDocument.test.ts +++ b/packages/effect/test/ui/kit/ComboboxDocument.test.ts @@ -82,7 +82,8 @@ const Config = Schema.Struct({ rtlPopup: Schema.Boolean, grid: Schema.Boolean, inline: Schema.Boolean, - modal: Schema.Boolean + modal: Schema.Boolean, + keepOpen: Schema.Boolean }) type Config = typeof Config.Type @@ -114,7 +115,7 @@ const Page = Html.component({ }), RequestedClose: ($, { reason }) => ({ ...$.model, - open: false, + open: $.props.keepOpen ? $.model.open : false, log: [...$.model.log, "RequestedClose " + reason] }), Picked: ($, { value }) => ({ @@ -281,6 +282,7 @@ const mountPage = Effect.fnUntraced(function*( grid: false, inline: false, modal: false, + keepOpen: false, ...config } const root = yield* BrowserHost.mountEffect(Page, props, app) @@ -437,7 +439,7 @@ describe("Combobox in a document", { concurrent: false }, () => { it.effect("a non-modal popup leaves the page exposed, a press outside closes it and puts the selected label back, and focus leaving closes it", () => Effect.gen(function*() { - const { log, root } = yield* mountPage() + const { log, props, root } = yield* mountPage() root.send(Message.Picked({ value: ["banana"] })) root.send( Message.RequestedInputValue({ inputValue: "Banana", reason: "test" }) @@ -457,12 +459,33 @@ describe("Combobox in a document", { concurrent: false }, () => { byId("after").focus() yield* tick assert.isFalse(shown()) + input().focus() + mousedown(input()) + yield* tick + // Simulate window deactivation with the input still active. + input().dispatchEvent(new FocusEvent("focusout", { bubbles: true })) + yield* tick + assert.isTrue(shown()) + input().blur() + yield* tick + assert.isFalse(shown()) + root.setProps({ ...props, keepOpen: true }) + input().focus() + mousedown(input()) + yield* tick + byId("after").focus() + yield* tick + assert.isTrue(shown()) assert.deepStrictEqual( log().filter((line) => !line.startsWith("RequestedInputValue") && !line.startsWith("Picked")), [ "RequestedOpen input-change", "RequestedClose outside-press", "RequestedOpen input-press", + "RequestedClose focus-out", + "RequestedOpen input-press", + "RequestedClose focus-out", + "RequestedOpen input-press", "RequestedClose focus-out" ] ) @@ -577,12 +600,34 @@ describe("Combobox in a document", { concurrent: false }, () => { assert.strictEqual(document.activeElement, byId("trigger")) assert.strictEqual(byId("trigger").textContent, "Banana") assert.strictEqual(input().value, "") + keydown(byId("trigger"), "ArrowDown") + yield* tick + input().blur() + yield* tick + assert.isFalse(shown()) + assert.strictEqual(document.activeElement, byId("trigger")) + keydown(byId("trigger"), "ArrowDown") + yield* tick + const hadFocus = document.hasFocus + document.hasFocus = () => false + try { + input().blur() + yield* tick + } finally { + document.hasFocus = hadFocus + } + assert.isFalse(shown()) + assert.strictEqual(document.activeElement, document.body) assert.deepStrictEqual(log(), [ "RequestedOpen list-navigation", "RequestedInputValue \"ban\" input-change", "Picked banana", "RequestedClose item-press", - "RequestedInputValue \"\" input-clear" + "RequestedInputValue \"\" input-clear", + "RequestedOpen list-navigation", + "RequestedClose focus-out", + "RequestedOpen list-navigation", + "RequestedClose focus-out" ]) }).pipe(Effect.scoped))