preact-custom-element
Version:
Wrap your component up as a custom element
3 lines (2 loc) • 3.05 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):(t||self).preactCustomElement=e(t.preact)}(this,function(t){function e(){return e=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o])}return t},e.apply(this,arguments)}function n(t,e){if(null==t)return{};var n,o,r={},i=Object.keys(t);for(o=0;o<i.length;o++)e.indexOf(n=i[o])>=0||(r[n]=t[n]);return r}var o=["context","children"],r=["useFragment"];function i(e){this.getChildContext=function(){return e.context};var r=e.children,i=n(e,o);return t.cloneElement(r,i)}function s(n){var o=new CustomEvent("_preact",{detail:{},bubbles:!0,cancelable:!0});this.dispatchEvent(o),this._vdom=t.h(i,e({},this._props,{context:o.detail.context}),d(this,this._vdomComponent,n)),(this.hasAttribute("hydrate")?t.hydrate:t.render)(this._vdom,this._root)}function a(t){return t.replace(/-(\w)/g,function(t,e){return e?e.toUpperCase():""})}function c(e,n,o){if(this._vdom){var r={};r[e]=o=null==o?void 0:o,r[a(e)]=o,this._vdom=t.cloneElement(this._vdom,r),t.render(this._vdom,this._root)}}function l(){t.render(this._vdom=null,this._root)}function u(o,i){var s=this,a=o.useFragment,c=n(o,r);return t.h(a?t.Fragment:"slot",e({},c,{ref:function(t){t?(s.ref=t,s._listener||(s._listener=function(t){t.stopPropagation(),t.detail.context=i},t.addEventListener("_preact",s._listener))):s.ref.removeEventListener("_preact",s._listener)}}))}function d(e,n,o){if(3===e.nodeType)return e.data;if(1!==e.nodeType)return null;var r=[],i={},s=0,c=e.attributes,l=e.childNodes;for(s=c.length;s--;)"slot"!==c[s].name&&(i[c[s].name]=c[s].value,i[a(c[s].name)]=c[s].value);for(s=l.length;s--;){var p=d(l[s],null,o),h=l[s].slot;h?i[h]=t.h(u,{name:h},p):r[s]=p}var f=!(!o||!o.shadow),m=n?t.h(u,{useFragment:!f},r):r;return!f&&n&&(e.innerHTML=""),t.h(n||e.nodeName.toLowerCase(),i,m)}return function(t,e,n,o){function r(){var e=Reflect.construct(HTMLElement,[],r);return e._vdomComponent=t,e._root=o&&o.shadow?e.attachShadow({mode:o.mode||"open"}):e,o&&o.adoptedStyleSheets&&(e._root.adoptedStyleSheets=o.adoptedStyleSheets),e}return(r.prototype=Object.create(HTMLElement.prototype)).constructor=r,r.prototype.connectedCallback=function(){s.call(this,o)},r.prototype.attributeChangedCallback=c,r.prototype.disconnectedCallback=l,n=n||t.observedAttributes||Object.keys(t.propTypes||{}),r.observedAttributes=n,t.formAssociated&&(r.formAssociated=!0),n.forEach(function(t){Object.defineProperty(r.prototype,t,{get:function(){return this._vdom?this._vdom.props[t]:this._props[t]},set:function(e){this._vdom?this.attributeChangedCallback(t,null,e):(this._props||(this._props={}),this._props[t]=e);var n=typeof e;null!=e&&"string"!==n&&"boolean"!==n&&"number"!==n||this.setAttribute(t,e)}})}),customElements.define(e||t.tagName||t.displayName||t.name,r),r}});
//# sourceMappingURL=preact-custom-element.umd.js.map