MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / registerComponent

Function registerComponent

www/js/_hyperscript-max.js:11120–11230  ·  view source on GitHub ↗
(templateEl, componentScript)

Source from the content-addressed store, hash-verified

11118 });
11119 }
11120 function registerComponent(templateEl, componentScript) {
11121 const tagName = templateEl.getAttribute("component");
11122 if (!tagName.includes("-")) {
11123 console.error("component name must contain a dash: '" + tagName + "'");
11124 return;
11125 }
11126 var raw = templateEl.textContent;
11127 var combined = "";
11128 var styleRegex = /<style[^>]*>([\s\S]*?)<\/style>/gi;
11129 var match;
11130 while ((match = styleRegex.exec(raw)) !== null) {
11131 combined += match[1] + "\n";
11132 }
11133 if (combined) {
11134 raw = raw.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "");
11135 templateEl.textContent = raw;
11136 var scopedStyle = document.createElement("style");
11137 scopedStyle.setAttribute("data-hyperscript-component", tagName);
11138 scopedStyle.textContent = "@scope (" + tagName + ") {\n" + combined + "}";
11139 document.head.appendChild(scopedStyle);
11140 }
11141 const templateSource = templateEl.textContent;
11142 const ComponentClass = class extends HTMLElement {
11143 connectedCallback() {
11144 if (this._hypercomp_initialized) return;
11145 this._hypercomp_initialized = true;
11146 this.setAttribute("dom-scope", "isolated");
11147 this._slotContent = this.innerHTML;
11148 this.innerHTML = "";
11149 var internalData = runtime2.getInternalData(this);
11150 if (!internalData.elementScope) internalData.elementScope = {};
11151 internalData.elementScope.attrs = createAttrs(this);
11152 if (componentScript) {
11153 this.setAttribute("_", componentScript);
11154 _hyperscript2.process(this);
11155 }
11156 const self2 = this;
11157 var source = substituteSlots(templateSource, self2._slotContent, tagName);
11158 queueMicrotask(function() {
11159 var result = self2._renderTemplate(source);
11160 if (result && result.then) {
11161 result.then(function(html) {
11162 self2._stampTemplate(html);
11163 self2._setupReactiveEffect(source);
11164 });
11165 } else {
11166 self2._stampTemplate(result);
11167 self2._setupReactiveEffect(source);
11168 }
11169 });
11170 }
11171 disconnectedCallback() {
11172 reactivity2.stopElementEffects(this);
11173 runtime2.cleanup(this);
11174 this._hypercomp_initialized = false;
11175 this._hypercomp_stamped = false;
11176 }
11177 _setupReactiveEffect(source) {

Callers 1

registerTemplateFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected