@webstudio-is/react-sdk 0.95.0 → 0.96.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.
Files changed (40) hide show
  1. package/lib/css/normalize.js +127 -53
  2. package/lib/index.js +1843 -32
  3. package/lib/types/index.d.ts +0 -1
  4. package/package.json +6 -7
  5. package/lib/app/index.js +0 -2
  6. package/lib/app/root.js +0 -18
  7. package/lib/component-renderer.js +0 -130
  8. package/lib/components/component-meta.js +0 -62
  9. package/lib/components/components-utils.js +0 -2
  10. package/lib/context.js +0 -21
  11. package/lib/css/css.js +0 -59
  12. package/lib/css/global-rules.js +0 -15
  13. package/lib/css/index.js +0 -4
  14. package/lib/css/normalize-type-check.js +0 -4
  15. package/lib/css/presets.js +0 -25
  16. package/lib/css/style-rules.js +0 -63
  17. package/lib/css/style-rules.test.js +0 -149
  18. package/lib/embed-template.js +0 -341
  19. package/lib/embed-template.test.js +0 -648
  20. package/lib/expression.js +0 -330
  21. package/lib/expression.test.js +0 -281
  22. package/lib/generator.js +0 -112
  23. package/lib/generator.test.js +0 -166
  24. package/lib/hook.js +0 -12
  25. package/lib/hook.test.js +0 -15
  26. package/lib/instance-utils.js +0 -43
  27. package/lib/instance-utils.test.js +0 -65
  28. package/lib/prop-meta.js +0 -150
  29. package/lib/props.js +0 -176
  30. package/lib/props.test.js +0 -159
  31. package/lib/pubsub/create.js +0 -56
  32. package/lib/pubsub/index.js +0 -2
  33. package/lib/pubsub/raf-queue.js +0 -20
  34. package/lib/tree/create-elements-tree.js +0 -134
  35. package/lib/tree/index.js +0 -4
  36. package/lib/tree/root.js +0 -85
  37. package/lib/tree/webstudio-component.js +0 -61
  38. package/lib/types/pubsub/create.d.ts +0 -28
  39. package/lib/types/pubsub/index.d.ts +0 -1
  40. package/lib/types/pubsub/raf-queue.d.ts +0 -1
