preact-custom-element
Version:
Wrap your component up as a custom element
3 lines (2 loc) • 2.57 kB
JavaScript
import{h as t,hydrate as e,render as n,cloneElement as o}from"preact";function r(){return(r=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}).apply(this,arguments)}var i=["context","children"];function a(t){this.getChildContext=function(){return t.context};var e=t.children,n=function(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}(t,i);return o(e,n)}function s(){var o=new CustomEvent("_preact",{detail:{},bubbles:!0,cancelable:!0});this.dispatchEvent(o),this._vdom=t(a,r({},this._props,{context:o.detail.context}),function e(n,o){if(3===n.nodeType)return n.data;if(1!==n.nodeType)return null;var r=[],i={},a=0,s=n.attributes,l=n.childNodes;for(a=s.length;a--;)"slot"!==s[a].name&&(i[s[a].name]=s[a].value,i[c(s[a].name)]=s[a].value);for(a=l.length;a--;){var u=e(l[a],null),d=l[a].slot;d?i[d]=t(p,{name:d},u):r[a]=u}var h=o?t(p,null,r):r;return t(o||n.nodeName.toLowerCase(),i,h)}(this,this._vdomComponent)),(this.hasAttribute("hydrate")?e:n)(this._vdom,this._root)}function c(t){return t.replace(/-(\w)/g,function(t,e){return e?e.toUpperCase():""})}function l(t,e,r){if(this._vdom){var i={};i[t]=r=null==r?void 0:r,i[c(t)]=r,this._vdom=o(this._vdom,i),n(this._vdom,this._root)}}function u(){n(this._vdom=null,this._root)}function p(e,n){var o=this;return t("slot",r({},e,{ref:function(t){t?(o.ref=t,o._listener||(o._listener=function(t){t.stopPropagation(),t.detail.context=n},t.addEventListener("_preact",o._listener))):o.ref.removeEventListener("_preact",o._listener)}}))}export default 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,e}return(r.prototype=Object.create(HTMLElement.prototype)).constructor=r,r.prototype.connectedCallback=s,r.prototype.attributeChangedCallback=l,r.prototype.disconnectedCallback=u,n=n||t.observedAttributes||Object.keys(t.propTypes||{}),r.observedAttributes=n,n.forEach(function(t){Object.defineProperty(r.prototype,t,{get:function(){return this._vdom.props[t]},set:function(e){this._vdom?this.attributeChangedCallback(t,null,e):(this._props||(this._props={}),this._props[t]=e,this.connectedCallback());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)}
//# sourceMappingURL=preact-custom-element.esm.js.map