UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

48 lines (47 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAddAttributePassingRef = exports.shouldAddAttributePassing = exports.getAttributePassingString = exports.ROOT_REF = void 0; exports.ROOT_REF = '_root'; const getAttributePassingString = (typescript) => { return ('/**\n' + ' * Passes `aria-*`, `data-*` & `class` attributes to correct child. Used in angular and stencil.\n' + ' * @param element the ref for the component\n' + ' * @param customElementSelector the custom element like `my-component`\n' + ' */\n' + `private enableAttributePassing(element${typescript ? ': HTMLElement | null' : ''}, customElementSelector${typescript ? ': string' : ''}) { ` + ' const parent = element?.closest(customElementSelector);\n' + ' if (element && parent) {\n' + ' const attributes = parent.attributes;\n' + ' for (let i = 0; i < attributes.length; i++) {\n' + ' const attr = attributes.item(i);\n' + " if (attr && (attr.name.startsWith('data-') || attr.name.startsWith('aria-'))) {\n" + ' element.setAttribute(attr.name, attr.value);\n' + ' parent.removeAttribute(attr.name);\n' + ' }\n' + " if (attr && attr.name === 'class') {\n" + " const isWebComponent = attr.value.includes('hydrated');\n" + " const value = attr.value.replace('hydrated', '').trim();\n" + " const currentClass = element.getAttribute('class');\n" + " element.setAttribute(attr.name, `${currentClass ? `${currentClass} ` : ''}${value}`);\n" + ' if (isWebComponent) {\n' + ' // Stencil is using this class for lazy loading component\n' + " parent.setAttribute('class', 'hydrated');\n" + ' } else {\n' + ' parent.removeAttribute(attr.name);\n' + ' }\n' + ' }\n' + ' }\n' + ' }\n' + '};'); }; exports.getAttributePassingString = getAttributePassingString; const shouldAddAttributePassing = (json, options) => { var _a, _b, _c; return ((_a = options.attributePassing) === null || _a === void 0 ? void 0 : _a.enabled) || ((_c = (_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.attributePassing) === null || _c === void 0 ? void 0 : _c.enabled); }; exports.shouldAddAttributePassing = shouldAddAttributePassing; const getAddAttributePassingRef = (json, options) => { var _a, _b, _c; return (((_b = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.attributePassing) === null || _b === void 0 ? void 0 : _b.customRef) || ((_c = options.attributePassing) === null || _c === void 0 ? void 0 : _c.customRef) || exports.ROOT_REF); }; exports.getAddAttributePassingRef = getAddAttributePassingRef;