[select] Fix support for transform animations when alignItemWithTrigger is active#3532
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
b41e14a to
c400b24
Compare
| const popupTop = positionerRect.top; | ||
| const popupHeight = positionerRect.height; | ||
| const textCenterY = textRect.top + textRect.height / 2; | ||
|
|
||
| const transformOriginY = | ||
| popupHeight > 0 ? ((textCenterY - popupTop) / popupHeight) * 100 : 50; | ||
|
|
||
| const clampedY = clamp(transformOriginY, 0, 100); | ||
|
|
||
| popupElement.style.setProperty('--transform-origin', `50% ${clampedY}%`); |
There was a problem hiding this comment.
Since the addition of try {} finally {} breaks the diff:
This part is new: it maintains a correct transform origin based on the location of the item.
| // Ensure we remove any transforms that can affect the location of the popup | ||
| // and therefore the calculations. | ||
| const originalTransform = popupElement.style.transform; | ||
| popupElement.style.transform = 'none'; | ||
| popupElement.style.removeProperty('--transform-origin'); |
There was a problem hiding this comment.
The key solution is to remove any transforms when doing the initial calculations (which is the "final" state of the popup location) then restore it back afterwards.
c400b24 to
d2eb3af
Compare
8cb8c74 to
d81e200
Compare
d81e200 to
fb7ad22
Compare
LukasTy
left a comment
There was a problem hiding this comment.
Looks nice. 👍
Fixes the initially (on first open) misplaced Popup problem. 👌
P.S. Best to review by adding ?w=1 to the URL (hide whitespace).
TIL! |
Currently transform animations cause a misalignment with the positioning of
alignItemWithTrigger(more noticeable with smaller initial scales or bigger lists). We disable this on our docs, but shadcn/create is using this as a default. Plus Radix seems to handle it correctly.Exaggerated for effect: https://stackblitz.com/edit/y6zpn7cf
https://deploy-preview-3532--base-ui.netlify.app/react/handbook/animation#animating-select-component-with-motion