Skip to content

Conversation

ikapta
Copy link
Contributor

@ikapta ikapta commented Jul 30, 2025

Fixes #7986

Submenu Positioning with Floating UI

Overview of Floating UI

flip(): Adjusts placement (e.g., top to bottom) to prevent overflow.
shift(): Moves the floating element along the x or y axis to stay within the viewport. By default, only mainAxis=true.

Problem Description

Since the submenu is set to position=right-start, the mainAxis changes to the y-axis in this horizontal placement. By default, shift() only adjusts the mainAxis, which may lead to inconsistent behavior due to Floating UI's handling.

Solution

Enable crossAxis: true in shift() to allow x-axis adjustments, aligning the submenu to the viewport's right boundary. As shift() precedes flip() in the usePopover middleware order, flip() does not trigger horizontal placement changes. The current middleware order in usePopover is sufficient, ensuring a visually consistent right-aligned submenu.

Visual Results

image image image

Per Floating UI docs: "Enabling this can lead to the floating element overlapping the reference element, which may not be desired and is often replaced by the flip() middleware."
However, this approach is effective for our use case.

Alternative Approach

Updating the middleware order to [flip(), shift()] would flip the submenu to left-start on overflow before shifting. The results would appear as follows:
image

image

Conclusion

Both solutions are effective. I opted for the minimal code change by enabling crossAxis: true in shift() for the align-right approach. Users can modify position and middleware but should be familiar with Floating UI middleware to understand the implications of their changes.

@rtivital rtivital merged commit 6f1d835 into mantinedev:master Jul 30, 2025
1 check passed
@rtivital
Copy link
Member

Thanks!

@clicktodev
Copy link
Contributor

clicktodev commented Jul 31, 2025

Thanks! However the second behavior seems like a much better default because the merged behavior completely hides the parent menu if the child has a multiple word label and the menu is on the edge of the screen.

@ikapta
Copy link
Contributor Author

ikapta commented Jul 31, 2025

Yes, it’s a thoughtful approach.
Pros: In most cases, with only a second-level submenu, if it hits the right edge, it can flip to the left.
Cons: This way not solve the long label, smaller screens or beyond second submenus, the submenu may still hit an edge and overlap the parent menu. This may rare cases.

@rtivital What's your advice? If it's okay, could adjust the middleware order by default.

@rtivital
Copy link
Member

I like the merged behavior more. Let's leave it as is. It should still be possible to customize on the user side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SubMenu overflows on small screen widths
3 participants