فهرست منبع

Fix transition issue with leave transition and @click.away

Caleb Porzio 3 سال پیش
والد
کامیت
6993a1ee4d
2فایلهای تغییر یافته به همراه25 افزوده شده و 3 حذف شده
  1. 12 3
      packages/alpinejs/src/directives/x-transition.js
  2. 13 0
      tests/cypress/manual-transition-test.html

+ 12 - 3
packages/alpinejs/src/directives/x-transition.js

@@ -134,9 +134,18 @@ window.Element.prototype._x_toggleAndCascadeWithTransitions = function (el, valu
     let clickAwayCompatibleShow = () => {document.visibilityState === 'visible' ? requestAnimationFrame(show) : setTimeout(show)}
     let clickAwayCompatibleShow = () => {document.visibilityState === 'visible' ? requestAnimationFrame(show) : setTimeout(show)}
 
 
     if (value) {
     if (value) {
-        el._x_transition
-            ? el._x_transition.in(show)
-            : clickAwayCompatibleShow()
+        if (el._x_transition && (el._x_transition.enter || el._x_transition.leave)) {
+            // This fixes a bug where if you are only transitioning OUT and you are also using @click.outside
+            // the element when shown immediately starts transitioning out. There is a test in the manual
+            // transition test file for this: /tests/cypress/manual-transition-test.html
+            (el._x_transition.enter && (Object.entries(el._x_transition.enter.during).length || Object.entries(el._x_transition.enter.start).length || Object.entries(el._x_transition.enter.end).length))
+                ? el._x_transition.in(show)
+                : clickAwayCompatibleShow()
+        } else {
+            el._x_transition
+                ? el._x_transition.in(show)
+                : clickAwayCompatibleShow()
+        }
 
 
         return
         return
     }
     }

+ 13 - 0
tests/cypress/manual-transition-test.html

@@ -120,6 +120,19 @@
             </td>
             </td>
         </tr>
         </tr>
 
 
+        <tr style="border-bottom: 1px solid black;">
+            <td><code>x-transition.leave only</code></td>
+            <td>
+                <div x-data="{ open: false }">
+                    <button @click="open = ! open">Toggle</button>
+
+                    <div x-show="open" @click.outside="open = false" x-transition.out.duration.1000ms>
+                        Hello 👋
+                    </div>
+                </div>
+            </td>
+        </tr>
+
         <tr>
         <tr>
             <td>
             <td>
                 <div><code>x-transition:enter.opacity.duration.500ms</code></div>
                 <div><code>x-transition:enter.opacity.duration.500ms</code></div>