fix(ui): prevent closing selects from stealing focus

Preserve focus when another control becomes active during the close animation.
Add regression tests for filter persistence, Escape, and custom range dialog focus.
This commit is contained in:
BiFangKNT
2026-10-18 10:21:48 +08:00
parent f42d3919cb
commit b9acf7d9b0
2 changed files with 43 additions and 0 deletions
+14
View File
@@ -52,6 +52,7 @@ function SelectContent({
className,
children,
position = 'popper',
onCloseAutoFocus,
...props
}: React.ComponentProps<typeof SelectPrimitive.Content>) {
return (
@@ -66,6 +67,19 @@ function SelectContent({
)}
position={position}
{...props}
onCloseAutoFocus={(event) => {
onCloseAutoFocus?.(event);
if (event.defaultPrevented) return;
const content = event.target as HTMLElement;
const activeElement = content.ownerDocument.activeElement;
// Exit animations may finish after another control has taken focus.
if (
activeElement &&
activeElement !== content.ownerDocument.body &&
!content.contains(activeElement)
)
event.preventDefault();
}}
>
<SelectScrollUpButton />
<SelectPrimitive.Viewport