@builder.io/sdk-react-native 0.0.1-58 → 0.0.1-60

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-react-native",
3
3
  "description": "Builder.io SDK for React Native",
4
- "version": "0.0.1-58",
4
+ "version": "0.0.1-60",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
7
  "release:dev": "npm version prerelease --no-git-tag-version && npm publish --tag dev --access public"
@@ -4,35 +4,34 @@ import { TARGET } from "../../constants/target.js";
4
4
  import BuilderContext from "../../context/builder.context";
5
5
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
6
6
  import RenderInlinedStyles from "../render-inlined-styles.js";
7
+ import { convertStyleMaptoCSS } from "../../helpers/css.js";
8
+ import { getMaxWidthQueryForSize } from "../../constants/device-sizes.js";
7
9
  function BlockStyles(props) {
8
10
  function useBlock() {
9
11
  return getProcessedBlock({
10
12
  block: props.block,
11
13
  state: builderContext.state,
12
- context: builderContext.context
14
+ context: builderContext.context,
15
+ evaluateBindings: true
13
16
  });
14
17
  }
15
- function camelToKebabCase(string) {
16
- return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
17
- }
18
18
  function css() {
19
- var _a;
20
- const styleObject = (_a = useBlock().responsiveStyles) == null ? void 0 : _a.large;
21
- if (!styleObject) {
22
- return "";
23
- }
24
- let str = `.${useBlock().id} {`;
25
- for (const key in styleObject) {
26
- const value = styleObject[key];
27
- if (typeof value === "string") {
28
- str += `${camelToKebabCase(key)}: ${value};`;
29
- }
30
- }
31
- str += "}";
32
- return str;
19
+ const styles = useBlock().responsiveStyles;
20
+ const largeStyles = styles == null ? void 0 : styles.large;
21
+ const mediumStyles = styles == null ? void 0 : styles.medium;
22
+ const smallStyles = styles == null ? void 0 : styles.small;
23
+ return `
24
+ ${largeStyles ? `.${useBlock().id} {${convertStyleMaptoCSS(largeStyles)}}` : ""}
25
+ ${mediumStyles ? `${getMaxWidthQueryForSize("medium")} {
26
+ .${useBlock().id} {${convertStyleMaptoCSS(mediumStyles)}}
27
+ }` : ""}
28
+ ${smallStyles ? `${getMaxWidthQueryForSize("small")} {
29
+ .${useBlock().id} {${convertStyleMaptoCSS(smallStyles)}}
30
+ }` : ""}
31
+ }`;
33
32
  }
34
33
  const builderContext = useContext(BuilderContext);
35
- return /* @__PURE__ */ React.createElement(React.Fragment, null, TARGET === "vue" || TARGET === "svelte" ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(RenderInlinedStyles, {
34
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, TARGET === "vue2" || TARGET === "vue3" || TARGET === "svelte" ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(RenderInlinedStyles, {
36
35
  styles: css()
37
36
  })) : null);
38
37
  }
@@ -38,14 +38,21 @@ import { getBlockProperties } from "../../functions/get-block-properties.js";
38
38
  import { getBlockStyles } from "../../functions/get-block-styles.js";
39
39
  import { getBlockTag } from "../../functions/get-block-tag.js";
40
40
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
41
+ import { evaluate } from "../../functions/evaluate.js";
41
42
  import BlockStyles from "./block-styles.js";
42
43
  import { isEmptyHtmlElement } from "./render-block.helpers.js";
43
44
  import RenderComponent from "./render-component.js";
45
+ import RenderRepeatedBlock from "./render-repeated-block.js";
44
46
  function RenderBlock(props) {
45
- var _a, _b;
47
+ var _a, _b, _c;
46
48
  function component() {
47
49
  var _a2;
48
- const componentName = (_a2 = useBlock().component) == null ? void 0 : _a2.name;
50
+ const componentName = (_a2 = getProcessedBlock({
51
+ block: props.block,
52
+ state: builderContext.state,
53
+ context: builderContext.context,
54
+ evaluateBindings: false
55
+ }).component) == null ? void 0 : _a2.name;
49
56
  if (!componentName) {
50
57
  return null;
51
58
  }
@@ -75,10 +82,11 @@ function RenderBlock(props) {
75
82
  return getBlockTag(useBlock());
76
83
  }
77
84
  function useBlock() {
78
- return getProcessedBlock({
85
+ return repeatItemData() ? props.block : getProcessedBlock({
79
86
  block: props.block,
80
87
  state: builderContext.state,
81
- context: builderContext.context
88
+ context: builderContext.context,
89
+ evaluateBindings: true
82
90
  });
83
91
  }
84
92
  function attributes() {
@@ -99,30 +107,64 @@ function RenderBlock(props) {
99
107
  attributes: attributes()
100
108
  });
101
109
  }
110
+ function renderComponentProps() {
111
+ return {
112
+ blockChildren: children(),
113
+ componentRef: componentRef(),
114
+ componentOptions: componentOptions()
115
+ };
116
+ }
102
117
  function children() {
103
118
  var _a2;
104
119
  return (_a2 = useBlock().children) != null ? _a2 : [];
105
120
  }
106
- function noCompRefChildren() {
107
- return componentRef() ? [] : children();
121
+ function childrenWithoutParentComponent() {
122
+ const shouldRenderChildrenOutsideRef = !componentRef() && !repeatItemData();
123
+ return shouldRenderChildrenOutsideRef ? children() : [];
124
+ }
125
+ function repeatItemData() {
126
+ const _a2 = props.block, { repeat } = _a2, blockWithoutRepeat = __objRest(_a2, ["repeat"]);
127
+ if (!(repeat == null ? void 0 : repeat.collection)) {
128
+ return void 0;
129
+ }
130
+ const { collection, itemName } = repeat;
131
+ const itemsArray = evaluate({
132
+ code: collection,
133
+ state: builderContext.state,
134
+ context: builderContext.context
135
+ });
136
+ if (Array.isArray(itemsArray)) {
137
+ const collectionName = collection.split(".").pop();
138
+ const itemNameToUse = itemName || (collectionName ? collectionName + "Item" : "item");
139
+ const repeatArray = itemsArray.map((item, index) => ({
140
+ context: __spreadProps(__spreadValues({}, builderContext), {
141
+ state: __spreadProps(__spreadValues({}, builderContext.state), {
142
+ $index: index,
143
+ $item: item,
144
+ [itemNameToUse]: item,
145
+ [`$${itemNameToUse}Index`]: index
146
+ })
147
+ }),
148
+ block: blockWithoutRepeat
149
+ }));
150
+ return repeatArray;
151
+ } else {
152
+ return void 0;
153
+ }
108
154
  }
109
155
  const builderContext = useContext(BuilderContext);
110
156
  const TagNameRef = tagName();
111
- return /* @__PURE__ */ React.createElement(React.Fragment, null, shouldWrap() ? /* @__PURE__ */ React.createElement(React.Fragment, null, !isEmptyHtmlElement(tagName()) ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(TagNameRef, __spreadValues({}, attributes()), /* @__PURE__ */ React.createElement(RenderComponent, {
112
- blockChildren: children(),
113
- componentRef: componentRef(),
114
- componentOptions: componentOptions()
115
- }), (_a = noCompRefChildren()) == null ? void 0 : _a.map((child) => /* @__PURE__ */ React.createElement(RenderBlock, {
157
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, shouldWrap() ? /* @__PURE__ */ React.createElement(React.Fragment, null, !isEmptyHtmlElement(tagName()) ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(TagNameRef, __spreadValues({}, attributes()), repeatItemData() ? /* @__PURE__ */ React.createElement(React.Fragment, null, (_a = repeatItemData()) == null ? void 0 : _a.map((data, index) => /* @__PURE__ */ React.createElement(RenderRepeatedBlock, {
158
+ key: index,
159
+ repeatContext: data.context,
160
+ block: data.block
161
+ }))) : /* @__PURE__ */ React.createElement(RenderComponent, __spreadValues({}, renderComponentProps())), (_b = childrenWithoutParentComponent()) == null ? void 0 : _b.map((child) => /* @__PURE__ */ React.createElement(RenderBlock, {
116
162
  key: "render-block-" + child.id,
117
163
  block: child
118
- })), (_b = noCompRefChildren()) == null ? void 0 : _b.map((child) => /* @__PURE__ */ React.createElement(BlockStyles, {
164
+ })), (_c = childrenWithoutParentComponent()) == null ? void 0 : _c.map((child) => /* @__PURE__ */ React.createElement(BlockStyles, {
119
165
  key: "block-style-" + child.id,
120
166
  block: child
121
- })))) : /* @__PURE__ */ React.createElement(TagNameRef, __spreadValues({}, attributes()))) : /* @__PURE__ */ React.createElement(RenderComponent, {
122
- blockChildren: children(),
123
- componentRef: componentRef(),
124
- componentOptions: componentOptions()
125
- }));
167
+ })))) : /* @__PURE__ */ React.createElement(TagNameRef, __spreadValues({}, attributes()))) : /* @__PURE__ */ React.createElement(RenderComponent, __spreadValues({}, renderComponentProps())));
126
168
  }
127
169
  export {
128
170
  RenderBlock as default
@@ -0,0 +1,29 @@
1
+ import * as React from "react";
2
+ import BuilderContext from "../../context/builder.context";
3
+ import RenderBlock from "./render-block.js";
4
+ function RenderRepeatedBlock(props) {
5
+ return /* @__PURE__ */ React.createElement(BuilderContext.Provider, {
6
+ value: {
7
+ get content() {
8
+ return props.repeatContext.content;
9
+ },
10
+ get state() {
11
+ return props.repeatContext.state;
12
+ },
13
+ get context() {
14
+ return props.repeatContext.context;
15
+ },
16
+ get apiKey() {
17
+ return props.repeatContext.apiKey;
18
+ },
19
+ get registeredComponents() {
20
+ return props.repeatContext.registeredComponents;
21
+ }
22
+ }
23
+ }, /* @__PURE__ */ React.createElement(RenderBlock, {
24
+ block: props.block
25
+ }));
26
+ }
27
+ export {
28
+ RenderRepeatedBlock as default
29
+ };
@@ -77,8 +77,8 @@ function RenderContent(props) {
77
77
  var _a2, _b2;
78
78
  return __spreadValues(__spreadValues(__spreadValues({}, (_b2 = (_a2 = props.content) == null ? void 0 : _a2.data) == null ? void 0 : _b2.state), props.data), overrideState);
79
79
  }
80
- function context() {
81
- return {};
80
+ function contextContext() {
81
+ return props.context || {};
82
82
  }
83
83
  function allRegisteredComponents() {
84
84
  const allComponentsArray = [
@@ -114,7 +114,7 @@ function RenderContent(props) {
114
114
  if (jsCode) {
115
115
  evaluate({
116
116
  code: jsCode,
117
- context: context(),
117
+ context: contextContext(),
118
118
  state: contentState()
119
119
  });
120
120
  }
@@ -125,7 +125,7 @@ function RenderContent(props) {
125
125
  function evalExpression(expression) {
126
126
  return expression.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
127
127
  code: group,
128
- context: context(),
128
+ context: contextContext(),
129
129
  state: contentState()
130
130
  }));
131
131
  }
@@ -228,7 +228,7 @@ function RenderContent(props) {
228
228
  return contentState();
229
229
  },
230
230
  get context() {
231
- return context();
231
+ return contextContext();
232
232
  },
233
233
  get apiKey() {
234
234
  return props.apiKey;
@@ -1,11 +1,5 @@
1
1
  import * as React from 'react';
2
- const sizeNames = ["xsmall", "small", "medium", "large"];
3
- const sizes = {
4
- xsmall: {
5
- min: 0,
6
- default: 0,
7
- max: 0
8
- },
2
+ const SIZES = {
9
3
  small: {
10
4
  min: 320,
11
5
  default: 321,
@@ -20,21 +14,9 @@ const sizes = {
20
14
  min: 990,
21
15
  default: 991,
22
16
  max: 1200
23
- },
24
- getWidthForSize(size) {
25
- return this[size].default;
26
- },
27
- getSizeForWidth(width) {
28
- for (const size of sizeNames) {
29
- const value = this[size];
30
- if (width <= value.max) {
31
- return size;
32
- }
33
- }
34
- return "large";
35
17
  }
36
18
  };
19
+ const getMaxWidthQueryForSize = (size) => `@media (max-width: ${SIZES[size].max}px)`;
37
20
  export {
38
- sizeNames,
39
- sizes
21
+ getMaxWidthQueryForSize
40
22
  };
@@ -0,0 +1,5 @@
1
+ import * as React from 'react';
2
+ const camelToKebabCase = (string) => string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
3
+ export {
4
+ camelToKebabCase
5
+ };
@@ -0,0 +1,7 @@
1
+ import * as React from 'react';
2
+ const convertStyleObject = (obj) => {
3
+ return obj;
4
+ };
5
+ export {
6
+ convertStyleObject
7
+ };
@@ -15,45 +15,19 @@ var __spreadValues = (a, b) => {
15
15
  }
16
16
  return a;
17
17
  };
18
- const propertiesThatMustBeNumber = new Set(["lineHeight"]);
19
- const displayValues = new Set(["flex", "none"]);
20
- const SHOW_WARNINGS = false;
21
- function validateReactNativeStyles(styles) {
22
- for (const key in styles) {
23
- const propertyValue = styles[key];
24
- if (key === "display" && !displayValues.has(propertyValue)) {
25
- if (SHOW_WARNINGS) {
26
- console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
27
- }
28
- delete styles[key];
29
- }
30
- if (typeof propertyValue === "string" && propertyValue.match(/^-?\d/)) {
31
- const newValue = parseFloat(propertyValue);
32
- if (!isNaN(newValue)) {
33
- styles[key] = newValue;
34
- }
35
- if (typeof newValue === "number" && newValue < 0) {
36
- styles[key] = 0;
37
- }
38
- }
39
- if (propertiesThatMustBeNumber.has(key) && typeof styles[key] !== "number") {
40
- if (SHOW_WARNINGS) {
41
- console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
42
- }
43
- delete styles[key];
44
- }
45
- }
46
- }
18
+ import { getMaxWidthQueryForSize } from "../constants/device-sizes.js";
19
+ import { convertStyleObject } from "./convert-style-object.js";
20
+ import { sanitizeBlockStyles } from "./sanitize-styles.js";
47
21
  function getBlockStyles(block) {
48
- var _a, _b, _c;
49
- const styles = __spreadValues(__spreadValues({}, (_a = block.responsiveStyles) == null ? void 0 : _a.large), block.styles);
22
+ var _a, _b, _c, _d, _e;
23
+ const styles = __spreadValues(__spreadValues({}, convertStyleObject((_a = block.responsiveStyles) == null ? void 0 : _a.large)), block.styles);
50
24
  if ((_b = block.responsiveStyles) == null ? void 0 : _b.medium) {
51
- Object.assign(styles, block.responsiveStyles.medium);
25
+ styles[getMaxWidthQueryForSize("medium")] = convertStyleObject((_c = block.responsiveStyles) == null ? void 0 : _c.medium);
52
26
  }
53
- if ((_c = block.responsiveStyles) == null ? void 0 : _c.small) {
54
- Object.assign(styles, block.responsiveStyles.small);
27
+ if ((_d = block.responsiveStyles) == null ? void 0 : _d.small) {
28
+ styles[getMaxWidthQueryForSize("small")] = convertStyleObject((_e = block.responsiveStyles) == null ? void 0 : _e.small);
55
29
  }
56
- validateReactNativeStyles(styles);
30
+ sanitizeBlockStyles(styles);
57
31
  return styles;
58
32
  }
59
33
  export {
@@ -21,9 +21,11 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
21
  import { evaluate } from "./evaluate.js";
22
22
  import { set } from "./set.js";
23
23
  import { transformBlock } from "./transform-block.js";
24
- function getProcessedBlock(options) {
25
- const { state, context } = options;
26
- const block = transformBlock(options.block);
24
+ const evaluateBindings = ({
25
+ block,
26
+ context,
27
+ state
28
+ }) => {
27
29
  if (!block.bindings) {
28
30
  return block;
29
31
  }
@@ -33,14 +35,19 @@ function getProcessedBlock(options) {
33
35
  });
34
36
  for (const binding in block.bindings) {
35
37
  const expression = block.bindings[binding];
36
- const value = evaluate({
37
- code: expression,
38
- state,
39
- context
40
- });
38
+ const value = evaluate({ code: expression, state, context });
41
39
  set(copied, binding, value);
42
40
  }
43
41
  return copied;
42
+ };
43
+ function getProcessedBlock(options) {
44
+ const { state, context } = options;
45
+ const block = transformBlock(options.block);
46
+ if (evaluateBindings) {
47
+ return evaluateBindings({ block, state, context });
48
+ } else {
49
+ return block;
50
+ }
44
51
  }
45
52
  export {
46
53
  getProcessedBlock
@@ -21,7 +21,8 @@ test("Can process bindings", () => {
21
21
  const processed = getProcessedBlock({
22
22
  block,
23
23
  context: {},
24
- state: { test: "hello" }
24
+ state: { test: "hello" },
25
+ evaluateBindings: true
25
26
  });
26
27
  expect(processed).not.toEqual(block);
27
28
  expect((_a = processed.properties) == null ? void 0 : _a.foo).toEqual("baz");
@@ -0,0 +1,33 @@
1
+ import * as React from 'react';
2
+ const propertiesThatMustBeNumber = new Set(["lineHeight"]);
3
+ const displayValues = new Set(["flex", "none"]);
4
+ const SHOW_WARNINGS = false;
5
+ const sanitizeBlockStyles = (styles) => {
6
+ for (const key in styles) {
7
+ const propertyValue = styles[key];
8
+ if (key === "display" && !displayValues.has(propertyValue)) {
9
+ if (SHOW_WARNINGS) {
10
+ console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
11
+ }
12
+ delete styles[key];
13
+ }
14
+ if (typeof propertyValue === "string" && propertyValue.match(/^-?\d/)) {
15
+ const newValue = parseFloat(propertyValue);
16
+ if (!isNaN(newValue)) {
17
+ styles[key] = newValue;
18
+ }
19
+ if (typeof newValue === "number" && newValue < 0) {
20
+ styles[key] = 0;
21
+ }
22
+ }
23
+ if (propertiesThatMustBeNumber.has(key) && typeof styles[key] !== "number") {
24
+ if (SHOW_WARNINGS) {
25
+ console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
26
+ }
27
+ delete styles[key];
28
+ }
29
+ }
30
+ };
31
+ export {
32
+ sanitizeBlockStyles
33
+ };
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+ import { camelToKebabCase } from "../functions/camel-to-kebab-case";
3
+ const convertStyleMaptoCSS = (style) => {
4
+ const cssProps = Object.entries(style).map(([key, value]) => {
5
+ if (typeof value === "string") {
6
+ return `${camelToKebabCase(key)}: ${value};`;
7
+ }
8
+ });
9
+ return cssProps.join("\n");
10
+ };
11
+ export {
12
+ convertStyleMaptoCSS
13
+ };
@@ -1,25 +0,0 @@
1
- import React from "react";
2
- import { Text } from "react-native";
3
- class ErrorBoundary extends React.Component {
4
- constructor(props) {
5
- super(props);
6
- this.state = { hasError: false };
7
- }
8
- static getDerivedStateFromError(error) {
9
- return { hasError: true };
10
- }
11
- componentDidCatch(error, errorInfo) {
12
- console.error("Error rendering Builder.io block", error, errorInfo);
13
- }
14
- render() {
15
- if (this.state.hasError) {
16
- return /* @__PURE__ */ React.createElement(Text, {
17
- style: { color: "gray" }
18
- }, "Error rendering Builder.io block");
19
- }
20
- return this.props.children;
21
- }
22
- }
23
- export {
24
- ErrorBoundary as default
25
- };