New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Popper] Upgrade floating-ui #2032
[Popper] Upgrade floating-ui #2032
Conversation
packages/react/popper/src/Popper.tsx
Outdated
transform: isPlaced | ||
? `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)` | ||
transform: isPositioned | ||
? `translate3d(${Math.round(x as number)}px, ${Math.round(y as number)}px, 0)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking isPositioned
makes sure that x
and y
are not null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the new floatingStyles
object returned from the hook v2
instead of computing the styles here. It performs rounding based on DPR and should be more accurate.
This point: // keep off the page when measuring
shouldn't matter because the browser doesn't paint until it's positioned anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the browser doesn't paint until it's positioned anyway
@atomiks Why not? I tried to remove this and in heavily loaded story, the difference is noticable.
Desktop.2023.05.13.-.15.34.14.02.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! Yeah, for SSR etc, then keeping it out of view (or using visibility: isPositioned ? undefined : 'hidden'
) is the correct approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atomiks I kept the logic for hidding it when it's not positioned but changed to use computed value from floatingStyles
when it's positioned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that there is translate
in floatingStyles
and translate3d
in Radix. I will change it to translate
to make it consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the object adds willChange: 'transform'
to apply GPU acceleration, making translate
act as translate3d
. It's only applied to high PPI displays to reduce blurring issues on Windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It performs rounding based on DPR and should be more accurate.
@atomiks I'm guessing this is what we're seeing here caught up by Chromatic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd assume so yes
packages/react/popper/package.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't upgrade version for the package because I'm not sure what version it should be. My suggestion is to bump minor version since this component heavily relies on Floating UI but doesn't bring any breaking changes to the end users.
@benoitgrelard @andy-hook Sorry for pinging. Can you please review? Does this PR is blocked by something? It would be nice to have this PR released because some projects might already use a new Floating UI version and installing Radix (Popper, Tooltip) results in duplicate packages. |
@lesha1201 in the latest version of |
packages/react/popper/src/Popper.tsx
Outdated
// we need to re-compute the positioning once the parent has finally been placed. | ||
// https://github.com/floating-ui/floating-ui/issues/1531 | ||
useLayoutEffect(() => { | ||
if (isRoot && isPlaced) { | ||
if (isRoot && isPositioned) { | ||
Array.from(positionUpdateFns) | ||
.reverse() | ||
.forEach((fn) => requestAnimationFrame(fn)); | ||
} | ||
}, [isRoot, isPlaced, positionUpdateFns]); | ||
}, [isRoot, isPositioned, positionUpdateFns]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should no longer be necessary since @floating-ui/dom@1.2.5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
packages/react/popper/src/Popper.tsx
Outdated
// default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues | ||
strategy: 'fixed', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isPositioned
lets you wait until the position is ready now and stick to absolute
, but that's probably a more intensive change for just upgrading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atomiks I agree, I would better leave it as it is for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isPositioned lets you wait until the position is ready now and stick to absolute, but that's probably a more intensive change for just upgrading
@atomiks, can you tell me more about this, specifically around the absolute
strategy? Are you saying absolute
should work for everything now? If so, why is there still a strategy option? This is something that we know we want to work out at some point because although fixed
works consistently for us, we know it isn't the most performant when scrolling, resizing, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means that you can prevent scroll jumps using absolute
, by doing this:
useLayoutEffect(() => {
isPositioned && element.focus();
}, [isPositioned]);
fixed
is useful in two cases:
- The reference element is in a fixed container (or is fixed itself) and the floating element isn't inside the fixed container. This prevents jitters when scrolling.
- "Breaking" the floating element out of a clipping container in many cases (when a containing block isn't being formed) to keep DOM context in tact. Not relevant if you're portalling.
packages/react/popper/src/Popper.tsx
Outdated
contentStyle.setProperty('--radix-popper-available-width', `${availableWidth}px`); | ||
contentStyle.setProperty('--radix-popper-available-height', `${availableHeight}px`); | ||
contentStyle.setProperty('--radix-popper-anchor-width', `${anchorWidth}px`); | ||
contentStyle.setProperty('--radix-popper-anchor-height', `${anchorHeight}px`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bugs with this that people have been filing are fixed in the latest version of @floating-ui/dom
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atomiks Can you please give me the context? What bugs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I found one issue. Now I get it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a story for Popover from the issue.
packages/react/popper/src/Popper.tsx
Outdated
// assign the reference dynamically once `Content` has mounted so we can collocate the logic | ||
useLayoutEffect(() => { | ||
reference(context.anchor); | ||
}, [reference, context.anchor]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@atomiks Thank you for the review! I will it handle it when I can (I hope this weekend). |
41cc3bb
to
0d99cd1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this both! I'll make a few more changes and test out our side.
@@ -24,6 +24,46 @@ export const Styled = () => { | |||
); | |||
}; | |||
|
|||
// Original issue: https://github.com/radix-ui/primitives/issues/2128 | |||
export const Boundary = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
// when nested contents are rendered in portals, they are appended out of order causing | ||
// children to be positioned incorrectly if initially open. | ||
// we need to re-compute the positioning once the parent has finally been placed. | ||
// https://github.com/floating-ui/floating-ui/issues/1531 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this issue has been fixed, hence why you removed all this. There's a bit more that can be removed, I'll take care of it.
@benoitgrelard Timing is my guess yes. Looking locally, all the tooltips paint at the same time (including with 6x slowdown), so I'm not sure how that screenshot is happening. My guess is that multiple screenshots are being taken, then being stitched together, and some are being taken too early, before the JS measurement logic has run when loading the route. This only coincidentally didn't appear before as maybe it was initializing more quickly. |
@benoitgrelard I also think that this is timing because the performance is about the same on my machine as well. Maybe we should set delay for that Story to make it more reliable https://www.chromatic.com/docs/delay? |
Yeah we might have to introduce some delay to some of those stories. We do this in places already. Btw, @atomiks, have you seen this my comment here? #2018 (comment) |
Yeah I forgot, feel free to open an issue! |
|
c7a6c59
to
461afb2
Compare
I haven't see anymore false positive like this one, so won't introduce delays yet. If we notice them, we can add them then. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @lesha1201 and @atomiks for this, I've tested my side, and it all looks great. Merging now 🎉
Description
This PR upgrades major version of
@floating-ui/react-dom
inPopper
component.Noticeble release notes from Floating UI:
The PR does the following:
@floating-ui/react-dom
to2.0.0
.@floating-ui/react-dom
from fixed version to minor version. Since Floating UI uses semantic versioning, it should be safe to use minor version and it will reduce a chance of duplicate packages for Floating UI for the end users.middleware
because now it's done inside@floating-ui/react-dom
.Affected issues: