Skip to content
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

Merged
merged 6 commits into from May 19, 2023

Conversation

lesha1201
Copy link
Contributor

@lesha1201 lesha1201 commented Mar 19, 2023

Description

This PR upgrades major version of @floating-ui/react-dom in Popper component.

Noticeble release notes from Floating UI:

The PR does the following:

  • Upgrades @floating-ui/react-dom to 2.0.0.
  • Changes @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.
  • Removes unnecessary logic/workarounds after the upgrade.
  • Removed filtering falsy values in middleware because now it's done inside @floating-ui/react-dom.

Affected issues:

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)`
Copy link
Contributor Author

@lesha1201 lesha1201 Mar 19, 2023

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.

Copy link

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.

Copy link
Contributor Author

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

Copy link

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link

@atomiks atomiks May 13, 2023

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CleanShot 2023-05-17 at 13 42 08@2x

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?

Copy link

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

Copy link
Contributor Author

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.

@lesha1201
Copy link
Contributor Author

lesha1201 commented Apr 26, 2023

@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.

@atomiks
Copy link

atomiks commented May 11, 2023

@lesha1201 in the latest version of @floating-ui/react-dom, there are quite a few changes that make a lot of this even simpler. I'll try to review them

Comment on lines 248 to 256
// 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]);
Copy link

@atomiks atomiks May 11, 2023

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 180 to 181
// default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues
strategy: 'fixed',
Copy link

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link

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.

Comment on lines 199 to 202
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`);
Copy link

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

Copy link
Contributor Author

@lesha1201 lesha1201 May 13, 2023

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Comment on lines 211 to 214
// assign the reference dynamically once `Content` has mounted so we can collocate the logic
useLayoutEffect(() => {
reference(context.anchor);
}, [reference, context.anchor]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this effect and pass the elements in directly in v2:

useFloating({
  elements: {reference: context.anchor},
});
Screenshot 2023-05-12 at 3 44 49 am

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@lesha1201
Copy link
Contributor Author

@atomiks Thank you for the review! I will it handle it when I can (I hope this weekend).

Copy link
Collaborator

@benoitgrelard benoitgrelard left a 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 = () => {
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Collaborator

hmm, weird, some more differences in the latest chromatic run, only in some of the tooltip stories:

CleanShot 2023-05-17 at 19 45 19@2x

Timing?
That's worrying though… 🤔

@atomiks
Copy link

atomiks commented May 17, 2023

@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.

@lesha1201
Copy link
Contributor Author

@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?

@benoitgrelard
Copy link
Collaborator

Yeah we might have to introduce some delay to some of those stories. We do this in places already.
Otherwise, I fear we will keep getting some false negatives like this.

Btw, @atomiks, have you seen this my comment here? #2018 (comment)

@atomiks
Copy link

atomiks commented May 17, 2023

Yeah I forgot, feel free to open an issue!

@benoitgrelard
Copy link
Collaborator

Yeah I forgot, feel free to open an issue!

floating-ui/floating-ui#2333

@benoitgrelard
Copy link
Collaborator

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.

Copy link
Collaborator

@benoitgrelard benoitgrelard left a 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 🎉

@benoitgrelard benoitgrelard merged commit bf3ad31 into radix-ui:main May 19, 2023
5 checks passed
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.

Popover - invalid value for --radix-popper-available-width
3 participants