@webstudio-is/react-sdk 0.182.0 → 0.185.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
@@ -57,7 +57,6 @@ var generateRemixParams = (pathname) => {
57
57
  // src/css/global-rules.ts
58
58
  import { getFontFaces } from "@webstudio-is/fonts";
59
59
  var addGlobalRules = (sheet, { assets, assetBaseUrl }) => {
60
- sheet.addPlaintextRule("html {margin: 0; display: grid; min-height: 100%}");
61
60
  const fontAssets = [];
62
61
  for (const asset of assets.values()) {
63
62
  if (asset.type === "font") {
@@ -76,12 +75,31 @@ import {
76
75
  generateAtomic
77
76
  } from "@webstudio-is/css-engine";
78
77
  import {
78
+ ROOT_INSTANCE_ID,
79
79
  createScope,
80
80
  parseComponentName
81
81
  } from "@webstudio-is/sdk";
82
82
 
83
83
  // src/core-components.ts
84
- import { ListViewIcon, PaintBrushIcon } from "@webstudio-is/icons/svg";
84
+ import {
85
+ ListViewIcon,
86
+ PaintBrushIcon,
87
+ EmbedIcon
88
+ } from "@webstudio-is/icons/svg";
89
+ import { html } from "@webstudio-is/sdk/normalize.css";
90
+ var rootComponent = "ws:root";
91
+ var rootMeta = {
92
+ category: "hidden",
93
+ type: "container",
94
+ label: "Global Root",
95
+ icon: EmbedIcon,
96
+ presetStyle: {
97
+ html
98
+ }
99
+ };
100
+ var rootPropsMeta = {
101
+ props: {}
102
+ };
85
103
  var portalComponent = "Slot";
86
104
  var collectionComponent = "ws:collection";
87
105
  var collectionMeta = {
@@ -175,14 +193,16 @@ var descendantPropsMeta = {
175
193
  initialProps: ["selector"]
176
194
  };
177
195
  var coreMetas = {
196
+ [rootComponent]: rootMeta,
178
197
  [collectionComponent]: collectionMeta,
179
198
  [descendantComponent]: descendantMeta
180
199
  };
181
200
  var corePropsMetas = {
201
+ [rootComponent]: rootPropsMeta,
182
202
  [collectionComponent]: collectionPropsMeta,
183
203
  [descendantComponent]: descendantPropsMeta
184
204
  };
185
- var isCoreComponent = (component) => component === collectionComponent || component === descendantComponent;
205
+ var isCoreComponent = (component) => component === rootComponent || component === collectionComponent || component === descendantComponent;
186
206
 
187
207
  // src/css/css.ts
188
208
  import { kebabCase } from "change-case";
@@ -229,7 +249,8 @@ var generateCss = ({
229
249
  presetClasses.set(component, className);
230
250
  }
231
251
  for (const [tag, styles2] of presetStyle) {
232
- const rule = globalSheet.addNestingRule(`:where(${tag}.${className})`);
252
+ const selector = component === rootComponent ? ":root" : `:where(${tag}.${className})`;
253
+ const rule = globalSheet.addNestingRule(selector);
233
254
  for (const declaration of styles2) {
234
255
  rule.setDeclaration({
235
256
  breakpoint: "presets",
@@ -279,6 +300,11 @@ var generateCss = ({
279
300
  for (const selection of styleSourceSelections.values()) {
280
301
  let { instanceId } = selection;
281
302
  const { values } = selection;
303
+ if (instanceId === ROOT_INSTANCE_ID) {
304
+ const rule2 = sheet.addNestingRule(`:root`);
305
+ rule2.applyMixins(values);
306
+ continue;
307
+ }
282
308
  let descendantSuffix = "";
283
309
  const instance = instances.get(instanceId);
284
310
  if (instance?.component === descendantComponent) {
@@ -306,7 +332,7 @@ var generateCss = ({
306
332
  }
307
333
  if (atomic) {
308
334
  const { cssText } = generateAtomic(sheet, {
309
- getKey: (rule) => instanceByRule.get(rule) ?? "",
335
+ getKey: (rule) => instanceByRule.get(rule),
310
336
  transformValue: imageValueTransformer,
311
337
  classes
312
338
  });
@@ -536,6 +562,28 @@ var showAttribute = "data-ws-show";
536
562
  var indexAttribute = "data-ws-index";
537
563
  var collapsedAttribute = "data-ws-collapsed";
538
564
  var textContentAttribute = "data-ws-text-content";
565
+ var attributeNameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
566
+ var attributeNameChar = attributeNameStartChar + ":\\-0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
567
+ var validAttributeNameRegex = new RegExp(
568
+ // eslint-disable-next-line no-misleading-character-class
569
+ "^[" + attributeNameStartChar + "][" + attributeNameChar + "]*$"
570
+ );
571
+ var illegalAttributeNameCache = /* @__PURE__ */ new Map();
572
+ var validatedAttributeNameCache = /* @__PURE__ */ new Map();
573
+ var isAttributeNameSafe = (attributeName) => {
574
+ if (validatedAttributeNameCache.has(attributeName)) {
575
+ return true;
576
+ }
577
+ if (illegalAttributeNameCache.has(attributeName)) {
578
+ return false;
579
+ }
580
+ if (validAttributeNameRegex.test(attributeName)) {
581
+ validatedAttributeNameCache.set(attributeName, true);
582
+ return true;
583
+ }
584
+ illegalAttributeNameCache.set(attributeName, true);
585
+ return false;
586
+ };
539
587
 
540
588
  // src/prop-meta.ts
541
589
  import { z } from "zod";
@@ -1282,6 +1330,7 @@ var generatePropValue = ({
1282
1330
  prop;
1283
1331
  };
1284
1332
  var generateJsxElement = ({
1333
+ context = "jsx",
1285
1334
  scope,
1286
1335
  instance,
1287
1336
  props,
@@ -1303,7 +1352,8 @@ ${indexAttribute}="${index}"`;
1303
1352
  let conditionValue;
1304
1353
  let collectionDataValue;
1305
1354
  let collectionItemValue;
1306
- const classes = Array.from(classesMap?.get(instance.id) ?? []);
1355
+ const classMapArray = classesMap?.get(instance.id);
1356
+ const classes = classMapArray !== void 0 ? [JSON.stringify(classMapArray.join(" "))] : [];
1307
1357
  for (const prop of props.values()) {
1308
1358
  if (prop.instanceId !== instance.id) {
1309
1359
  continue;
@@ -1314,6 +1364,9 @@ ${indexAttribute}="${index}"`;
1314
1364
  dataSources,
1315
1365
  usedDataSources
1316
1366
  });
1367
+ if (isAttributeNameSafe(prop.name) === false) {
1368
+ continue;
1369
+ }
1317
1370
  if (prop.name === showAttribute) {
1318
1371
  if (propValue === "true") {
1319
1372
  continue;
@@ -1333,10 +1386,8 @@ ${indexAttribute}="${index}"`;
1333
1386
  }
1334
1387
  continue;
1335
1388
  }
1336
- if (prop.name === "className") {
1337
- if (prop.type === "string") {
1338
- classes.push(prop.value);
1339
- }
1389
+ if (prop.name === "className" && propValue !== void 0) {
1390
+ classes.push(propValue);
1340
1391
  continue;
1341
1392
  }
1342
1393
  if (propValue !== void 0) {
@@ -1346,7 +1397,7 @@ ${prop.name}={${propValue}}`;
1346
1397
  }
1347
1398
  if (classes.length !== 0) {
1348
1399
  generatedProps += `
1349
- className=${JSON.stringify(classes.join(" "))}`;
1400
+ className={${classes.join(` + " " + `)}}`;
1350
1401
  }
1351
1402
  let generatedElement = "";
1352
1403
  if (instance.component === collectionComponent) {
@@ -1379,7 +1430,13 @@ className=${JSON.stringify(classes.join(" "))}`;
1379
1430
  }
1380
1431
  if (conditionValue) {
1381
1432
  let conditionalElement = "";
1382
- conditionalElement += `{(${conditionValue}) &&
1433
+ let before = "";
1434
+ let after = "";
1435
+ if (context === "jsx") {
1436
+ before = "{";
1437
+ after = "}";
1438
+ }
1439
+ conditionalElement += `${before}(${conditionValue}) &&
1383
1440
  `;
1384
1441
  if (instance.component === collectionComponent) {
1385
1442
  conditionalElement += "<>\n";
@@ -1388,7 +1445,7 @@ className=${JSON.stringify(classes.join(" "))}`;
1388
1445
  } else {
1389
1446
  conditionalElement += generatedElement;
1390
1447
  }
1391
- conditionalElement += `}
1448
+ conditionalElement += `${after}
1392
1449
  `;
1393
1450
  return conditionalElement;
1394
1451
  }
@@ -1434,6 +1491,7 @@ var generateJsxChildren = ({
1434
1491
  continue;
1435
1492
  }
1436
1493
  generatedChildren += generateJsxElement({
1494
+ context: "jsx",
1437
1495
  scope,
1438
1496
  instance,
1439
1497
  props,
@@ -1476,6 +1534,7 @@ var generateWebstudioComponent = ({
1476
1534
  }
1477
1535
  const usedDataSources = /* @__PURE__ */ new Map();
1478
1536
  const generatedJsx = generateJsxElement({
1537
+ context: "expression",
1479
1538
  scope,
1480
1539
  instance,
1481
1540
  props,
@@ -1572,10 +1631,12 @@ export {
1572
1631
  getIndexesWithinAncestors,
1573
1632
  idAttribute,
1574
1633
  indexAttribute,
1634
+ isAttributeNameSafe,
1575
1635
  isCoreComponent,
1576
1636
  namespaceMeta,
1577
1637
  normalizeProps,
1578
1638
  portalComponent,
1639
+ rootComponent,
1579
1640
  selectorIdAttribute,
1580
1641
  showAttribute,
1581
1642
  stateCategories,
@@ -1,6 +1,7 @@
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, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
3
+ export declare const generateJsxElement: ({ context, scope, instance, props, dataSources, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
4
+ context?: "expression" | "jsx";
4
5
  scope: Scope;
5
6
  instance: Instance;
6
7
  props: Props;