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">
14 `iron-collapse` creates a collapsible block of content. By default, the content
15 will be collapsed. Use `opened` or `toggle()` to show/hide the content.
17 <button on-click="{{toggle}}">toggle collapse</button>
19 <iron-collapse id="collapse">
20 <div>Content goes here...</div>
26 this.$.collapse.toggle();
29 `iron-collapse` adjusts the height/width of the collapsible element to show/hide
30 the content. So avoid putting padding/margin/border on the collapsible directly,
31 and instead put a div inside and style that.
36 border: 1px solid #dedede;
41 <div class="collapse-content">
42 <div>Content goes here...</div>
49 @element iron-collapse
52 <dom-module id=
"iron-collapse">
58 transition-duration:
300ms;
61 :host(.iron-collapse-closed) {
65 :host(:not(.iron-collapse-opened)) {
88 * If true, the orientation is horizontal; otherwise is vertical.
90 * @attribute horizontal
95 observer
: '_horizontalChanged'
99 * Set opened to true to show the collapse element and to false to hide it.
107 observer
: '_openedChanged'
114 'aria-expanded': 'false'
118 transitionend
: '_transitionEnd'
122 // Avoid transition at the beginning e.g. page loads and enable
123 // transitions only after the element is rendered and ready.
124 this._enableTransition
= true;
128 * Toggle the opened state.
133 this.opened
= !this.opened
;
137 this.toggleClass('iron-collapse-closed', false);
138 this.updateSize('auto', false);
139 var s
= this._calcSize();
140 this.updateSize('0px', false);
141 // force layout to ensure transition will go
143 this.updateSize(s
, true);
147 this.toggleClass('iron-collapse-opened', false);
148 this.updateSize(this._calcSize(), false);
149 // force layout to ensure transition will go
151 this.updateSize('0px', true);
154 updateSize: function(size
, animated
) {
155 this.enableTransition(animated
);
157 var nochange
= s
[this.dimension
] === size
;
158 s
[this.dimension
] = size
;
159 if (animated
&& nochange
) {
160 this._transitionEnd();
164 enableTransition: function(enabled
) {
165 this.style
.transitionDuration
= (enabled
&& this._enableTransition
) ? '' : '0s';
168 _horizontalChanged: function() {
169 this.dimension
= this.horizontal
? 'width' : 'height';
170 this.style
.transitionProperty
= this.dimension
;
173 _openedChanged: function() {
174 this[this.opened
? 'show' : 'hide']();
175 this.setAttribute('aria-expanded', this.opened
? 'true' : 'false');
179 _transitionEnd: function() {
181 this.updateSize('auto', false);
183 this.toggleClass('iron-collapse-closed', !this.opened
);
184 this.toggleClass('iron-collapse-opened', this.opened
);
185 this.enableTransition(false);
188 _calcSize: function() {
189 return this.getBoundingClientRect()[this.dimension
] + 'px';