Skip to content

fix: adds backward compatibility for innerRefs in OverflowMenu #19086

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

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions packages/react/src/components/OverflowMenu/OverflowMenu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,29 @@ describe('OverflowMenu', () => {
expect(button).not.toHaveClass('cds--overflow-menu--open');
expect(button).toHaveFocus();
});
describe('Ref handling', () => {
it('should support both standard ref and innerRef', () => {
const standardRef = React.createRef();
const innerRef = React.createRef();

render(
<OverflowMenu
ref={standardRef}
innerRef={innerRef}
aria-label="Overflow menu"
data-testid="overflow-menu">
<OverflowMenuItem itemText="Option 1" />
<OverflowMenuItem itemText="Option 2" />
</OverflowMenu>
);
const buttonElement = screen.getByRole('button');
expect(standardRef.current).not.toBeNull();
expect(innerRef.current).not.toBeNull();
expect(standardRef.current).toBe(buttonElement);
expect(innerRef.current).toBe(buttonElement);

// Verify both refs point to the same element & not null
expect(standardRef.current).toBe(innerRef.current);
});
});
});
9 changes: 7 additions & 2 deletions packages/react/src/components/OverflowMenu/OverflowMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ export interface OverflowMenuProps {
size?: 'sm' | 'md' | 'lg';

/**
* The ref to the HTML element that should receive focus when the OverflowMenu opens
* The ref to the overflow menu's trigger button element.
* @deprecated Use the standard React `ref` prop instead.
*/
innerRef?: Ref<any>;
}
Expand Down Expand Up @@ -258,6 +259,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
renderIcon: IconElement = OverflowMenuVertical,
selectorPrimaryFocus = '[data-floating-menu-primary-focus]',
size = 'md',
innerRef,
...other
},
ref
Expand Down Expand Up @@ -543,6 +545,9 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
})}
</FloatingMenu>
);
const combinedRef = innerRef
? mergeRefs(triggerRef, innerRef, ref)
: mergeRefs(triggerRef, ref);

return (
<>
Expand All @@ -559,7 +564,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
className={overflowMenuClasses}
onClick={handleClick}
id={id}
ref={mergeRefs(triggerRef, ref)}
ref={combinedRef}
size={size}
label={iconDescription}
kind="ghost">
Expand Down
Loading