Skip to content

Commit

Permalink
Merge pull request #492 from alpinejs/next-tick-fix
Browse files Browse the repository at this point in the history
Fix transition nextTick
  • Loading branch information
calebporzio authored May 15, 2020
2 parents 7b17259 + 239b7df commit 37151dd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
12 changes: 10 additions & 2 deletions dist/alpine-ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -5467,7 +5467,11 @@
return index < modifiers.indexOf('out');
}.bind(this)) : modifiers;
transitionHelperIn(el, modifiers, show); // Otherwise, we can assume x-transition:enter.
} else if (attrs.length > 0) {
} else if (attrs.filter(function (attr) {
_newArrowCheck(this, _this4);

return ['enter', 'enter-start', 'enter-end'].includes(attr.value);
}.bind(this)).length > 0) {
transitionClassesIn(el, attrs, show);
} else {
// If neither, just show that damn thing.
Expand All @@ -5492,7 +5496,11 @@
return index > modifiers.indexOf('out');
}.bind(this)) : modifiers;
transitionHelperOut(el, modifiers, settingBothSidesOfTransition, hide);
} else if (attrs.length > 0) {
} else if (attrs.filter(function (attr) {
_newArrowCheck(this, _this5);

return ['leave', 'leave-start', 'leave-end'].includes(attr.value);
}.bind(this)).length > 0) {
transitionClassesOut(el, attrs, hide);
} else {
hide();
Expand Down
4 changes: 2 additions & 2 deletions dist/alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@

modifiers = settingBothSidesOfTransition ? modifiers.filter((i, index) => index < modifiers.indexOf('out')) : modifiers;
transitionHelperIn(el, modifiers, show); // Otherwise, we can assume x-transition:enter.
} else if (attrs.length > 0) {
} else if (attrs.filter(attr => ['enter', 'enter-start', 'enter-end'].includes(attr.value)).length > 0) {
transitionClassesIn(el, attrs, show);
} else {
// If neither, just show that damn thing.
Expand All @@ -183,7 +183,7 @@
const settingBothSidesOfTransition = modifiers.includes('in') && modifiers.includes('out');
modifiers = settingBothSidesOfTransition ? modifiers.filter((i, index) => index > modifiers.indexOf('out')) : modifiers;
transitionHelperOut(el, modifiers, settingBothSidesOfTransition, hide);
} else if (attrs.length > 0) {
} else if (attrs.filter(attr => ['leave', 'leave-start', 'leave-end'].includes(attr.value)).length > 0) {
transitionClassesOut(el, attrs, hide);
} else {
hide();
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function transitionIn(el, show, forceSkip = false) {

transitionHelperIn(el, modifiers, show)
// Otherwise, we can assume x-transition:enter.
} else if (attrs.length > 0) {
} else if (attrs.filter(attr => ['enter', 'enter-start', 'enter-end'].includes(attr.value)).length > 0) {
transitionClassesIn(el, attrs, show)
} else {
// If neither, just show that damn thing.
Expand All @@ -176,7 +176,7 @@ export function transitionOut(el, hide, forceSkip = false) {
? modifiers.filter((i, index) => index > modifiers.indexOf('out')) : modifiers

transitionHelperOut(el, modifiers, settingBothSidesOfTransition, hide)
} else if (attrs.length > 0) {
} else if (attrs.filter(attr => ['leave', 'leave-start', 'leave-end'].includes(attr.value)).length > 0) {
transitionClassesOut(el, attrs, hide)
} else {
hide()
Expand Down
63 changes: 63 additions & 0 deletions test/transition.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Alpine from 'alpinejs'
import { wait } from '@testing-library/dom'
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms))

global.MutationObserver = class {
observe() {}
Expand Down Expand Up @@ -145,6 +146,68 @@ test('transition out', async () => {
)
})

test('if only transition leave directives are present, don\'t transition in at all', async () => {
var frameStack = []

jest.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
frameStack.push(callback)
});

document.body.innerHTML = `
<div x-data="{ show: false }">
<button x-on:click="show = ! show"></button>
<span x-show="show"
x-transition:leave="leave"
x-transition:leave-start="leave-start"
x-transition:leave-end="leave-end"
></span>
</div>
`

Alpine.start()

await wait(() => { expect(document.querySelector('span').getAttribute('style')).toEqual('display: none;') })

document.querySelector('button').click()

await timeout(10)

expect(frameStack.length).toEqual(0)
expect(document.querySelector('span').getAttribute('style')).toEqual(null)
})

test('if only transition enter directives are present, don\'t transition out at all', async () => {
var frameStack = []

jest.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
frameStack.push(callback)
});

document.body.innerHTML = `
<div x-data="{ show: true }">
<button x-on:click="show = ! show"></button>
<span x-show="show"
x-transition:enter="enter"
x-transition:enter-start="enter-start"
x-transition:enter-end="enter-end"
></span>
</div>
`

Alpine.start()

await wait(() => { expect(document.querySelector('span').getAttribute('style')).toEqual(null) })

document.querySelector('button').click()

await timeout(10)

expect(frameStack.length).toEqual(0)
expect(document.querySelector('span').getAttribute('style')).toEqual('display: none;')
})

test('original class attribute classes are preserved after transition finishes', async () => {
var frameStack = []

Expand Down

0 comments on commit 37151dd

Please sign in to comment.