Skip to content
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

fix(dropdown): memoize floating ui ref to prevent max call depth #17002

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React, {
MouseEvent,
ReactNode,
useEffect,
useMemo,
} from 'react';
import {
useSelect,
Expand Down Expand Up @@ -497,8 +498,13 @@ const Dropdown = React.forwardRef(
},
};

const menuProps = getMenuProps();
const menuRef = mergeRefs(menuProps.ref, refs.setFloating);
const menuProps = useMemo(
() =>
getMenuProps({
ref: autoAlign ? refs.setFloating : null,
}),
[autoAlign]
);

// Slug is always size `mini`
let normalizedSlug;
Expand Down Expand Up @@ -527,7 +533,7 @@ const Dropdown = React.forwardRef(
warnText={warnText}
light={light}
isOpen={isOpen}
ref={refs.setReference}
ref={autoAlign ? refs.setReference : null}
id={id}>
{invalid && (
<WarningFilled className={`${prefix}--list-box__invalid-icon`} />
Expand Down Expand Up @@ -567,7 +573,7 @@ const Dropdown = React.forwardRef(
/>
</button>
{normalizedSlug}
<ListBox.Menu {...menuProps} ref={menuRef}>
<ListBox.Menu {...menuProps}>
{isOpen &&
items.map((item, index) => {
const isObject = item !== null && typeof item === 'object';
Expand Down
Loading