-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
bug(drag-drop): cdkDragPreview matchSize does not always match size due to view container insertion #19060
Comments
…tainer We create a custom preview through `ViewContainerRef.createEmbeddedView` and we move it to the correct place in the DOM. The problem is that for the brief period that it's in the DOM, we also measure the host node if the `matchSize` option is set. In some cases (e.g. a flex container), this can throw off the size. These changes switch to measuring the size ahead of time when necessary. Fixes angular#19060.
Thank you for the detailed report 👍 . The idea behind inserting the v thiewrough the |
…tainer (#19062) We create a custom preview through `ViewContainerRef.createEmbeddedView` and we move it to the correct place in the DOM. The problem is that for the brief period that it's in the DOM, we also measure the host node if the `matchSize` option is set. In some cases (e.g. a flex container), this can throw off the size. These changes switch to measuring the size ahead of time when necessary. Fixes #19060.
…tainer (#19062) We create a custom preview through `ViewContainerRef.createEmbeddedView` and we move it to the correct place in the DOM. The problem is that for the brief period that it's in the DOM, we also measure the host node if the `matchSize` option is set. In some cases (e.g. a flex container), this can throw off the size. These changes switch to measuring the size ahead of time when necessary. Fixes #19060. (cherry picked from commit cd75979)
…tainer (angular#19062) We create a custom preview through `ViewContainerRef.createEmbeddedView` and we move it to the correct place in the DOM. The problem is that for the brief period that it's in the DOM, we also measure the host node if the `matchSize` option is set. In some cases (e.g. a flex container), this can throw off the size. These changes switch to measuring the size ahead of time when necessary. Fixes angular#19060.
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
The below StackBlitz reproduction is a fork of the horizontal list official example. All that was added was a
cdkDragPreview
with thematchSize
input property.(In this case, the preview element is basically the same thing as the cdkDrag element, which doesn't make too much sense, but it is just for demonstration purposes and could contain different elements/styles)
Upon dragging an item, the preview does not match the same size as the original item/placeholder.
(Notice the "Bronze Age" preview is not as wide causing text wrap)
The cause of this problem is the flexbox droplist container (where the items grow/shrink to fill the available space) combined with how the preview element is created.
The preview template is currently being stamped out in the
cdkDrag
injected view container.If you put a breakpoint immediately after the
viewRef.detectChanges()
line, you will see the preview becoming another item in the list, as creating and inserting the embedded view in the view container essentially just tacks on another sibling.("Bronze Age" was started to be dragged)
Since the preview item is added as a sibling, flexbox recalculates the width of all the items. The
matchSize
functionality is then executed with these modified (smaller, in this case) dimensions.Possible Solution
Is a view container even necessary here?
Changing the above code to the following appears to work just fine.
This creates the same drag preview
viewRef
, except it is not immediately attached to the DOM.The reproduction link contains some copied source code to preview this change - just need to comment/uncomment those lines.
Alternatively, the dragged item could be measured before the preview is created. But if there is no reason to use a view container, the above change seems like the more optimal approach.
Reproduction
StackBlitz
Expected Behavior
Preview to match size.
Actual Behavior
Preview does not match size.
Environment
The text was updated successfully, but these errors were encountered: