Skip to content

Commit

Permalink
Simplify + clean up class creation
Browse files Browse the repository at this point in the history
Another option here would be to just use ES Classes directly. Babel transpiles them to a syntax that [doesn't work with Custom Elements](WICG/webcomponents#587), which limits options.
  • Loading branch information
developit committed Aug 24, 2018
1 parent 4071750 commit 436992e
Showing 1 changed file with 24 additions and 96 deletions.
120 changes: 24 additions & 96 deletions src/index.js
Expand Up @@ -2,108 +2,36 @@ import { h, render } from 'preact';

const Empty = () => null;

var _createClass = (function() {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ('value' in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function(Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
}
return call && (typeof call === 'object' || typeof call === 'function')
? call
: self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== 'function' && superClass !== null) {
throw new TypeError(
'Super expression must either be null or a function, not ' +
typeof superClass
);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass);
}

export default function register(Component, tagName, propNames) {
let klass = (function(_HTMLElement) {
_inherits(klass, _HTMLElement);

function klass() {
_classCallCheck(this, klass);

var _this = Reflect.construct(HTMLElement, [], klass);
function PreactCustomElement() {
var self = Reflect.construct(HTMLElement, [], PreactCustomElement);
self._vdomComponent = Component;
return self;
}

_this._vdomComponent = Component;
return _this;
PreactCustomElement.prototype = Object.create(HTMLElement.prototype);
Object.setPrototypeOf(PreactCustomElement, HTMLElement);

Object.assign(PreactCustomElement.prototype, {
constructor: PreactCustomElement,
connectedCallback() {
renderElement.apply(this);
},
attributeChangedCallback() {
renderElement.apply(this);
},
detachedCallback() {
unRenderElement.apply(this);
}
});

_createClass(
klass,
[
{
key: 'connectedCallback',
value: function connectedCallback() {
renderElement.apply(this);
}
},
{
key: 'attributeChangedCallback',
value: function attributeChangedCallback() {
renderElement.apply(this);
}
},
{
key: 'detachedCallback',
value: function detachedCallback() {
unRenderElement.apply(this);
}
}
],
[
{
key: 'observedAttributes',
get: function() {
return propNames;
}
}
]
);
Object.defineProperty(PreactCustomElement, 'observedAttributes', {
get: () => propNames
});

return klass;
})(HTMLElement);
return window.customElements.define(
return customElements.define(
tagName || Component.displayName || Component.name,
klass
PreactCustomElement
);
}

Expand Down

0 comments on commit 436992e

Please sign in to comment.