Browse Source

x-collapse: only set overflow:hidden when strictly needed (#2213)

* x-collapse: only set overflow:hidden when strictly needed

* Fix test

* Add manual test

* Fix test

* Update tests/cypress/integration/plugins/collapse.spec.js

Co-authored-by: Simone Todaro <simo.todaro@gmail.com>

* Update tests/cypress/integration/plugins/collapse.spec.js

Co-authored-by: Simone Todaro <simo.todaro@gmail.com>

Co-authored-by: Simone Todaro <simo.todaro@gmail.com>
George Boot 3 years ago
parent
commit
55aa3cbf04

+ 12 - 4
packages/collapse/src/index.js

@@ -3,9 +3,9 @@ export default function (Alpine) {
         let duration = modifierValue(modifiers, 'duration', 250) / 1000
         let floor = 0
 
-        el.style.overflow = 'hidden'
         if (! el._x_isShown) el.style.height = `${floor}px`
         if (! el._x_isShown) el.style.removeProperty('display')
+        if (! el._x_isShown) el.style.overflow = 'hidden'
 
         // Override the setStyles function with one that won't
         // revert updates to the height style.
@@ -27,21 +27,29 @@ export default function (Alpine) {
                 let current = el.getBoundingClientRect().height
 
                 Alpine.setStyles(el, {
-                    height: 'auto'
+                    height: 'auto',
                 })
 
                 let full = el.getBoundingClientRect().height
 
+                Alpine.setStyles(el, {
+                    overflow: null
+                })
+
                 if (current === full) { current = floor }
-                
+
                 Alpine.transition(el, Alpine.setStyles, {
                     during: transitionStyles,
                     start: { height: current+'px' },
                     end: { height: full+'px' },
                 }, () => el._x_isShown = true, () => {})
             },
-    
+
             out(before = () => {}, after = () => {}) {
+                Alpine.setStyles(el, {
+                    overflow: 'hidden'
+                })
+
                 let full = el.getBoundingClientRect().height
 
                 Alpine.transition(el, setFunction, {

+ 4 - 3
tests/cypress/integration/plugins/collapse.spec.js

@@ -9,8 +9,9 @@ test('can collapse and expand element',
     `],
     ({ get }, reload) => {
         get('h1').should(haveComputedStyle('height', '0px'))
+        get('h1').should(haveAttribute('style', 'height: 0px; overflow: hidden;'))
         get('button').click()
-        get('h1').should(haveAttribute('style', 'overflow: hidden; height: auto;'))
+        get('h1').should(haveAttribute('style', 'height: auto;'))
         get('button').click()
         get('h1').should(haveComputedStyle('height', '0px'))
     },
@@ -27,7 +28,7 @@ test('@click.away with x-collapse (prevent race condition)',
     ({ get }) => {
         get('h1').should(haveComputedStyle('height', '0px'))
         get('button').click()
-        get('h1').should(haveAttribute('style', 'overflow: hidden; height: auto;'))
+        get('h1').should(haveAttribute('style', 'height: auto;'))
     }
 )
 
@@ -43,6 +44,6 @@ test('@click.away with x-collapse and borders (prevent race condition)',
     ({ get }) => {
         get('h1').should(haveComputedStyle('height', '0px'))
         get('button').click()
-        get('h1').should(haveAttribute('style', 'overflow: hidden; height: auto;'))
+        get('h1').should(haveAttribute('style', 'height: auto;'))
     }
 )

+ 17 - 2
tests/cypress/manual-transition-test.html

@@ -1,4 +1,5 @@
 <html>
+    <script src="/../../packages/collapse/dist/cdn.js" defer></script>
     <script src="/../../packages/alpinejs/dist/cdn.js" defer></script>
     <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
 
@@ -8,9 +9,9 @@
             <td>
                 <div x-data="{ show: false }">
                     <button @click="show = true">Show</button>
-                  
+
                     <h1 x-show="show" @click.away="show = false">h1</h1>
-                  
+
                     <h2>h2</h2>
                   </div>
                 </div>
@@ -153,5 +154,19 @@
                 </div>
             </td>
         </tr>
+
+        <tr>
+            <td><code>x-collapse with no overflow hidden when expanded</code></td>
+            <td>
+                <div x-data="{ show: false }">
+                    <button @click="show = !show">Show</button>
+
+                    <div x-show="show" x-collapse>
+                        <div class="ring-2 ring-offset-2 ring-red-400">my red ring should be fully visible</div>
+                    </div>
+                  </div>
+                </div>
+            </td>
+        </tr>
     </table>
 </html>