Skip to content

Commit

Permalink
Make x-transition delay syntax consistent with duration syntax (#3476)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
trych and calebporzio committed May 11, 2023
1 parent c6cee92 commit e394a47
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/alpinejs/src/directives/x-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
let wantsScale = wantsAll || modifiers.includes('scale')
let opacityValue = wantsOpacity ? 0 : 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 property = 'opacity, transform'
let durationIn = modifierValue(modifiers, 'duration', 150) / 1000
Expand All @@ -60,7 +60,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
if (transitioningIn) {
el._x_transition.enter.during = {
transformOrigin: origin,
transitionDelay: delay,
transitionDelay: `${delay}s`,
transitionProperty: property,
transitionDuration: `${durationIn}s`,
transitionTimingFunction: easing,
Expand All @@ -80,7 +80,7 @@ function registerTransitionsFromHelper(el, modifiers, stage) {
if (transitioningOut) {
el._x_transition.leave.during = {
transformOrigin: origin,
transitionDelay: delay,
transitionDelay: `${delay}s`,
transitionProperty: property,
transitionDuration: `${durationOut}s`,
transitionTimingFunction: easing,
Expand Down Expand Up @@ -318,7 +318,7 @@ export function modifierValue(modifiers, key, fallback) {
if (isNaN(rawValue)) return fallback
}

if (key === 'duration') {
if (key === 'duration' || key === 'delay') {
// Support x-transition.duration.500ms && duration.500
let match = rawValue.match(/([0-9]+)ms/)
if (match) return match[1]
Expand Down
34 changes: 33 additions & 1 deletion tests/cypress/integration/directives/x-transition.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'bound x-transition can handle empty string and true values',
html`
Expand All @@ -107,4 +139,4 @@ test(
get('span').should(beVisible())
}

);
);

0 comments on commit e394a47

Please sign in to comment.