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

CSS display: block !important breaks dragging #560

Open
pdmccormick opened this issue Mar 23, 2018 · 1 comment
Open

CSS display: block !important breaks dragging #560

pdmccormick opened this issue Mar 23, 2018 · 1 comment
Labels

Comments

@pdmccormick
Copy link

pdmccormick commented Mar 23, 2018

Issue

Using the !important modifier on the display property causes items to be stuck in place and unable to be moved. This comes up, for example, when using Bootstrap 4 and the d-flex class, which contains the property display: flex !important.

This is because getElementBehindPoint uses the gu-hide class to momentarily hide an element (which at the start of a drag is the mirror element), which itself uses display: none !important.

When the element is not properly hidden first, getElementBehindPoint winds up returning the same element, which in the mirror element case leads to the drop target being considered to be the body element, which causes drag to bail early since the body is outside the container, making the dragged element appear to be stuck in its original location.

Example

In the following example, the items in black can be freely moved and reordered, while the items in red (which have styling display: block !important via a named class) are stuck in place. This patch applies against the current index.html:

index 5830c71..29e05c4 100644
--- a/index.html
+++ b/index.html
@@ -14,9 +14,15 @@
   <div class='parent'>
     <label for='hy'>Move stuff between these two containers. Note how the stuff gets inserted near the mouse pointer? Great stuff.</label>
     <div class='wrapper'>
+      <style>
+      .display-block-important {
+        display: block !important;
+        color: red;
+      }
+      </style>
       <div id='left-defaults' class='container'>
-        <div>You can move these elements between these two containers</div>
-        <div>Moving them anywhere else isn't quite possible</div>
+        <div class='display-block-important'>You can move these elements between these two containers</div>
+        <div class='dispay-block-important'>Moving them anywhere else isn't quite possible</div>
         <div>Anything can be moved around. That includes images, <a href='https://github.com/bevacqua/dragula'>links</a>, or any other nested elements.
         <div class='image-thing'><img src='resources/icon.svg' onerror='this.src="resources/icon.png"' alt='dragula'/></div><sub>(You can still click on links, as usual!)</sub>
         </div>

Proposed solution

Modify getElementBehindPoint to use the CSS visibility property to temporarily hide an element instead of relying on the gu-hide class. This should not affect any of the user-defined styling needs, as this is simply an internal mechanism used to identify what is behind the clicked element.

index 67b9381..49176dd 100644
--- a/dist/dragula.js
+++ b/dist/dragula.js
@@ -584,12 +584,12 @@ function getScroll (scrollProp, offsetProp) {
 }
 
 function getElementBehindPoint (point, x, y) {
-  var p = point || {};
-  var state = p.className;
+  var p = point || { style: {} };
+  var state = p.style.visibility;
   var el;
-  p.className += ' gu-hide';
+  p.style.visibility = 'hidden';
   el = doc.elementFromPoint(x, y);
-  p.className = state;
+  p.style.visibility = state;
   return el;
 }
 
pdmccormick added a commit to pdmccormick/dragula that referenced this issue Mar 23, 2018
@dcantatore dcantatore added the bug label Sep 28, 2020
@thenitai
Copy link

After hours of searching I've found this fix. Other instances of Dragula on the same page worked but not another one. Was exactly the issue with the CSS. Shame that it's still not in the dist :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants