3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11
<link rel="import" href="../polymer/polymer.html">
12
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
15
`iron-collapse` creates a collapsible block of content. By default, the content
16
will be collapsed. Use `opened` or `toggle()` to show/hide the content.
18
<button on-click="toggle">toggle collapse</button>
20
<iron-collapse id="collapse">
21
<div>Content goes here...</div>
27
this.$.collapse.toggle();
30
`iron-collapse` adjusts the height/width of the collapsible element to show/hide
31
the content. So avoid putting padding/margin/border on the collapsible directly,
32
and instead put a div inside and style that.
37
border: 1px solid #dedede;
42
<div class="collapse-content">
43
<div>Content goes here...</div>
50
@element iron-collapse
53
<dom-module id="iron-collapse">
59
transition-duration: 300ms;
63
:host(.iron-collapse-closed) {
67
:host(:not(.iron-collapse-opened)) {
88
Polymer.IronResizableBehavior
94
* If true, the orientation is horizontal; otherwise is vertical.
96
* @attribute horizontal
101
observer: '_horizontalChanged'
105
* Set opened to true to show the collapse element and to false to hide it.
113
observer: '_openedChanged'
117
* Set noAnimation to true to disable animations
119
* @attribute noAnimation
128
return this.horizontal ? 'width' : 'height';
133
'aria-hidden': 'true',
134
'aria-expanded': 'false'
138
transitionend: '_transitionEnd'
141
attached: function() {
142
// It will take care of setting correct classes and styles.
143
this._transitionEnd();
147
* Toggle the opened state.
152
this.opened = !this.opened;
163
updateSize: function(size, animated) {
165
if (this.style[this.dimension] === size) {
169
this._updateTransition(false);
170
// If we can animate, must do some prep work.
171
if (animated && !this.noAnimation && this._isDisplayed) {
172
// Animation will start at the current size.
173
var startSize = this._calcSize();
174
// For `auto` we must calculate what is the final size for the animation.
175
// After the transition is done, _transitionEnd will set the size back to `auto`.
176
if (size === 'auto') {
177
this.style[this.dimension] = size;
178
size = this._calcSize();
180
// Go to startSize without animation.
181
this.style[this.dimension] = startSize;
182
// Force layout to ensure transition will go. Set offsetHeight to itself
183
// so that compilers won't remove it.
184
this.offsetHeight = this.offsetHeight;
186
this._updateTransition(true);
188
// Set the final size.
189
this.style[this.dimension] = size;
193
* enableTransition() is deprecated, but left over so it doesn't break existing code.
194
* Please use `noAnimation` property instead.
196
* @method enableTransition
197
* @deprecated since version 1.0.4
199
enableTransition: function(enabled) {
200
console.warn('`enableTransition()` is deprecated, use `noAnimation` instead.');
201
this.noAnimation = !enabled;
204
_updateTransition: function(enabled) {
205
this.style.transitionDuration = (enabled && !this.noAnimation) ? '' : '0s';
208
_horizontalChanged: function() {
209
this.style.transitionProperty = this.dimension;
210
var otherDimension = this.dimension === 'width' ? 'height' : 'width';
211
this.style[otherDimension] = '';
212
this.updateSize(this.opened ? 'auto' : '0px', false);
215
_openedChanged: function() {
216
this.setAttribute('aria-expanded', this.opened);
217
this.setAttribute('aria-hidden', !this.opened);
219
this.toggleClass('iron-collapse-closed', false);
220
this.toggleClass('iron-collapse-opened', false);
221
this.updateSize(this.opened ? 'auto' : '0px', true);
223
// Focus the current collapse.
227
if (this.noAnimation) {
228
this._transitionEnd();
232
_transitionEnd: function() {
234
this.style[this.dimension] = 'auto';
236
this.toggleClass('iron-collapse-closed', !this.opened);
237
this.toggleClass('iron-collapse-opened', this.opened);
238
this._updateTransition(false);
243
* Simplistic heuristic to detect if element has a parent with display: none
248
var rect = this.getBoundingClientRect();
249
for (var prop in rect) {
250
if (rect[prop] !== 0) return true;
255
_calcSize: function() {
256
return this.getBoundingClientRect()[this.dimension] + 'px';