@unlayer/react-elements 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1553,6 +1553,29 @@ function getDisplayName2(element) {
1553
1553
  const type = element.type;
1554
1554
  return type?.displayName || type?.name;
1555
1555
  }
1556
+ var VALID_ROOTS = /* @__PURE__ */ new Set(["Body", "Email", "Page", "Document"]);
1557
+ function unwrapRoot(element) {
1558
+ let current = element;
1559
+ for (let depth = 0; depth < 10; depth++) {
1560
+ const name = getDisplayName2(current);
1561
+ if (name && VALID_ROOTS.has(name)) break;
1562
+ const type = current.type;
1563
+ const isPlainFunctionComponent = typeof type === "function" && !type.prototype?.isReactComponent;
1564
+ if (!isPlainFunctionComponent) break;
1565
+ let produced;
1566
+ try {
1567
+ produced = type({ ...current.props });
1568
+ } catch (cause) {
1569
+ const detail = cause instanceof Error ? cause.message : String(cause);
1570
+ throw new Error(
1571
+ `[Unlayer] renderToJson: could not unwrap <${name || "wrapper"}>. A wrapper must be a plain component that synchronously returns a root (<Email>, <Page>, <Document>, or <Body>) and uses no React hooks. Pass the root element directly \u2014 e.g. renderToJson(<Email>\u2026</Email>). (${detail})`
1572
+ );
1573
+ }
1574
+ if (!React__default.default.isValidElement(produced)) break;
1575
+ current = produced;
1576
+ }
1577
+ return current;
1578
+ }
1556
1579
  function collectChildren2(node) {
1557
1580
  const result = [];
1558
1581
  React__default.default.Children.forEach(node, (child) => {
@@ -1769,9 +1792,9 @@ function renderRowToJson(element) {
1769
1792
  return processRow(element, counters);
1770
1793
  }
1771
1794
  function renderToJson(element) {
1795
+ element = unwrapRoot(element);
1772
1796
  const displayName = getDisplayName2(element);
1773
- const validRoots = /* @__PURE__ */ new Set(["Body", "Email", "Page", "Document"]);
1774
- if (!displayName || !validRoots.has(displayName)) {
1797
+ if (!displayName || !VALID_ROOTS.has(displayName)) {
1775
1798
  throw new Error(
1776
1799
  `[Unlayer] renderToJson: Root element must be <Body>, <Email>, <Page>, or <Document>, but got <${displayName || "unknown"}>. Wrap your content: <Body><Row><Column>...</Column></Row></Body>`
1777
1800
  );