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

Commit 68f6653

Browse files
committed
refactor(accordion): use ngAnimate for animations
- Deprecate collapse module Consider removing it after transition module is deprecated as well.
1 parent 431b9c7 commit 68f6653

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

src/accordion/accordion.js

Lines changed: 62 additions & 2 deletions
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,64 @@ 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+
.css({height: '0'})
143+
.removeClass('collapse')
144+
.addClass('collapsing')
145+
;
146+
}
147+
done();
148+
},
149+
addClass: function (element, className, done) {
150+
if (className == 'in') {
151+
element
152+
.css({height: element[0].scrollHeight + 'px'})
153+
.one('$animate:close', function closeFn() {
154+
element
155+
.removeClass('collapsing')
156+
.css({height: 'auto'});
157+
});
158+
}
159+
done();
160+
},
161+
beforeRemoveClass: function (element, className, done) {
162+
if (className == 'in') {
163+
element
164+
// IMPORTANT: The height must be set before adding "collapsing" class.
165+
// Otherwise, the browser attempts to animate from height 0 (in
166+
// collapsing class) to the given height here.
167+
.css({height: element[0].scrollHeight + 'px'})
168+
// initially all panel collapse have the collapse class, this removal
169+
// prevents the animation from jumping to collapsed state
170+
.removeClass('collapse')
171+
.addClass('collapsing');
172+
}
173+
done();
174+
},
175+
removeClass: function (element, className, done) {
176+
if (className == 'in') {
177+
element
178+
.css({height: '0'})
179+
.one('$animate:close', function closeFn() {
180+
element
181+
.removeClass('collapsing')
182+
.addClass('collapse');
183+
});
184+
}
185+
done();
186+
}
187+
};
188+
})
189+
190+
;

src/accordion/test/accordion.spec.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)