Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 603a4e7

Browse files
committed
refactor(accordion): use ngAnimate for animations
- Deprecate collapse module Consider removing it after transition module is deprecated as well. Fixes #2871 Fixes #3141 Closes #1675
1 parent 431b9c7 commit 603a4e7

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/accordion/accordion.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
1+
angular.module('ui.bootstrap.accordion', [])
22

33
.constant('accordionConfig', {
44
closeOthers: true
@@ -127,4 +127,63 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
127127
});
128128
}
129129
};
130-
});
130+
})
131+
132+
/**
133+
* Animations based on addition and removal of `in` class
134+
* This requires the bootstrap classes to be present in order to take advantage
135+
* of the animation classes.
136+
*/
137+
.animation('.panel-collapse', function () {
138+
return {
139+
beforeAddClass: function (element, className, done) {
140+
if (className == 'in') {
141+
element
142+
.removeClass('collapse')
143+
.addClass('collapsing')
144+
;
145+
}
146+
done();
147+
},
148+
addClass: function (element, className, done) {
149+
if (className == 'in') {
150+
element
151+
.css({height: element[0].scrollHeight + 'px'})
152+
.one('$animate:close', function closeFn() {
153+
element
154+
.removeClass('collapsing')
155+
.css({height: 'auto'});
156+
});
157+
}
158+
done();
159+
},
160+
beforeRemoveClass: function (element, className, done) {
161+
if (className == 'in') {
162+
element
163+
// IMPORTANT: The height must be set before adding "collapsing" class.
164+
// Otherwise, the browser attempts to animate from height 0 (in
165+
// collapsing class) to the given height here.
166+
.css({height: element[0].scrollHeight + 'px'})
167+
// initially all panel collapse have the collapse class, this removal
168+
// prevents the animation from jumping to collapsed state
169+
.removeClass('collapse')
170+
.addClass('collapsing');
171+
}
172+
done();
173+
},
174+
removeClass: function (element, className, done) {
175+
if (className == 'in') {
176+
element
177+
.css({height: '0'})
178+
.one('$animate:close', function closeFn() {
179+
element
180+
.removeClass('collapsing')
181+
.addClass('collapse');
182+
});
183+
}
184+
done();
185+
}
186+
};
187+
})
188+
189+
;

src/accordion/test/accordion.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ describe('accordion', function () {
265265
});
266266

267267
it('should have visible panel body when the group with isOpen set to true', function () {
268-
expect(findGroupBody(0)[0].clientHeight).not.toBe(0);
269-
expect(findGroupBody(1)[0].clientHeight).toBe(0);
268+
expect(findGroupBody(0)).toHaveClass('in');
269+
expect(findGroupBody(1)).not.toHaveClass('in');
270270
});
271271
});
272272

src/collapse/collapse.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @deprecated Switching over to using ngAnimate for animations
3+
*/
14
angular.module('ui.bootstrap.collapse', ['ui.bootstrap.transition'])
25

36
.directive('collapse', ['$transition', function ($transition) {

template/accordion/accordion-group.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ <h4 class="panel-title">
44
<a href="javascript:void(0)" tabindex="0" class="accordion-toggle" ng-click="toggleOpen()" accordion-transclude="heading"><span ng-class="{'text-muted': isDisabled}">{{heading}}</span></a>
55
</h4>
66
</div>
7-
<div class="panel-collapse" collapse="!isOpen">
7+
<div class="panel-collapse collapse" ng-class="{in: isOpen}">
88
<div class="panel-body" ng-transclude></div>
99
</div>
1010
</div>

0 commit comments

Comments
 (0)