Skip to content

Commit 84de066

Browse files
wkeeseheloiselui
andauthored
fix(menu): place submenu on left when no room on right (#22261)
Also fix alignment of menu when on the left. IMO we want the left submenu and right submenu to appear in mirror positions. According to https://floating-ui.com/docs/offset a simple setting like this should do that, regardless of which side the menu appears on: middleware: [offset({ mainAxis: 6, crossAxis: -6 })], But for some reason that doesn't work. I needed to adjust the mainAxis setting depending on whether the menu is to the left or right. Fixes #22252. Co-authored-by: Heloise Lui <71858203+heloiselui@users.noreply.github.com>
1 parent 7a80035 commit 84de066

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
autoUpdate,
3030
offset,
3131
FloatingFocusManager,
32+
flip,
3233
} from '@floating-ui/react';
3334
import { CaretRight, CaretLeft, Checkmark } from '@carbon/icons-react';
3435
import { keys, match } from '../../internal/keyboard';
@@ -149,7 +150,13 @@ export const MenuItem = forwardRef<HTMLLIElement, MenuItemProps>(
149150
},
150151
placement: rtl ? 'left-start' : 'right-start',
151152
whileElementsMounted: autoUpdate,
152-
middleware: [offset({ mainAxis: -6, crossAxis: -6 })],
153+
middleware: [
154+
flip(),
155+
offset(({ placement }) => ({
156+
mainAxis: placement.startsWith('left') ? 10 : -6,
157+
crossAxis: -6,
158+
})),
159+
],
153160
strategy: 'fixed',
154161
});
155162
const { getReferenceProps, getFloatingProps } = useInteractions([

0 commit comments

Comments
 (0)