Close Combobox and Autocomplete when focus leaves without another focus target (#11217)

Co-authored-by: AliUyarGW <275436540+AliUyarGW@users.noreply.github.com>
This commit is contained in:
effect-bot
2026-12-02 10:08:52 +13:00
committed by GitHub
co-authored by AliUyarGW
parent 88f3dc22ca
commit e4a49598a4
4 changed files with 63 additions and 10 deletions
+5
View File
@@ -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()`.
@@ -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,
@@ -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 <body> 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<typeof setTimeout> | 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<typeof setTimeout> | 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()
@@ -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))