Skip to content

Commit

Permalink
fix(Menu): focused items should scrollintoview when overflowed
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcfaul committed Jul 26, 2023
1 parent 0d5ec4f commit a9848b5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/react-core/src/components/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ const MenuItemBase: React.FunctionComponent<MenuItemProps> = ({
setFlyoutRef(null);
}
};

React.useEffect(() => {
if (isFocused && ref.current) {
const itemEl = ref.current;
const parentListEl = itemEl.parentElement;

if (parentListEl) {
const isAboveTop = itemEl.offsetTop - parentListEl.offsetTop < parentListEl.scrollTop;
const isBelowBottom = itemEl.offsetTop - parentListEl.offsetTop + itemEl.clientHeight;

if (isAboveTop || isBelowBottom) {
itemEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}
}
}, [isFocused]);

const isSelectMenu = menuRole === 'listbox';

const renderItem = (
Expand Down Expand Up @@ -428,13 +445,7 @@ const MenuItemBase: React.FunctionComponent<MenuItemProps> = ({
{...(hasCheckbox && { 'aria-label': ariaLabel })}
{...props}
>
{tooltipProps ? (
<Tooltip {...tooltipProps}>
{renderItem}
</Tooltip>
) : (
renderItem
)}
{tooltipProps ? <Tooltip {...tooltipProps}>{renderItem}</Tooltip> : renderItem}
</li>
);
};
Expand Down

0 comments on commit a9848b5

Please sign in to comment.