-
Notifications
You must be signed in to change notification settings - Fork 30
fix(components): Fix autocomplete crashing on resize larger [JOB-151844] #2924
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
Changes from all commits
241f2e8
2aac935
abb0930
e34943b
9dd03a2
008dbdc
052e989
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 |
|---|---|---|
|
|
@@ -50,6 +50,18 @@ export interface UseAutocompleteListNavProps { | |
| outsidePressExcludeSelector?: string; | ||
| } | ||
|
|
||
| function useStableSetReference( | ||
| refs: UseFloatingReturn["refs"], | ||
| ): (el: HTMLElement | null) => void { | ||
| const latestRefs = useRef(refs); | ||
| latestRefs.current = refs; | ||
|
|
||
| return useCallback( | ||
| (el: HTMLElement | null) => latestRefs.current.setReference(el), | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
| export function useAutocompleteListNav({ | ||
| navigableCount, | ||
| shouldResetActiveIndexOnClose, | ||
|
|
@@ -84,12 +96,13 @@ export function useAutocompleteListNav({ | |
| offset(MENU_OFFSET), | ||
| flip({ fallbackPlacements: ["top"] }), | ||
| size({ | ||
| apply({ availableHeight, elements }) { | ||
| apply({ availableHeight, rects, elements }) { | ||
| const maxHeight = calculateMaxHeight(availableHeight, { | ||
| maxHeight: AUTOCOMPLETE_MAX_HEIGHT, | ||
| }); | ||
| Object.assign(elements.floating.style, { | ||
| maxHeight: `${maxHeight}px`, | ||
| width: `${rects.reference.width}px`, | ||
|
Contributor
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. clever! I forgot
Contributor
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 did notice we used to have a
Contributor
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.
|
||
| }); | ||
| }, | ||
| }), | ||
|
|
@@ -154,10 +167,7 @@ export function useAutocompleteListNav({ | |
| }); | ||
| }, [navigableCount, setActiveIndex, listRef]); | ||
|
|
||
| const setReferenceElement = useCallback( | ||
| (el: HTMLElement | null) => refs.setReference(el), | ||
| [refs], | ||
| ); | ||
| const setReferenceElement = useStableSetReference(refs); | ||
|
|
||
| return { | ||
| refs, | ||
|
|
||
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.
my first thought is if we could use
useCallbackRefinstead.something like
const setReferenceElement = useCallbackRef(() => refs.setReference);I'll have to double check this is equivalent.
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.
useCallbackRef(() => refs.setReference)isn’t equivalent because it returns the function instead of invoking it with the element. I’d needuseCallbackRef((el) => refs.setReference(el)), but for a ref callbackuseStableSetReferenceis safer since it keepsrefscurrent during render rather than after commit (useEffect).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.
fair enough, I'm happy to leave it as is then.