Skip to content

Commit a9848b5

Browse files
committed
fix(Menu): focused items should scrollintoview when overflowed
1 parent 0d5ec4f commit a9848b5

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

packages/react-core/src/components/Menu/MenuItem.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,23 @@ const MenuItemBase: React.FunctionComponent<MenuItemProps> = ({
309309
setFlyoutRef(null);
310310
}
311311
};
312+
313+
React.useEffect(() => {
314+
if (isFocused && ref.current) {
315+
const itemEl = ref.current;
316+
const parentListEl = itemEl.parentElement;
317+
318+
if (parentListEl) {
319+
const isAboveTop = itemEl.offsetTop - parentListEl.offsetTop < parentListEl.scrollTop;
320+
const isBelowBottom = itemEl.offsetTop - parentListEl.offsetTop + itemEl.clientHeight;
321+
322+
if (isAboveTop || isBelowBottom) {
323+
itemEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
324+
}
325+
}
326+
}
327+
}, [isFocused]);
328+
312329
const isSelectMenu = menuRole === 'listbox';
313330

314331
const renderItem = (
@@ -428,13 +445,7 @@ const MenuItemBase: React.FunctionComponent<MenuItemProps> = ({
428445
{...(hasCheckbox && { 'aria-label': ariaLabel })}
429446
{...props}
430447
>
431-
{tooltipProps ? (
432-
<Tooltip {...tooltipProps}>
433-
{renderItem}
434-
</Tooltip>
435-
) : (
436-
renderItem
437-
)}
448+
{tooltipProps ? <Tooltip {...tooltipProps}>{renderItem}</Tooltip> : renderItem}
438449
</li>
439450
);
440451
};

0 commit comments

Comments
 (0)