Skip to content

Commit 46482c9

Browse files
committed
add transition class for v-transition elements
1 parent 6571ac5 commit 46482c9

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/directives/transition.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var _ = require('../util')
2+
13
module.exports = {
24

35
priority: 1000,
@@ -9,7 +11,7 @@ module.exports = {
911
}
1012
},
1113

12-
update: function (id) {
14+
update: function (id, oldId) {
1315
var vm = this.el.__vue__ || this.vm
1416
this.el.__v_trans = {
1517
id: id,
@@ -19,6 +21,10 @@ module.exports = {
1921
// computed CSS.
2022
fns: vm.$options.transitions[id]
2123
}
24+
if (oldId) {
25+
_.removeClass(this.el, oldId + '-transition')
26+
}
27+
_.addClass(this.el, (id || 'v') + '-transition')
2228
}
2329

2430
}

src/transition/css.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var removeClass = _.removeClass
44
var transDurationProp = _.transitionProp + 'Duration'
55
var animDurationProp = _.animationProp + 'Duration'
66

7+
var TYPE_TRANSITION = 1
8+
var TYPE_ANIMATION = 2
9+
710
var queue = []
811
var queued = false
912

@@ -43,7 +46,8 @@ function flush () {
4346
queue.forEach(run)
4447
queue = []
4548
queued = false
46-
/* dummy return, so js linters don't complain about unused variable f */
49+
// dummy return, so js linters don't complain about unused
50+
// variable f
4751
return f
4852
}
4953

@@ -63,13 +67,13 @@ function run (job) {
6367
var transitionType = getTransitionType(el, data, cls)
6468

6569
if (job.dir > 0) { // ENTER
66-
if (transitionType === 1) {
70+
if (transitionType === TYPE_TRANSITION) {
6771
// trigger transition by removing enter class
6872
removeClass(el, cls)
6973
// only need to listen for transitionend if there's
7074
// a user callback
7175
if (cb) setupTransitionCb(_.transitionEndEvent)
72-
} else if (transitionType === 2) {
76+
} else if (transitionType === TYPE_ANIMATION) {
7377
// animations are triggered when class is added
7478
// so we just listen for animationend to remove it.
7579
setupTransitionCb(_.animationEndEvent, function () {
@@ -84,7 +88,7 @@ function run (job) {
8488
if (transitionType) {
8589
// leave transitions/animations are both triggered
8690
// by adding the class, just remove it on end event.
87-
var event = transitionType === 1
91+
var event = transitionType === TYPE_TRANSITION
8892
? _.transitionEndEvent
8993
: _.animationEndEvent
9094
setupTransitionCb(event, function () {
@@ -130,8 +134,6 @@ function run (job) {
130134
* @param {Object} data
131135
* @param {String} className
132136
* @return {Number}
133-
* 1 - transition
134-
* 2 - animation
135137
*/
136138

137139
function getTransitionType (el, data, className) {
@@ -143,13 +145,13 @@ function getTransitionType (el, data, className) {
143145
inlineStyles[transDurationProp] ||
144146
computedStyles[transDurationProp]
145147
if (transDuration && transDuration !== '0s') {
146-
type = 1
148+
type = TYPE_TRANSITION
147149
} else {
148150
var animDuration =
149151
inlineStyles[animDurationProp] ||
150152
computedStyles[animDurationProp]
151153
if (animDuration && animDuration !== '0s') {
152-
type = 2
154+
type = TYPE_ANIMATION
153155
}
154156
}
155157
if (type) {

test/unit/specs/directives/repeat_spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,11 @@ if (_.inBrowser) {
585585
})
586586
vm.items.splice(1, 1, {a:4})
587587
setTimeout(function () {
588-
expect(el.innerHTML).toBe('<div>1</div><div>4</div><div>3</div>')
588+
expect(el.innerHTML).toBe(
589+
'<div class="test-transition">1</div>' +
590+
'<div class="test-transition">4</div>' +
591+
'<div class="test-transition">3</div>'
592+
)
589593
document.body.removeChild(el)
590594
done()
591595
}, 100)
@@ -614,7 +618,7 @@ if (_.inBrowser) {
614618

615619
it('nested track by', function (done) {
616620
assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}<div v-repeat="list" track-by="id">{{msg}}</div></div>', function () {
617-
assertTrackBy('<div v-transition v-repeat="list" track-by="id">{{msg}}<div v-transition v-repeat="list" track-by="id">{{msg}}</div></div>', done)
621+
assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}<div v-repeat="list" track-by="id">{{msg}}</div></div>', done)
618622
})
619623

620624
function assertTrackBy(template, next) {

test/unit/specs/directives/transition_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ if (_.inBrowser) {
2323
dir.bind()
2424
expect(dir.el.__v_trans.id).toBe('test')
2525
expect(dir.el.__v_trans.fns).toBe(fns)
26+
expect(dir.el.className === 'test-transition')
27+
dir.update('lol', 'test')
28+
expect(dir.el.__v_trans.id).toBe('lol')
29+
expect(dir.el.__v_trans.fns).toBeUndefined()
30+
expect(dir.el.className === 'lol-transition')
2631
})
2732

2833
it('dynamic transitions', function (done) {

0 commit comments

Comments
 (0)