Repository navigation
fix(ComboBox): keep the popover open when pressing a non-field part of it #11003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,12 @@ import { | |
| useRef, | ||
| useState | ||
| } from 'react'; | ||
| import {getActiveElement, getEventTarget, nodeContains} from '../utils/shadowdom/DOMFunctions'; | ||
| import { | ||
| getActiveElement, | ||
| getEventTarget, | ||
| getParentElement, | ||
| nodeContains | ||
| } from '../utils/shadowdom/DOMFunctions'; | ||
| import {getChildNodes} from 'react-stately/private/collections/getChildNodes'; | ||
| import {getItemCount} from 'react-stately/private/collections/getItemCount'; | ||
| import {getItemId, listData} from '../listbox/utils'; | ||
|
|
@@ -61,6 +66,7 @@ import {useRouter} from '../utils/openLink'; | |
| import {useSelectableCollection} from '../selection/useSelectableCollection'; | ||
| import {useTextField} from '../textfield/useTextField'; | ||
| import {useUpdateEffect} from '../utils/useUpdateEffect'; | ||
| import {willOpenKeyboard} from '../utils/keyboard'; | ||
|
|
||
| export interface AriaComboBoxProps<T, M extends SelectionMode = 'single'> | ||
| extends ComboBoxProps<T, M>, DOMProps, InputDOMProps, AriaLabelingProps { | ||
|
|
@@ -504,6 +510,26 @@ export function useComboBox<T, M extends SelectionMode = 'single'>( | |
| : undefined | ||
| ); | ||
|
|
||
| // Pressing a part of the popover that can't take focus (padding, text, or a native button in | ||
| // Safari) moves focus to the body, and the input's blur would commit and close the popover. | ||
| // Keep focus in the input, except for fields that need it. This is a native listener because | ||
| // usePress stops mousedown propagation. Skipped when the input is inside the popover (mobile trays). | ||
| useEvent( | ||
| popoverRef, | ||
| 'mousedown', | ||
| state.isOpen | ||
| ? e => { | ||
| let popover = popoverRef.current; | ||
| if ( | ||
| !nodeContains(popover, inputRef.current) && | ||
| !isFieldTarget(getEventTarget(e) as Element, popover) | ||
| ) { | ||
| e.preventDefault(); | ||
| } | ||
| } | ||
| : undefined | ||
| ); | ||
|
|
||
| return { | ||
| labelProps: { | ||
| ...labelProps, | ||
|
|
@@ -559,6 +585,20 @@ export function useComboBox<T, M extends SelectionMode = 'single'>( | |
| }; | ||
| } | ||
|
|
||
| function isFieldTarget(target: Element | null, popover: Element | null): boolean { | ||
| for (let element = target; element && element !== popover; element = getParentElement(element)) { | ||
| if ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't understand this if, why does it look for willOpenKeyboard? I'm imagining this is covering the same case as https://react-aria.adobe.com/ComboBox#item-actions but inside the popover there'd probably be some accessibility problems
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two cases:
|
||
| willOpenKeyboard(element) || | ||
| element instanceof HTMLSelectElement || | ||
| (element instanceof HTMLInputElement && element.type === 'range') | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // This is a modified version of useSlotId that uses useEffect instead of useLayoutEffect. | ||
| // Triggering re-renders from useLayoutEffect breaks useComboBoxState's useEffect logic in React 18. | ||
| // These re-renders preempt async state updates in the useEffect, which ends up running multiple times | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this wouldn't work for touch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which touch case do you mean? From what I understand, a tap still fires
mousedownaftertouchend.I made a codepen to test in real device (Google pixel 7a, chrome) (https://codepen.io/zecka/full/vEmLwzw) and with Playwright touch emulation, but I may be missing something.