Bläddra i källkod

Make x-transition delay syntax consistent with duration syntax (#3476)

* Make x-transition delay syntax consistent with duration syntax

* Add testing for duration and delay syntax without ms

---------

Co-authored-by: Caleb Porzio <calebporzio@gmail.com>
trych 2 år sedan
förälder
incheckning
e394a47076

+ 4 - 4
packages/alpinejs/src/directives/x-transition.js

@@ -50,7 +50,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
     let wantsScale = wantsAll || modifiers.includes('scale')
     let wantsScale = wantsAll || modifiers.includes('scale')
     let opacityValue = wantsOpacity ? 0 : 1
     let opacityValue = wantsOpacity ? 0 : 1
     let scaleValue = wantsScale ? modifierValue(modifiers, 'scale', 95) / 100 : 1
     let scaleValue = wantsScale ? modifierValue(modifiers, 'scale', 95) / 100 : 1
-    let delay = modifierValue(modifiers, 'delay', 0)
+    let delay = modifierValue(modifiers, 'delay', 0) / 1000
     let origin = modifierValue(modifiers, 'origin', 'center')
     let origin = modifierValue(modifiers, 'origin', 'center')
     let property = 'opacity, transform'
     let property = 'opacity, transform'
     let durationIn = modifierValue(modifiers, 'duration', 150) / 1000
     let durationIn = modifierValue(modifiers, 'duration', 150) / 1000
@@ -60,7 +60,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
     if (transitioningIn) {
     if (transitioningIn) {
         el._x_transition.enter.during = {
         el._x_transition.enter.during = {
             transformOrigin: origin,
             transformOrigin: origin,
-            transitionDelay: delay,
+            transitionDelay: `${delay}s`,
             transitionProperty: property,
             transitionProperty: property,
             transitionDuration: `${durationIn}s`,
             transitionDuration: `${durationIn}s`,
             transitionTimingFunction: easing,
             transitionTimingFunction: easing,
@@ -80,7 +80,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
     if (transitioningOut) {
     if (transitioningOut) {
         el._x_transition.leave.during = {
         el._x_transition.leave.during = {
             transformOrigin: origin,
             transformOrigin: origin,
-            transitionDelay: delay,
+            transitionDelay: `${delay}s`,
             transitionProperty: property,
             transitionProperty: property,
             transitionDuration: `${durationOut}s`,
             transitionDuration: `${durationOut}s`,
             transitionTimingFunction: easing,
             transitionTimingFunction: easing,
@@ -318,7 +318,7 @@ export function modifierValue(modifiers, key, fallback) {
         if (isNaN(rawValue)) return fallback
         if (isNaN(rawValue)) return fallback
     }
     }
 
 
-    if (key === 'duration') {
+    if (key === 'duration' || key === 'delay') {
         // Support x-transition.duration.500ms && duration.500
         // Support x-transition.duration.500ms && duration.500
         let match = rawValue.match(/([0-9]+)ms/)
         let match = rawValue.match(/([0-9]+)ms/)
         if (match) return match[1]
         if (match) return match[1]

+ 33 - 1
tests/cypress/integration/directives/x-transition.spec.js

@@ -81,6 +81,38 @@ test('transition:enter in nested x-show visually runs',
     }
     }
 )
 )
 
 
+test('transition duration and delay with and without ms syntax',
+    html`
+        <div x-data="{ showMs: false, showBlank: false }">
+
+            <span class="ms"
+                  x-show="showMs"
+                  x-transition.delay.80ms.duration.120ms>ms syntax</span>
+            <span class="blank"
+                  x-show="showBlank"
+                  x-transition.delay.80.duration.120>blank syntax</span>
+
+            <button class="ms"    x-on:click="showMs = true"></button>
+            <button class="blank" x-on:click="showBlank = true"></button>
+        </div>
+    `,
+    ({ get }) => {
+        get('span.ms').should(notBeVisible())
+        get('button.ms').click()
+        get('span.ms').should(notBeVisible()) // Not visible due to delay
+        get('span.ms').should(beVisible())
+        get('span.ms').should(notHaveComputedStyle('opacity', '1')) // We expect a value between 0 and 1
+        get('span.ms').should(haveComputedStyle('opacity', '1')) // Eventually opacity will be 1
+
+        get('span.blank').should(notBeVisible())
+        get('button.blank').click()
+        get('span.blank').should(notBeVisible()) // Not visible due to delay
+        get('span.blank').should(beVisible())
+        get('span.blank').should(notHaveComputedStyle('opacity', '1')) // We expect a value between 0 and 1
+        get('span.blank').should(haveComputedStyle('opacity', '1')) // Eventually opacity will be 1
+    }
+)
+
 test(
 test(
     'bound x-transition can handle empty string and true values',
     'bound x-transition can handle empty string and true values',
     html`
     html`
@@ -107,4 +139,4 @@ test(
             get('span').should(beVisible())
             get('span').should(beVisible())
         }
         }
     
     
-);
+);