Skip to content

Bug: Drag and Drop not working inside MatDialog (wrong previous and current index) #15880

Closed

Description

@betancourtc

What is the expected behavior?

Reorder items in a list inside Mat-dialog using Angular Material Drag and Drop.

What is the current behavior?

Drag and drop does not work properly on the last items of the list. Previous and current index are the same in the moveItemInArray function

What are the steps to reproduce?

I have an array, where each item contains a list of colors. Clicking the edit button opens an Angular Material Dialog with the list of colors (for that item). The colors can be reordered using drag and drop. The list of colors from the first items of the list work fine, but when trying to reorder the list of colors in the last items of the list, drag and drop does not work. I did some debugging and found out that for the items not working, the event passed to the moveItemInArray, has the same number for the current and the previous index.

As previously mentioned, click on the edit button for one of the first items in the list and try reordering the colors, it works fine, choose the last item in the list and you will see the colors cannot be reordered, take a look at the console to get the previous and current indexes.

StackBlitz starter: https://stackblitz.com/edit/angular-u6dt7a

Not sure whether this is a CDK or a Mat-Dialog dialog.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 7, Angular Material 7.3.7
Chrome, Firefox

Is there anything else we should know?

When reducing the list of items to just a couple of them, then all list of colors for all items work fine. Problem starts when the list of items starts to grow in number.

