mantine
mantine copied to clipboard
Wrong alignment behavior with Tooltip/Popover components
What package has an issue
@mantine/core
Describe the bug
I like that you choose to go with Floating UI as a base for all tooltips, dropdowns, etc... I love Floating UI, but with mantine I don't have the same behavior.
Image you have a tooltip text with approx. maximum width 450px. Also you support responsiveness for at least 320px.
We have different behavior than expected (as in Floating UI) in this cases:
- when width of tooltip is not enough to to fill free space between target and direction side
- when viewport is less then width of tooltip
Expected behavior
Default (when free space is enough)

When free space is not enough:

Actual behavior
- for both cases we have broken layout - content of tooltip is outside of viewport, or for multiline target it has broken alignment (especially when arrow is enabled)
What version of @mantine/hooks page do you have in package.json?
latest
If possible, please include a link to a codesandbox with the reproduced problem
No response
Do you know how to fix the issue
No response
Are you willing to participate in fixing this issue and create a pull request with the fix
No response
Possible fix
Refactor logic to prevent usage of direct width value (maxWidth is preferable), also I think you need to research edge cases
Another case
https://user-images.githubusercontent.com/18473137/191240900-c7f0ed2d-56ab-484a-8b7b-74b6a74e980b.mp4
Fixed in 5.5.0
@rtivital For me issue still present :(
- Open the DEMO
- Hover once to show tooltip first time. Then, resize ui window to the minimum allowed size, then try again to hover to tooltip trigger (
(i)) multiple times, you will see weird behavior like one that I have and demonstrated you in video from above. Note, that I have this issue without resizes and etc., but maybe it's a common case, and if a fix for this exists, maybe it will help me.
No idea about that, maybe some issue with floating ui. Anyway, the issue was originally about arrow position and inline tooltip, the shift of tooltip is probably unrelated.
Hey @rtivital I think the issue is this:
https://github.com/mantinedev/mantine/blob/aa3b261914a873ede6ade244cda5df8812aec71c/src/mantine-core/src/Tooltip/Tooltip.tsx#L153-L154
The ?? '' should be ?? 0. By laying out the element at the top-left of the screen initially, it will have its "final" dimensions ready. Currently, it gets resized by the layout when at the right of the screen, which causes it to have smaller initial dimensions than it should.
More info:
Thanks, @atomiks, I'll change that to zero. I remember taking this exact logic from one of the floating ui examples about 4 months, it must have been changed since then.
I've changed empty string to 0 in 5.5.6, it will be out soon
I think it should also be done for Popover:
https://github.com/mantinedev/mantine/blob/4fa634ebddf15a8515a9018e37b1e86d2e00883a/src/mantine-core/src/Popover/PopoverDropdown/PopoverDropdown.tsx#L71-L72
Another thing I discovered is if you resize the window to be narrow after it gets positioned for the first time, it can have the wrong position for subsequent renders. (Assuming the useFloating() hook does not get unmounted between renders).
There are two fixes for that:
- Set
width: max-contenton the floating element. This ensures parent containers don't affect its dimensions, no matter where it is located on the document, so it can be measured properly.
OR
- Use
transform: translate()for the coordinates, and keeptop/leftto0at all times. The layout will not be affected in this case either, and its dimensions will be full size even when it pushes up against the edge of the document.
I've added the same changes as in Tooltip to Popover and set width: max-content