@webstudio-is/react-sdk 0.133.0 → 0.135.0

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/lib/index.js CHANGED
@@ -649,6 +649,7 @@ import { StyleValue } from "@webstudio-is/css-engine";
649
649
  import jsep from "jsep";
650
650
  import jsepAssignment from "@jsep-plugin/assignment";
651
651
  import jsepObject from "@jsep-plugin/object";
652
+ jsep.literals["undefined"] = "undefined";
652
653
  jsep.plugins.register(jsepAssignment);
653
654
  jsep.plugins.register(jsepObject);
654
655
  var generateCode = (node, failOnForbidden, options) => {
@@ -1191,7 +1192,8 @@ var componentCategories = [
1191
1192
  "media",
1192
1193
  "forms",
1193
1194
  "radix",
1194
- "hidden"
1195
+ "hidden",
1196
+ "utilities"
1195
1197
  ];
1196
1198
  var stateCategories = ["states", "component-states"];
1197
1199
  var ComponentState = z3.object({
@@ -1337,7 +1339,8 @@ import { parseComponentName } from "@webstudio-is/sdk";
1337
1339
  var generateAction = ({
1338
1340
  scope,
1339
1341
  prop,
1340
- dataSources
1342
+ dataSources,
1343
+ usedDataSources
1341
1344
  }) => {
1342
1345
  const setters = /* @__PURE__ */ new Set();
1343
1346
  let args = [];
@@ -1354,6 +1357,7 @@ var generateAction = ({
1354
1357
  const depId = decodeDataSourceVariable(identifier);
1355
1358
  const dep = depId ? dataSources.get(depId) : void 0;
1356
1359
  if (dep) {
1360
+ usedDataSources.set(dep.id, dep);
1357
1361
  if (assignee) {
1358
1362
  setters.add(dep);
1359
1363
  }
@@ -1389,7 +1393,8 @@ var generateAction = ({
1389
1393
  var generatePropValue = ({
1390
1394
  scope,
1391
1395
  prop,
1392
- dataSources
1396
+ dataSources,
1397
+ usedDataSources
1393
1398
  }) => {
1394
1399
  if (prop.type === "asset" || prop.type === "page") {
1395
1400
  return;
@@ -1402,17 +1407,19 @@ var generatePropValue = ({
1402
1407
  if (dataSource === void 0) {
1403
1408
  return;
1404
1409
  }
1410
+ usedDataSources.set(dataSource.id, dataSource);
1405
1411
  return scope.getName(dataSource.id, dataSource.name);
1406
1412
  }
1407
1413
  if (prop.type === "expression") {
1408
1414
  return generateExpression({
1409
1415
  expression: prop.value,
1410
1416
  dataSources,
1417
+ usedDataSources,
1411
1418
  scope
1412
1419
  });
1413
1420
  }
1414
1421
  if (prop.type === "action") {
1415
- return generateAction({ scope, prop, dataSources });
1422
+ return generateAction({ scope, prop, dataSources, usedDataSources });
1416
1423
  }
1417
1424
  prop;
1418
1425
  };
@@ -1421,6 +1428,7 @@ var generateJsxElement = ({
1421
1428
  instance,
1422
1429
  props,
1423
1430
  dataSources,
1431
+ usedDataSources,
1424
1432
  indexesWithinAncestors,
1425
1433
  children,
1426
1434
  classesMap
@@ -1445,7 +1453,12 @@ ${indexAttribute}="${index}"`;
1445
1453
  if (prop.instanceId !== instance.id) {
1446
1454
  continue;
1447
1455
  }
1448
- const propValue = generatePropValue({ scope, prop, dataSources });
1456
+ const propValue = generatePropValue({
1457
+ scope,
1458
+ prop,
1459
+ dataSources,
1460
+ usedDataSources
1461
+ });
1449
1462
  if (prop.name === showAttribute) {
1450
1463
  if (propValue === "true") {
1451
1464
  continue;
@@ -1525,6 +1538,7 @@ var generateJsxChildren = ({
1525
1538
  instances,
1526
1539
  props,
1527
1540
  dataSources,
1541
+ usedDataSources,
1528
1542
  indexesWithinAncestors,
1529
1543
  classesMap
1530
1544
  }) => {
@@ -1540,6 +1554,7 @@ var generateJsxChildren = ({
1540
1554
  const expression = generateExpression({
1541
1555
  expression: child.value,
1542
1556
  dataSources,
1557
+ usedDataSources,
1543
1558
  scope
1544
1559
  });
1545
1560
  generatedChildren = `{${expression}}
@@ -1557,6 +1572,7 @@ var generateJsxChildren = ({
1557
1572
  instance,
1558
1573
  props,
1559
1574
  dataSources,
1575
+ usedDataSources,
1560
1576
  indexesWithinAncestors,
1561
1577
  classesMap,
1562
1578
  children: generateJsxChildren({
@@ -1566,6 +1582,7 @@ var generateJsxChildren = ({
1566
1582
  instances,
1567
1583
  props,
1568
1584
  dataSources,
1585
+ usedDataSources,
1569
1586
  indexesWithinAncestors
1570
1587
  })
1571
1588
  });
@@ -1590,17 +1607,36 @@ var generateWebstudioComponent = ({
1590
1607
  if (instance === void 0) {
1591
1608
  return "";
1592
1609
  }
1610
+ const usedDataSources = /* @__PURE__ */ new Map();
1611
+ const generatedJsx = generateJsxElement({
1612
+ scope,
1613
+ instance,
1614
+ props,
1615
+ dataSources,
1616
+ usedDataSources,
1617
+ indexesWithinAncestors,
1618
+ classesMap,
1619
+ children: generateJsxChildren({
1620
+ scope,
1621
+ children: instance.children,
1622
+ instances,
1623
+ props,
1624
+ dataSources,
1625
+ usedDataSources,
1626
+ indexesWithinAncestors,
1627
+ classesMap
1628
+ })
1629
+ });
1593
1630
  let generatedProps = "";
1594
1631
  if (parameters.length > 0) {
1595
1632
  let generatedPropsValue = "{ ";
1596
1633
  let generatedPropsType = "{ ";
1597
1634
  for (const parameter of parameters) {
1598
- const dataSource = dataSources.get(parameter.value);
1599
- if (dataSource === void 0) {
1600
- continue;
1635
+ const dataSource = usedDataSources.get(parameter.value);
1636
+ if (dataSource) {
1637
+ const valueName = scope.getName(dataSource.id, dataSource.name);
1638
+ generatedPropsValue += `${parameter.name}: ${valueName}, `;
1601
1639
  }
1602
- const valueName = scope.getName(dataSource.id, dataSource.name);
1603
- generatedPropsValue += `${parameter.name}: ${valueName}, `;
1604
1640
  generatedPropsType += `${parameter.name}: any; `;
1605
1641
  }
1606
1642
  generatedPropsValue += `}`;
@@ -1608,7 +1644,7 @@ var generateWebstudioComponent = ({
1608
1644
  generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
1609
1645
  }
1610
1646
  let generatedDataSources = "";
1611
- for (const dataSource of dataSources.values()) {
1647
+ for (const dataSource of usedDataSources.values()) {
1612
1648
  if (dataSource.type === "variable") {
1613
1649
  const valueName = scope.getName(dataSource.id, dataSource.name);
1614
1650
  const setterName = scope.getName(
@@ -1631,23 +1667,6 @@ var generateWebstudioComponent = ({
1631
1667
  `;
1632
1668
  }
1633
1669
  }
1634
- const generatedJsx = generateJsxElement({
1635
- scope,
1636
- instance,
1637
- props,
1638
- dataSources,
1639
- indexesWithinAncestors,
1640
- classesMap,
1641
- children: generateJsxChildren({
1642
- scope,
1643
- children: instance.children,
1644
- instances,
1645
- props,
1646
- dataSources,
1647
- indexesWithinAncestors,
1648
- classesMap
1649
- })
1650
- });
1651
1670
  let generatedComponent = "";
1652
1671
  generatedComponent += `const ${name} = (${generatedProps}) => {
1653
1672
  `;
@@ -1665,25 +1684,11 @@ var generateResourcesLoader = ({
1665
1684
  dataSources,
1666
1685
  resources
1667
1686
  }) => {
1668
- let generatedVariables = "";
1669
1687
  let generatedOutput = "";
1670
1688
  let generatedLoaders = "";
1671
1689
  let hasResources = false;
1690
+ const usedDataSources = /* @__PURE__ */ new Map();
1672
1691
  for (const dataSource of dataSources.values()) {
1673
- if (dataSource.type === "variable") {
1674
- const name = scope.getName(dataSource.id, dataSource.name);
1675
- const value = JSON.stringify(dataSource.value.value);
1676
- generatedVariables += `let ${name} = ${value}
1677
- `;
1678
- }
1679
- if (dataSource.type === "parameter") {
1680
- if (dataSource.id !== page.pathVariableId) {
1681
- continue;
1682
- }
1683
- const name = scope.getName(dataSource.id, dataSource.name);
1684
- generatedVariables += `const ${name} = _props.params
1685
- `;
1686
- }
1687
1692
  if (dataSource.type === "resource") {
1688
1693
  const resource = resources.get(dataSource.resourceId);
1689
1694
  if (resource === void 0) {
@@ -1702,6 +1707,7 @@ var generateResourcesLoader = ({
1702
1707
  const url = generateExpression({
1703
1708
  expression: resource.url,
1704
1709
  dataSources,
1710
+ usedDataSources,
1705
1711
  scope
1706
1712
  });
1707
1713
  generatedLoaders += `url: ${url},
@@ -1714,6 +1720,7 @@ var generateResourcesLoader = ({
1714
1720
  const value = generateExpression({
1715
1721
  expression: header.value,
1716
1722
  dataSources,
1723
+ usedDataSources,
1717
1724
  scope
1718
1725
  });
1719
1726
  generatedLoaders += `{ name: "${header.name}", value: ${value} },
@@ -1725,12 +1732,30 @@ var generateResourcesLoader = ({
1725
1732
  const body = generateExpression({
1726
1733
  expression: resource.body,
1727
1734
  dataSources,
1735
+ usedDataSources,
1728
1736
  scope
1729
1737
  });
1730
1738
  generatedLoaders += `body: ${body},
1731
1739
  `;
1732
1740
  }
1733
1741
  generatedLoaders += `}),
1742
+ `;
1743
+ }
1744
+ }
1745
+ let generatedVariables = "";
1746
+ for (const dataSource of usedDataSources.values()) {
1747
+ if (dataSource.type === "variable") {
1748
+ const name = scope.getName(dataSource.id, dataSource.name);
1749
+ const value = JSON.stringify(dataSource.value.value);
1750
+ generatedVariables += `let ${name} = ${value}
1751
+ `;
1752
+ }
1753
+ if (dataSource.type === "parameter") {
1754
+ if (dataSource.id !== page.pathParamsDataSourceId) {
1755
+ continue;
1756
+ }
1757
+ const name = scope.getName(dataSource.id, dataSource.name);
1758
+ generatedVariables += `const ${name} = _props.params
1734
1759
  `;
1735
1760
  }
1736
1761
  }
@@ -1743,8 +1768,8 @@ var generateResourcesLoader = ({
1743
1768
  `;
1744
1769
  generated += `export const loadResources = async (_props: { params: Params }) => {
1745
1770
  `;
1771
+ generated += generatedVariables;
1746
1772
  if (hasResources) {
1747
- generated += generatedVariables;
1748
1773
  generated += `const [
1749
1774
  `;
1750
1775
  generated += generatedOutput;
@@ -1862,7 +1887,7 @@ var generatePageMeta = ({
1862
1887
  continue;
1863
1888
  }
1864
1889
  if (dataSource.type === "parameter") {
1865
- if (dataSource.id === page.pathVariableId) {
1890
+ if (dataSource.id === page.pathParamsDataSourceId) {
1866
1891
  const valueName = localScope.getName(dataSource.id, dataSource.name);
1867
1892
  generated += ` let ${valueName} = params
1868
1893
  `;
@@ -1,6 +1,6 @@
1
1
  import type { Instances, Instance, Props, Scope, DataSources, Prop } from "@webstudio-is/sdk";
2
2
  import type { IndexesWithinAncestors } from "./instance-utils";
3
- export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, classesMap, }: {
3
+ export declare const generateJsxElement: ({ scope, instance, props, dataSources, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
4
4
  scope: Scope;
5
5
  instance: Instance;
6
6
  props: Map<string, {
@@ -117,11 +117,44 @@ export declare const generateJsxElement: ({ scope, instance, props, dataSources,
117
117
  resourceId: string;
118
118
  scopeInstanceId?: string | undefined;
119
119
  }>;
120
+ usedDataSources: Map<string, {
121
+ value: {
122
+ value: number;
123
+ type: "number";
124
+ } | {
125
+ value: string;
126
+ type: "string";
127
+ } | {
128
+ value: boolean;
129
+ type: "boolean";
130
+ } | {
131
+ value: string[];
132
+ type: "string[]";
133
+ } | {
134
+ type: "json";
135
+ value?: unknown;
136
+ };
137
+ type: "variable";
138
+ name: string;
139
+ id: string;
140
+ scopeInstanceId?: string | undefined;
141
+ } | {
142
+ type: "parameter";
143
+ name: string;
144
+ id: string;
145
+ scopeInstanceId?: string | undefined;
146
+ } | {
147
+ type: "resource";
148
+ name: string;
149
+ id: string;
150
+ resourceId: string;
151
+ scopeInstanceId?: string | undefined;
152
+ }>;
120
153
  indexesWithinAncestors: IndexesWithinAncestors;
121
154
  children: string;
122
155
  classesMap?: Map<string, string[]> | undefined;
123
156
  }) => string;
124
- export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
157
+ export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, usedDataSources, indexesWithinAncestors, classesMap, }: {
125
158
  scope: Scope;
126
159
  children: Instance["children"];
127
160
  instances: Map<string, {
@@ -254,6 +287,39 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
254
287
  resourceId: string;
255
288
  scopeInstanceId?: string | undefined;
256
289
  }>;
290
+ usedDataSources: Map<string, {
291
+ value: {
292
+ value: number;
293
+ type: "number";
294
+ } | {
295
+ value: string;
296
+ type: "string";
297
+ } | {
298
+ value: boolean;
299
+ type: "boolean";
300
+ } | {
301
+ value: string[];
302
+ type: "string[]";
303
+ } | {
304
+ type: "json";
305
+ value?: unknown;
306
+ };
307
+ type: "variable";
308
+ name: string;
309
+ id: string;
310
+ scopeInstanceId?: string | undefined;
311
+ } | {
312
+ type: "parameter";
313
+ name: string;
314
+ id: string;
315
+ scopeInstanceId?: string | undefined;
316
+ } | {
317
+ type: "resource";
318
+ name: string;
319
+ id: string;
320
+ resourceId: string;
321
+ scopeInstanceId?: string | undefined;
322
+ }>;
257
323
  indexesWithinAncestors: IndexesWithinAncestors;
258
324
  classesMap?: Map<string, string[]> | undefined;
259
325
  }) => string;
@@ -688,7 +688,7 @@ declare const WsComponentPropsMeta: z.ZodObject<{
688
688
  initialProps?: string[] | undefined;
689
689
  }>;
690
690
  export type WsComponentPropsMeta = z.infer<typeof WsComponentPropsMeta>;
691
- export declare const componentCategories: readonly ["general", "text", "data", "media", "forms", "radix", "hidden"];
691
+ export declare const componentCategories: readonly ["general", "text", "data", "media", "forms", "radix", "hidden", "utilities"];
692
692
  export declare const stateCategories: readonly ["states", "component-states"];
693
693
  export declare const ComponentState: z.ZodObject<{
694
694
  category: z.ZodOptional<z.ZodEnum<["states", "component-states"]>>;
@@ -706,7 +706,7 @@ export declare const ComponentState: z.ZodObject<{
706
706
  export type ComponentState = z.infer<typeof ComponentState>;
707
707
  export declare const defaultStates: ComponentState[];
708
708
  export declare const WsComponentMeta: z.ZodObject<{
709
- category: z.ZodOptional<z.ZodEnum<["general", "text", "data", "media", "forms", "radix", "hidden"]>>;
709
+ category: z.ZodOptional<z.ZodEnum<["general", "text", "data", "media", "forms", "radix", "hidden", "utilities"]>>;
710
710
  type: z.ZodEnum<["container", "control", "embed", "rich-text-child"]>;
711
711
  requiredAncestors: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
712
712
  invalidAncestors: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -2035,7 +2035,7 @@ export declare const WsComponentMeta: z.ZodObject<{
2035
2035
  }, "strip", z.ZodTypeAny, {
2036
2036
  type: "embed" | "control" | "container" | "rich-text-child";
2037
2037
  icon: string;
2038
- category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | undefined;
2038
+ category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | "utilities" | undefined;
2039
2039
  requiredAncestors?: string[] | undefined;
2040
2040
  invalidAncestors?: string[] | undefined;
2041
2041
  indexWithinAncestor?: string | undefined;
@@ -2488,7 +2488,7 @@ export declare const WsComponentMeta: z.ZodObject<{
2488
2488
  }, {
2489
2489
  type: "embed" | "control" | "container" | "rich-text-child";
2490
2490
  icon: string;
2491
- category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | undefined;
2491
+ category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | "utilities" | undefined;
2492
2492
  requiredAncestors?: string[] | undefined;
2493
2493
  invalidAncestors?: string[] | undefined;
2494
2494
  indexWithinAncestor?: string | undefined;
@@ -1,7 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { componentAttribute, idAttribute } from "../props";
3
- export type AnyComponent = React.ForwardRefExoticComponent<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, "ref"> & {
3
+ export type AnyComponent = React.ForwardRefExoticComponent<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, "ref"> & WebstudioComponentSystemProps & React.RefAttributes<HTMLElement>>;
4
+ export type Components = Map<string, AnyComponent>;
5
+ export type WebstudioComponentSystemProps = {
4
6
  [componentAttribute]: string;
5
7
  [idAttribute]: string;
6
- } & React.RefAttributes<HTMLElement>>;
7
- export type Components = Map<string, AnyComponent>;
8
+ };
@@ -2250,7 +2250,7 @@ export declare const namespaceMeta: (meta: WsComponentMeta, namespace: string, c
2250
2250
  value: string;
2251
2251
  type: "expression";
2252
2252
  } | EmbedTemplateInstance)[] | undefined;
2253
- category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | undefined;
2253
+ category?: "data" | "text" | "hidden" | "media" | "general" | "forms" | "radix" | "utilities" | undefined;
2254
2254
  requiredAncestors?: string[] | undefined;
2255
2255
  invalidAncestors?: string[] | undefined;
2256
2256
  indexWithinAncestor?: string | undefined;
@@ -49,7 +49,7 @@ export declare const generateExpression: ({ expression, dataSources, usedDataSou
49
49
  resourceId: string;
50
50
  scopeInstanceId?: string | undefined;
51
51
  }>;
52
- usedDataSources?: Map<string, {
52
+ usedDataSources: Map<string, {
53
53
  value: {
54
54
  value: number;
55
55
  type: "number";
@@ -81,7 +81,7 @@ export declare const generateExpression: ({ expression, dataSources, usedDataSou
81
81
  id: string;
82
82
  resourceId: string;
83
83
  scopeInstanceId?: string | undefined;
84
- }> | undefined;
84
+ }>;
85
85
  scope: Scope;
86
86
  }) => string;
87
87
  export declare const executeExpression: (expression: undefined | string) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.133.0",
3
+ "version": "0.135.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -16,8 +16,8 @@
16
16
  "type-fest": "^4.3.1",
17
17
  "typescript": "5.2.2",
18
18
  "zod": "^3.21.4",
19
- "@webstudio-is/jest-config": "1.0.7",
20
- "@webstudio-is/tsconfig": "1.0.7"
19
+ "@webstudio-is/tsconfig": "1.0.7",
20
+ "@webstudio-is/jest-config": "1.0.7"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "@remix-run/react": "^1.19.1",
@@ -33,11 +33,11 @@
33
33
  "jsep": "^1.3.8",
34
34
  "nanoid": "^5.0.1",
35
35
  "title-case": "^4.1.0",
36
- "@webstudio-is/css-engine": "0.133.0",
37
- "@webstudio-is/fonts": "0.133.0",
38
- "@webstudio-is/icons": "^0.133.0",
39
- "@webstudio-is/image": "0.133.0",
40
- "@webstudio-is/sdk": "0.133.0"
36
+ "@webstudio-is/fonts": "0.135.0",
37
+ "@webstudio-is/css-engine": "0.135.0",
38
+ "@webstudio-is/icons": "^0.135.0",
39
+ "@webstudio-is/image": "0.135.0",
40
+ "@webstudio-is/sdk": "0.135.0"
41
41
  },
42
42
  "exports": {
43
43
  ".": {