@postnord/web-components
Version:
PostNord Web Components
72 lines (68 loc) • 2.56 kB
JavaScript
/*!
* Built with Stencil
* By PostNord.
*/
'use strict';
var index = require('./index-DVv2io0H.js');
var index$1 = require('./index.cjs.js');
const animateHeightFactory = Base => {
const A = class extends Base {
constructor(hostRef) {
super();
index.registerInstance(this, hostRef);
}
animation;
animationDurationDefault = 400;
animationDuration = this.animationDurationDefault;
isClosing = false;
isExpanding = false;
animateDropdown(element, open, startHeight, endHeight) {
this.cancelAnimations();
if (index$1.reduceMotion())
this.animationDuration = 0;
else
this.animationDuration = this.animationDurationDefault;
element.dataset.moving = '';
this.animation = element.animate({
height: [startHeight, endHeight],
}, {
duration: this.animationDuration,
easing: 'cubic-bezier(0.6, 0, 0.2, 1)',
});
this.animation.onfinish = () => this.animationFinish(element);
this.animation.oncancel = () => (open ? (this.isExpanding = false) : (this.isClosing = false));
}
openDropdown(element) {
const { clientHeight, scrollHeight } = element;
const height = this.isClosing ? clientHeight : 0;
element.style.height = `${scrollHeight}px`;
this.isExpanding = true;
this.animateDropdown(element, true, `${height}px`, `${element.scrollHeight}px`);
}
closeDropdown(element) {
const { scrollHeight, clientHeight } = element;
const height = this.isExpanding ? clientHeight : scrollHeight;
element.style.height = `0px`;
this.isClosing = true;
this.animateDropdown(element, false, `${height}px`, `0px`);
}
animationFinish(element) {
this.cancelAnimations();
element.style.height = this.isClosing ? '0px' : '';
delete element.dataset.moving;
this.isClosing = false;
this.isExpanding = false;
}
cancelAnimations() {
if (this.animation)
this.animation.cancel();
}
isMoving() {
return this.isClosing || this.isExpanding;
}
};
return A;
};
exports.animateHeightFactory = animateHeightFactory;
//# sourceMappingURL=index-BKbe1-IX.js.map
//# sourceMappingURL=index-BKbe1-IX.js.map