Activity

  1. Airblader commented on Apr 24, 2019

    @Airblader
    Contributor

    I'm having the suspicion that this somehow has to do with MatDialog. I've also observed that sometimes the indices are different, but still not correct. It also seems to only happen in combination with cdkDropList, a standalone cdkDrag works fine.

    For the record, the data passed to the dialog doesn't matter – you can even pass a static array and observe the same effect.

  2. changed the title [-]Bug: Drag and Drop not working (wrong previous and current index)[/-] [+]Bug: Drag and Drop not working inside MatDialog (wrong previous and current index)[/+] on Apr 25, 2019
  3. betancourtc commented on Apr 26, 2019

    @betancourtc
    Author

    I wasn't sure either where the problem was, but as you mentioned it might be on the MatDialog side and not the CDK. Any ideas on how to get this working? I don't see any errors on the console, so don't really know where to start looking to get a fix for this.

  4. Airblader commented on Apr 26, 2019

    @Airblader
    Contributor

    It seems to correlate of the combination of opening a dialog when you have scrolled the page.

    Try this: make the window as big as possible, load the app and open the dialog for the most bottom item without scrolling; it works fine. Now make the window smaller, reload the app, scroll to the same item and edit it again; it will now break.

    As you can also see, when a dialog opens, somehow the body scroll disappears, which I assume is intended behavior for dialogs. I think this somehow interferes with the calculations done for the drag/drop translate styles.

  5. Airblader commented on Apr 26, 2019

    @Airblader
    Contributor

    Disabling the cdk-global-scrollblock class applied to the <html> tag when modals open makes the issue disappear.

    Edit: If cdk-global-scrollback is applied to body rather than html it also seems to work. I'm sure this would break something else, of course. ;-)

    html.cdk-global-scrollblock {
        position: initial !important;
    }
    
    html.cdk-global-scrollblock body {
        position: fixed;
    }
    
  6. Airblader commented on Apr 26, 2019

    @Airblader
    Contributor

    @betancourtc I think a workaround would be to inject a MAT_DIALOG_SCROLL_STRATEGY that does not use the block scroll strategy. This is of course suboptimal since you probably do not want to allow scrolling while a modal is open – it should make the drag and drop work for now, though.

  7. Airblader commented on Apr 26, 2019

    @Airblader
    Contributor

    This is about as far as I was able to get. I think it would be good if the people working on drag and drop could weigh in now, at least we identified that the issue is the scrollblock.

  8. betancourtc commented on May 3, 2019

    @betancourtc
    Author

    @Airblader Thanks a lot for your help. I hope too the people working on the drag and drop module take a look at this. Cheers

  9. andrewseguin commented on May 7, 2019

    @andrewseguin
    Contributor

    I'm unable to reproduce the issue, the drag and drop seems to be working correctly. Can you confirm it is breaking and let me know what browser and version you are using?

  10. added
    needs: clarification
    P4
    on May 7, 2019
  11. betancourtc commented on May 8, 2019

    @betancourtc
    Author

    @andrewseguin I was able to reproduce the issue trying to drag and drop the colors on the last theme (number 50) from my example in StackBlitz on Chrome Version 73.0.3683.103 (64-bit) and on Firefox Version 66.0.3 (64-bit). Both on Windows 10.

  12. Airblader commented on May 8, 2019

    @Airblader
    Contributor

    @andrewseguin The problem only occurs when you click "Edit" on one of the items on the bottom if you had to scroll (significantly). If you have a big monitor, make sure to resize the window to something small so you need to scroll down first.

    I am absolutely able to reproduce with the provided StackBlitz (as can be seen by my previous research efforts). The issue gets gradually worse as you have scrolled more, and at some point it just breaks entirely.

  13. Airblader commented on May 8, 2019

    @Airblader
    Contributor

    BTW, I somewhat disagree with the P4 label. Drag and drop in a modal is no edge-case, and this is part of the CDK flat-out breaking in its core functionality.

  14. striky1 commented on Jul 2, 2019

    @striky1

    Hi here,

    I can confirm that it is not working also on:

    
    Angular CLI: 8.0.3
    Node: 10.16.0
    OS: darwin x64
    Angular: 8.0.1
    

    If I was scrolled down on site and then I opened dialog with Drag&Drop two lists, order in them or move item from list to another list is not working. Event of drop function returns same container for previous or current container property, with same indexes.

    Can u take a look on that, pls? :)

  15. 6 remaining items

  16. ENM1989 commented on Nov 26, 2019

    @ENM1989

    Same problem here with mat-dialog and drag and drop.

  17. uvcreation commented on Dec 9, 2019

    @uvcreation

    Same problem here, is someone got the solution for this issue?

  18. larqitos commented on Dec 11, 2019

    @larqitos

    @uvcreation you can take a look at my quick fix here: #15880 (comment)

  19. uvcreation commented on Dec 11, 2019

    @uvcreation

    @larqitos It works for me after using ::ng-deep. Thanks mate!

  20. kotsopoulosd commented on Dec 20, 2019

    @kotsopoulosd

    Same problem here but derives from different actions. (It has nothing to do with scrolling in my case)

    The drag and drop lists are inside matDialog. The first time I open the dialog works fine in Chrome and IE. No matter how many times I open the same matDialog everything works fine.

    However if I open another matDialog and then try to open the MatDialog with the drag and drop list, it does not work. The event.container is the same as the event.previousContainer and it does not drop the item neither in the left nor in the right list and vise versa.
    The funny thing is that works fine with IE but not with Chrome.

    Angular CLI: 8.3.8
    Node: 10.16.3
    OS: win32 x64
    Angular: 8.2.9

  21. Hydhen commented on May 12, 2020

    @Hydhen

    I have the same problem, but just with a single dialog, that contains a drag'n'drop directive, the directive works fine within my parent component, but doesn't react to drag inside a dialog
    I have no scroll into my parent nor my dialog

  22. uvcreation commented on Jun 10, 2020

    @uvcreation

    I have the same problem, but just with a single dialog, that contains a drag'n'drop directive, the directive works fine within my parent component, but doesn't react to drag inside a dialog
    I have no scroll into my parent nor my dialog

    try to add this to your component css file

    ::ng-deep html.cdk-global-scrollblock {
    top: 0px !important;
    }

  23. added
    needs triage
    and removed
    needs: clarification
    on Aug 13, 2020
  24. crisbeto commented on Aug 16, 2020

    @crisbeto
    Member

    Duplicate of #14280.

  25. removed
    needs triage
    on Aug 16, 2020
  26. angular-automatic-lock-bot commented on Sep 16, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  27. locked and limited conversation to collaborators on Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area: cdk/drag-drop

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions