@logo-elements/component-base
Version:
A set of mixins used by Logo Elements which is extended from Vaadin components.
57 lines (51 loc) • 1.61 kB
JavaScript
/**
* @license
* Copyright LOGO YAZILIM SANAYİ VE TİCARET A.Ş.
*
* Save to the extent permitted by law, you may not use, copy, modify,
* distribute or create derivative works of this material or any part
* of it without the prior written consent of LOGO YAZILIM SANAYİ VE TİCARET A.Ş. Limited.
* Any reproduction of this material must contain this notice.
*/
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
/**
* A mixin to provide content for named slots defined by component.
*
* @polymerMixin
*/
export const SlotMixin = dedupingMixin(
(superclass) =>
class SlotMixinClass extends superclass {
/**
* List of named slots to initialize.
* @protected
*/
get slots() {
return {};
}
/** @protected */
ready() {
super.ready();
this._connectSlotMixin();
}
/** @private */
_connectSlotMixin() {
Object.keys(this.slots).forEach((slotName) => {
// Ignore labels of nested components, if any
const hasContent = this._getDirectSlotChild(slotName) !== undefined;
if (!hasContent) {
const slotFactory = this.slots[slotName];
const slotContent = slotFactory();
if (slotContent instanceof Element) {
slotContent.setAttribute('slot', slotName);
this.appendChild(slotContent);
}
}
});
}
/** @protected */
_getDirectSlotChild(slotName) {
return Array.from(this.children).find((el) => el.slot === slotName);
}
}
);