@@ -1,648 +0,0 @@
1
- "use strict";
2
- import { expect, test } from "@jest/globals";
3
- import { generateDataFromEmbedTemplate, namespaceMeta } from "./embed-template";
4
- import { showAttribute } from "./tree";
5
- const expectString = expect.any(String);
6
- const defaultBreakpointId = "base";
7
- test("generate data for embedding from instances and text", () => {
8
- expect(
9
- generateDataFromEmbedTemplate(
10
- [
11
- { type: "text", value: "hello" },
12
- {
13
- type: "instance",
14
- component: "Box1",
15
- children: [
16
- { type: "instance", component: "Box2", children: [] },
17
- { type: "text", value: "world" }
18
- ]
19
- }
20
- ],
21
- /* @__PURE__ */ new Map(),
22
- defaultBreakpointId
23
- )
24
- ).toEqual({
25
- children: [
26
- { type: "text", value: "hello" },
27
- { type: "id", value: expectString }
28
- ],
29
- instances: [
30
- {
31
- type: "instance",
32
- id: expectString,
33
- component: "Box1",
34
- children: [
35
- { type: "id", value: expectString },
36
- { type: "text", value: "world" }
37
- ]
38
- },
39
- {
40
- type: "instance",
41
- id: expectString,
42
- component: "Box2",
43
- children: []
44
- }
45
- ],
46
- props: [],
47
- dataSources: [],
48
- styleSourceSelections: [],
49
- styleSources: [],
50
- styles: []
51
- });
52
- });
53
- test("generate data for embedding from props", () => {
54
- expect(
55
- generateDataFromEmbedTemplate(
56
- [
57
- {
58
- type: "instance",
59
- component: "Box1",
60
- props: [
61
- { type: "string", name: "data-prop1", value: "value1" },
62
- { type: "string", name: "data-prop2", value: "value2" }
63
- ],
64
- children: [
65
- {
66
- type: "instance",
67
- component: "Box2",
68
- props: [{ type: "string", name: "data-prop3", value: "value3" }],
69
- children: []
70
- }
71
- ]
72
- }
73
- ],
74
- /* @__PURE__ */ new Map(),
75
- defaultBreakpointId
76
- )
77
- ).toEqual({
78
- children: [{ type: "id", value: expectString }],
79
- instances: [
80
- {
81
- type: "instance",
82
- id: expectString,
83
- component: "Box1",
84
- children: [{ type: "id", value: expectString }]
85
- },
86
- {
87
- type: "instance",
88
- id: expectString,
89
- component: "Box2",
90
- children: []
91
- }
92
- ],
93
- props: [
94
- {
95
- type: "string",
96
- id: expectString,
97
- instanceId: expectString,
98
- name: "data-prop1",
99
- value: "value1"
100
- },
101
- {
102
- type: "string",
103
- id: expectString,
104
- instanceId: expectString,
105
- name: "data-prop2",
106
- value: "value2"
107
- },
108
- {
109
- type: "string",
110
- id: expectString,
111
- instanceId: expectString,
112
- name: "data-prop3",
113
- value: "value3"
114
- }
115
- ],
116
- dataSources: [],
117
- styleSourceSelections: [],
118
- styleSources: [],
119
- styles: []
120
- });
121
- });
122
- test("generate data for embedding from styles", () => {
123
- expect(
124
- generateDataFromEmbedTemplate(
125
- [
126
- {
127
- type: "instance",
128
- component: "Box1",
129
- styles: [
130
- { property: "width", value: { type: "keyword", value: "auto" } },
131
- { property: "height", value: { type: "keyword", value: "auto" } }
132
- ],
133
- children: [
134
- {
135
- type: "instance",
136
- component: "Box2",
137
- styles: [
138
- {
139
- property: "color",
140
- value: { type: "keyword", value: "black" }
141
- }
142
- ],
143
- children: []
144
- }
145
- ]
146
- }
147
- ],
148
- /* @__PURE__ */ new Map(),
149
- defaultBreakpointId
150
- )
151
- ).toEqual({
152
- children: [{ type: "id", value: expectString }],
153
- instances: [
154
- {
155
- type: "instance",
156
- id: expectString,
157
- component: "Box1",
158
- children: [{ type: "id", value: expectString }]
159
- },
160
- {
161
- type: "instance",
162
- id: expectString,
163
- component: "Box2",
164
- children: []
165
- }
166
- ],
167
- props: [],
168
- dataSources: [],
169
- styleSourceSelections: [
170
- {
171
- instanceId: expectString,
172
- values: [expectString]
173
- },
174
- {
175
- instanceId: expectString,
176
- values: [expectString]
177
- }
178
- ],
179
- styleSources: [
180
- {
181
- type: "local",
182
- id: expectString
183
- },
184
- {
185
- type: "local",
186
- id: expectString
187
- }
188
- ],
189
- styles: [
190
- {
191
- breakpointId: "base",
192
- styleSourceId: expectString,
193
- state: void 0,
194
- property: "width",
195
- value: { type: "keyword", value: "auto" }
196
- },
197
- {
198
- breakpointId: "base",
199
- styleSourceId: expectString,
200
- state: void 0,
201
- property: "height",
202
- value: { type: "keyword", value: "auto" }
203
- },
204
- {
205
- breakpointId: "base",
206
- styleSourceId: expectString,
207
- state: void 0,
208
- property: "color",
209
- value: { type: "keyword", value: "black" }
210
- }
211
- ]
212
- });
213
- });
214
- test("generate data for embedding from props bound to data source variables", () => {
215
- expect(
216
- generateDataFromEmbedTemplate(
217
- [
218
- {
219
- type: "instance",
220
- component: "Box1",
221
- dataSources: {
222
- showOtherBoxDataSource: { type: "variable", initialValue: false }
223
- },
224
- props: [
225
- {
226
- type: "dataSource",
227
- name: "showOtherBox",
228
- dataSourceName: "showOtherBoxDataSource"
229
- }
230
- ],
231
- children: []
232
- },
233
- {
234
- type: "instance",
235
- component: "Box2",
236
- props: [
237
- {
238
- type: "dataSource",
239
- name: showAttribute,
240
- dataSourceName: "showOtherBoxDataSource"
241
- }
242
- ],
243
- children: []
244
- }
245
- ],
246
- /* @__PURE__ */ new Map(),
247
- defaultBreakpointId
248
- )
249
- ).toEqual({
250
- children: [
251
- { type: "id", value: expectString },
252
- { type: "id", value: expectString }
253
- ],
254
- instances: [
255
- { type: "instance", id: expectString, component: "Box1", children: [] },
256
- { type: "instance", id: expectString, component: "Box2", children: [] }
257
- ],
258
- props: [
259
- {
260
- id: expectString,
261
- instanceId: expectString,
262
- type: "dataSource",
263
- name: "showOtherBox",
264
- value: expectString
265
- },
266
- {
267
- id: expectString,
268
- instanceId: expectString,
269
- type: "dataSource",
270
- name: showAttribute,
271
- value: expectString
272
- }
273
- ],
274
- dataSources: [
275
- {
276
- type: "variable",
277
- id: expectString,
278
- scopeInstanceId: expectString,
279
- name: "showOtherBoxDataSource",
280
- value: {
281
- type: "boolean",
282
- value: false
283
- }
284
- }
285
- ],
286
- styleSourceSelections: [],
287
- styleSources: [],
288
- styles: []
289
- });
290
- });
291
- test("generate data for embedding from props bound to data source expressions", () => {
292
- expect(
293
- generateDataFromEmbedTemplate(
294
- [
295
- {
296
- type: "instance",
297
- component: "Box1",
298
- dataSources: {
299
- boxState: { type: "variable", initialValue: "initial" },
300
- boxStateSuccess: {
301
- type: "expression",
302
- code: `boxState === 'success'`
303
- }
304
- },
305
- props: [
306
- {
307
- type: "dataSource",
308
- name: "state",
309
- dataSourceName: "boxState"
310
- }
311
- ],
312
- children: []
313
- },
314
- {
315
- type: "instance",
316
- component: "Box2",
317
- props: [
318
- {
319
- type: "dataSource",
320
- name: showAttribute,
321
- dataSourceName: "boxStateSuccess"
322
- }
323
- ],
324
- children: []
325
- }
326
- ],
327
- /* @__PURE__ */ new Map(),
328
- defaultBreakpointId
329
- )
330
- ).toEqual({
331
- children: [
332
- { type: "id", value: expectString },
333
- { type: "id", value: expectString }
334
- ],
335
- instances: [
336
- { type: "instance", id: expectString, component: "Box1", children: [] },
337
- { type: "instance", id: expectString, component: "Box2", children: [] }
338
- ],
339
- props: [
340
- {
341
- id: expectString,
342
- instanceId: expectString,
343
- type: "dataSource",
344
- name: "state",
345
- value: expectString
346
- },
347
- {
348
- id: expectString,
349
- instanceId: expectString,
350
- type: "dataSource",
351
- name: showAttribute,
352
- value: expectString
353
- }
354
- ],
355
- dataSources: [
356
- {
357
- type: "variable",
358
- id: expectString,
359
- scopeInstanceId: expectString,
360
- name: "boxState",
361
- value: {
362
- type: "string",
363
- value: "initial"
364
- }
365
- },
366
- {
367
- type: "expression",
368
- id: expectString,
369
- scopeInstanceId: expectString,
370
- name: "boxStateSuccess",
371
- code: expect.stringMatching(/\$ws\$dataSource\$\w+ === 'success'/)
372
- }
373
- ],
374
- styleSourceSelections: [],
375
- styleSources: [],
376
- styles: []
377
- });
378
- });
379
- test("generate data for embedding from action props", () => {
380
- expect(
381
- generateDataFromEmbedTemplate(
382
- [
383
- {
384
- type: "instance",
385
- component: "Box1",
386
- dataSources: {
387
- boxState: { type: "variable", initialValue: "initial" }
388
- },
389
- props: [
390
- {
391
- type: "dataSource",
392
- name: "state",
393
- dataSourceName: "boxState"
394
- }
395
- ],
396
- children: [
397
- {
398
- type: "instance",
399
- component: "Box2",
400
- props: [
401
- {
402
- type: "action",
403
- name: "onClick",
404
- value: [{ type: "execute", code: `boxState = 'success'` }]
405
- },
406
- {
407
- type: "action",
408
- name: "onChange",
409
- value: [
410
- {
411
- type: "execute",
412
- args: ["value"],
413
- code: `boxState = value`
414
- }
415
- ]
416
- }
417
- ],
418
- children: []
419
- }
420
- ]
421
- }
422
- ],
423
- /* @__PURE__ */ new Map(),
424
- defaultBreakpointId
425
- )
426
- ).toEqual({
427
- children: [{ type: "id", value: expectString }],
428
- instances: [
429
- {
430
- type: "instance",
431
- id: expectString,
432
- component: "Box1",
433
- children: [{ type: "id", value: expectString }]
434
- },
435
- { type: "instance", id: expectString, component: "Box2", children: [] }
436
- ],
437
- props: [
438
- {
439
- id: expectString,
440
- instanceId: expectString,
441
- type: "dataSource",
442
- name: "state",
443
- value: expectString
444
- },
445
- {
446
- id: expectString,
447
- instanceId: expectString,
448
- type: "action",
449
- name: "onClick",
450
- value: [
451
- {
452
- type: "execute",
453
- args: [],
454
- code: expect.stringMatching(/\$ws\$dataSource\$\w+ = 'success'/)
455
- }
456
- ]
457
- },
458
- {
459
- id: expectString,
460
- instanceId: expectString,
461
- type: "action",
462
- name: "onChange",
463
- value: [
464
- {
465
- type: "execute",
466
- args: ["value"],
467
- code: expect.stringMatching(/\$ws\$dataSource\$\w+ = value/)
468
- }
469
- ]
470
- }
471
- ],
472
- dataSources: [
473
- {
474
- type: "variable",
475
- id: expectString,
476
- scopeInstanceId: expectString,
477
- name: "boxState",
478
- value: {
479
- type: "string",
480
- value: "initial"
481
- }
482
- }
483
- ],
484
- styleSourceSelections: [],
485
- styleSources: [],
486
- styles: []
487
- });
488
- });
489
- test("generate styles from tokens", () => {
490
- const presetTokens = {
491
- box: {
492
- styles: [
493
- {
494
- property: "width",
495
- value: { type: "keyword", value: "max-content" }
496
- },
497
- {
498
- property: "height",
499
- value: { type: "keyword", value: "max-content" }
500
- }
501
- ]
502
- },
503
- boxBright: {
504
- styles: [
505
- {
506
- property: "color",
507
- value: { type: "keyword", value: "red" }
508
- },
509
- {
510
- property: "backgroundColor",
511
- value: { type: "keyword", value: "pink" }
512
- }
513
- ]
514
- },
515
- boxNone: {
516
- styles: [
517
- {
518
- property: "color",
519
- value: { type: "keyword", value: "transparent" }
520
- },
521
- {
522
- property: "backgroundColor",
523
- value: { type: "keyword", value: "transparent" }
524
- }
525
- ]
526
- }
527
- };
528
- const { styleSourceSelections, styleSources, styles } = generateDataFromEmbedTemplate(
529
- [
530
- {
531
- type: "instance",
532
- component: "Box",
533
- tokens: ["box", "boxBright"],
534
- children: []
535
- }
536
- ],
537
- /* @__PURE__ */ new Map([["Box", { type: "container", icon: "", presetTokens }]]),
538
- defaultBreakpointId
539
- );
540
- expect(styleSources).toEqual([
541
- { id: "Box:box", name: "Box", type: "token" },
542
- { id: "Box:boxBright", name: "Box Bright", type: "token" }
543
- ]);
544
- expect(styleSourceSelections).toEqual([
545
- { instanceId: expectString, values: ["Box:box", "Box:boxBright"] }
546
- ]);
547
- expect(styles).toEqual([
548
- {
549
- breakpointId: "base",
550
- property: "width",
551
- styleSourceId: "Box:box",
552
- value: { type: "keyword", value: "max-content" }
553
- },
554
- {
555
- breakpointId: "base",
556
- property: "height",
557
- styleSourceId: "Box:box",
558
- value: { type: "keyword", value: "max-content" }
559
- },
560
- {
561
- breakpointId: "base",
562
- property: "color",
563
- styleSourceId: "Box:boxBright",
564
- value: { type: "keyword", value: "red" }
565
- },
566
- {
567
- breakpointId: "base",
568
- property: "backgroundColor",
569
- styleSourceId: "Box:boxBright",
570
- value: { type: "keyword", value: "pink" }
571
- }
572
- ]);
573
- });
574
- test("add namespace to selected components in embed template", () => {
575
- expect(
576
- namespaceMeta(
577
- {
578
- type: "container",
579
- label: "",
580
- icon: "",
581
- requiredAncestors: ["Button", "Box"],
582
- invalidAncestors: ["Tooltip"],
583
- indexWithinAncestor: "Tooltip",
584
- presetTokens: {
585
- base: { styles: [] },
586
- small: { styles: [] },
587
- large: { styles: [] }
588
- },
589
- template: [
590
- {
591
- type: "instance",
592
- component: "Tooltip",
593
- tokens: ["base", "small"],
594
- children: [
595
- { type: "text", value: "Some text" },
596
- {
597
- type: "instance",
598
- component: "Box",
599
- children: [
600
- {
601
- type: "instance",
602
- component: "Button",
603
- children: []
604
- }
605
- ]
606
- }
607
- ]
608
- }
609
- ]
610
- },
611
- "my-namespace",
612
- /* @__PURE__ */ new Set(["Tooltip", "Button"])
613
- )
614
- ).toEqual({
615
- type: "container",
616
- label: "",
617
- icon: "",
618
- requiredAncestors: ["my-namespace:Button", "Box"],
619
- invalidAncestors: ["my-namespace:Tooltip"],
620
- indexWithinAncestor: "my-namespace:Tooltip",
621
- presetTokens: {
622
- base: { styles: [] },
623
- small: { styles: [] },
624
- large: { styles: [] }
625
- },
626
- template: [
627
- {
628
- type: "instance",
629
- component: "my-namespace:Tooltip",
630
- tokens: ["base", "small"],
631
- children: [
632
- { type: "text", value: "Some text" },
633
- {
634
- type: "instance",
635
- component: "Box",
636
- children: [
637
- {
638
- type: "instance",
639
- component: "my-namespace:Button",
640
- children: []
641
- }
642
- ]
643
- }
644
- ]
645
- }
646
- ]
647
- });
648
- });