Skip to content

Commit a7d184e

Browse files
committed
transition class can take format of "enter,leave"
1 parent 9dc45ea commit a7d184e

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

src/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ CompilerProto.compile = function (node, root) {
259259

260260
// CSS class transition
261261
if (transClass) {
262-
node.sd_trans_class = transClass
262+
node.sd_trans_class = utils.split(transClass)
263263
}
264264

265265
// finally, only normal directives left!

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ module.exports = {
159159

160160
// add transition info
161161
node.sd_trans = this.trans
162-
node.sd_trans_class = this.transClass
162+
node.sd_trans_class = utils.split(this.transClass)
163163

164164
// append node into DOM first
165165
// so sd-if can get access to parentNode

src/transition.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,21 @@ transition.codes = codes
5454
/**
5555
* Togggle a CSS class to trigger transition
5656
*/
57-
function applyTransitionClass (el, stage, changeState, className) {
57+
function applyTransitionClass (el, stage, changeState, classes) {
5858

5959
if (!endEvent) {
6060
changeState()
6161
return codes.CSS_SKIP
6262
}
6363

6464
var classList = el.classList,
65-
lastLeaveCallback = el.sd_trans_cb
65+
lastLeaveCallback = el.sd_trans_cb,
66+
className
6667

6768
if (stage > 0) { // enter
6869

70+
className = classes[0]
71+
6972
// cancel unfinished leave transition
7073
if (lastLeaveCallback) {
7174
el.removeEventListener(endEvent, lastLeaveCallback)
@@ -85,6 +88,8 @@ function applyTransitionClass (el, stage, changeState, className) {
8588

8689
} else { // leave
8790

91+
className = classes[classes.length > 1 ? 1 : 0]
92+
8893
// trigger hide transition
8994
classList.add(className)
9095
var onEnd = function (e) {

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ function makeHash () {
1313
return Object.create(null)
1414
}
1515

16+
/**
17+
* trim for map
18+
*/
19+
function trim (str) {
20+
return str.trim()
21+
}
22+
1623
var utils = module.exports = {
1724

1825
hash: makeHash,
@@ -47,6 +54,15 @@ var utils = module.exports = {
4754
})
4855
},
4956

57+
/**
58+
* split a transition class string into array
59+
*/
60+
split: function (classString) {
61+
if (classString) {
62+
return classString.split(',').map(trim)
63+
}
64+
},
65+
5066
/**
5167
* Accurate type check
5268
* internal use only, so no need to check for NaN

test/functional/fixtures/transition.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
-moz-transition: all .2s ease;
1818
-webkit-transition: all .2s ease;
1919
}
20-
.test.shrink {
20+
.test.enter, .test.leave {
2121
height: 0;
2222
padding-top: 0;
2323
padding-bottom: 0;
2424
border-top-width: 0;
2525
border-bottom-width: 0;
2626
}
27+
.test.enter {
28+
background-color: #00f;
29+
}
30+
.test.leave {
31+
background-color: #0f0;
32+
}
2733
</style>
2834
</head>
2935
<body>
@@ -40,15 +46,15 @@
4046
class="test"
4147
sd-repeat="item:items"
4248
sd-if="filter(item)"
43-
sd-transition-class="shrink"
49+
sd-transition-class="enter, leave"
4450
sd-attr="data-id:item.a">
4551
{{item.a}}
4652
</div>
4753
<div
4854
class="test"
4955
sd-repeat="item:items"
5056
sd-show="filter(item)"
51-
sd-transition-class="shrink"
57+
sd-transition-class="enter, leave"
5258
sd-attr="data-id:item.a">
5359
{{item.a}}
5460
</div>

test/functional/specs/transition.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ casper.test.begin('Transition', 23, function (test) {
1616
.thenClick('.button-1')
1717
.wait(minWait, function () {
1818
test.assertElementCount('.test', 4)
19-
test.assertElementCount('.test.shrink', 2)
19+
test.assertElementCount('.test.leave', 2)
2020
})
2121
.wait(transDuration, function () {
2222
test.assertElementCount('.test', 3)
23-
test.assertElementCount('.test.shrink', 0)
23+
test.assertElementCount('.test.leave', 0)
2424
test.assertNotVisible('.test[data-id="1"]')
2525
})
2626
.thenClick('.button-2')
2727
.wait(minWait, function () {
2828
test.assertElementCount('.test', 3)
29-
test.assertElementCount('.test.shrink', 2)
29+
test.assertElementCount('.test.leave', 2)
3030
})
3131
.wait(transDuration, function () {
3232
test.assertElementCount('.test', 2)
@@ -41,7 +41,7 @@ casper.test.begin('Transition', 23, function (test) {
4141
.thenClick('.pop')
4242
.wait(minWait, function () {
4343
test.assertElementCount('.test', 4)
44-
test.assertElementCount('.test.shrink', 2)
44+
test.assertElementCount('.test.leave', 2)
4545
})
4646
.wait(transDuration, function () {
4747
test.assertElementCount('.test', 2)

test/unit/specs/transition.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('UNIT: Transition', function () {
4343
var el = mockEl('css'),
4444
c = mockChange(function () {
4545
c.called = true
46-
assert.ok(el.classList.contains('test'))
46+
assert.ok(el.classList.contains('enter'))
4747
}),
4848
code,
4949
cbCalled = false
@@ -65,7 +65,7 @@ describe('UNIT: Transition', function () {
6565
})
6666

6767
it('should remove the class afterwards', function () {
68-
assert.notOk(el.classList.contains('test'))
68+
assert.notOk(el.classList.contains('enter'))
6969
})
7070

7171
it('should return correct code', function () {
@@ -85,7 +85,7 @@ describe('UNIT: Transition', function () {
8585
})
8686

8787
it('should add the class', function () {
88-
assert.ok(el.classList.contains('test'))
88+
assert.ok(el.classList.contains('leave'))
8989
})
9090

9191
it('should call changeState on transitionend', function () {
@@ -102,7 +102,7 @@ describe('UNIT: Transition', function () {
102102
})
103103

104104
it('should remove the class after called', function () {
105-
assert.notOk(el.classList.contains('test'))
105+
assert.notOk(el.classList.contains('leave'))
106106
})
107107

108108
it('should return correct code', function () {
@@ -215,7 +215,7 @@ describe('UNIT: Transition', function () {
215215
function mockEl (type) {
216216
var el = document.createElement('div')
217217
if (type === 'css') {
218-
el.sd_trans_class = 'test'
218+
el.sd_trans_class = ['enter', 'leave']
219219
} else if (type === 'js') {
220220
el.sd_trans = 'test'
221221
}

0 commit comments

Comments
 (0)