@builder.io/sdk-react 0.2.3-4 → 0.3.1

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 (288) hide show
  1. package/README.md +72 -3
  2. package/dist/sdk/blocks/columns/columns.js +5 -5
  3. package/dist/sdk/blocks/image/image.js +8 -7
  4. package/dist/sdk/blocks/symbol/symbol.js +3 -2
  5. package/dist/sdk/blocks/video/video.js +4 -1
  6. package/dist/sdk/components/render-block/block-styles.js +3 -1
  7. package/dist/sdk/components/render-block/render-block.helpers.d.ts +0 -1
  8. package/dist/sdk/components/render-block/render-block.helpers.js +8 -20
  9. package/dist/sdk/components/render-block/render-block.js +24 -15
  10. package/dist/sdk/components/render-block/render-repeated-block.js +3 -2
  11. package/dist/sdk/components/render-content/render-content.js +31 -12
  12. package/dist/sdk/components/render-content-variants/helpers.d.ts +12 -0
  13. package/dist/sdk/components/render-content-variants/helpers.js +154 -0
  14. package/dist/sdk/components/render-content-variants/render-content-variants.d.ts +5 -0
  15. package/dist/sdk/components/render-content-variants/render-content-variants.js +29 -0
  16. package/dist/sdk/components/render-inlined-styles.js +2 -2
  17. package/dist/sdk/constants/sdk-version.d.ts +1 -0
  18. package/dist/sdk/constants/sdk-version.js +1 -0
  19. package/dist/sdk/context/builder.context.js +3 -2
  20. package/dist/sdk/context/types.d.ts +17 -2
  21. package/dist/sdk/functions/evaluate.d.ts +4 -3
  22. package/dist/sdk/functions/evaluate.js +23 -2
  23. package/dist/sdk/functions/evaluate.test.d.ts +1 -0
  24. package/dist/sdk/functions/evaluate.test.js +17 -0
  25. package/dist/sdk/functions/get-block-actions-handler.d.ts +1 -1
  26. package/dist/sdk/functions/get-block-actions-handler.js +3 -1
  27. package/dist/sdk/functions/get-block-actions.d.ts +1 -1
  28. package/dist/sdk/functions/get-processed-block.d.ts +2 -2
  29. package/dist/sdk/functions/get-processed-block.js +16 -4
  30. package/dist/sdk/functions/get-processed-block.test.js +3 -1
  31. package/dist/sdk/helpers/canTrack.d.ts +1 -0
  32. package/dist/sdk/helpers/canTrack.js +2 -0
  33. package/dist/sdk/index.d.ts +1 -0
  34. package/dist/sdk/scripts/init-editing.js +2 -0
  35. package/package.json +5 -5
  36. package/CHANGELOG.md +0 -25
  37. package/packages/react/src/blocks/BaseText.tsx +0 -19
  38. package/packages/react/src/blocks/button/button.tsx +0 -45
  39. package/packages/react/src/blocks/button/component-info.ts +0 -42
  40. package/packages/react/src/blocks/columns/columns.tsx +0 -211
  41. package/packages/react/src/blocks/columns/component-info.ts +0 -254
  42. package/packages/react/src/blocks/custom-code/component-info.ts +0 -31
  43. package/packages/react/src/blocks/custom-code/custom-code.tsx +0 -76
  44. package/packages/react/src/blocks/embed/component-info.ts +0 -55
  45. package/packages/react/src/blocks/embed/embed.tsx +0 -59
  46. package/packages/react/src/blocks/embed/helpers.ts +0 -8
  47. package/packages/react/src/blocks/form/component-info.ts +0 -282
  48. package/packages/react/src/blocks/form/form.tsx +0 -320
  49. package/packages/react/src/blocks/fragment/component-info.ts +0 -10
  50. package/packages/react/src/blocks/fragment/fragment.tsx +0 -14
  51. package/packages/react/src/blocks/image/component-info.ts +0 -172
  52. package/packages/react/src/blocks/image/image.helpers.ts +0 -75
  53. package/packages/react/src/blocks/image/image.tsx +0 -144
  54. package/packages/react/src/blocks/img/component-info.ts +0 -21
  55. package/packages/react/src/blocks/img/img.tsx +0 -40
  56. package/packages/react/src/blocks/input/component-info.ts +0 -78
  57. package/packages/react/src/blocks/input/input.tsx +0 -33
  58. package/packages/react/src/blocks/raw-text/component-info.ts +0 -15
  59. package/packages/react/src/blocks/raw-text/raw-text.tsx +0 -18
  60. package/packages/react/src/blocks/section/component-info.ts +0 -50
  61. package/packages/react/src/blocks/section/section.tsx +0 -33
  62. package/packages/react/src/blocks/select/component-info.ts +0 -61
  63. package/packages/react/src/blocks/select/select.tsx +0 -35
  64. package/packages/react/src/blocks/submit-button/component-info.ts +0 -32
  65. package/packages/react/src/blocks/submit-button/submit-button.tsx +0 -17
  66. package/packages/react/src/blocks/symbol/component-info.ts +0 -43
  67. package/packages/react/src/blocks/symbol/symbol.tsx +0 -111
  68. package/packages/react/src/blocks/text/component-info.ts +0 -24
  69. package/packages/react/src/blocks/text/text.tsx +0 -20
  70. package/packages/react/src/blocks/textarea/component-info.ts +0 -48
  71. package/packages/react/src/blocks/textarea/textarea.tsx +0 -24
  72. package/packages/react/src/blocks/util.ts +0 -18
  73. package/packages/react/src/blocks/video/component-info.ts +0 -109
  74. package/packages/react/src/blocks/video/video.tsx +0 -84
  75. package/packages/react/src/components/render-block/block-styles.tsx +0 -98
  76. package/packages/react/src/components/render-block/render-block.helpers.ts +0 -138
  77. package/packages/react/src/components/render-block/render-block.tsx +0 -216
  78. package/packages/react/src/components/render-block/render-component.tsx +0 -52
  79. package/packages/react/src/components/render-block/render-repeated-block.tsx +0 -43
  80. package/packages/react/src/components/render-block/types.ts +0 -7
  81. package/packages/react/src/components/render-blocks.tsx +0 -97
  82. package/packages/react/src/components/render-content/builder-editing.tsx +0 -8
  83. package/packages/react/src/components/render-content/components/render-styles.helpers.ts +0 -79
  84. package/packages/react/src/components/render-content/components/render-styles.tsx +0 -45
  85. package/packages/react/src/components/render-content/index.ts +0 -1
  86. package/packages/react/src/components/render-content/render-content.helpers.ts +0 -48
  87. package/packages/react/src/components/render-content/render-content.tsx +0 -410
  88. package/packages/react/src/components/render-content/render-content.types.ts +0 -33
  89. package/packages/react/src/components/render-inlined-styles.tsx +0 -36
  90. package/packages/react/src/constants/builder-registered-components.ts +0 -42
  91. package/packages/react/src/constants/device-sizes.ts +0 -65
  92. package/packages/react/src/constants/target.ts +0 -2
  93. package/packages/react/src/context/builder.context.ts +0 -12
  94. package/packages/react/src/context/types.ts +0 -26
  95. package/packages/react/src/functions/camel-to-kebab-case.ts +0 -2
  96. package/packages/react/src/functions/evaluate.ts +0 -57
  97. package/packages/react/src/functions/event-handler-name.ts +0 -6
  98. package/packages/react/src/functions/extract-text-styles.ts +0 -39
  99. package/packages/react/src/functions/fast-clone.ts +0 -5
  100. package/packages/react/src/functions/get-block-actions-handler.ts +0 -20
  101. package/packages/react/src/functions/get-block-actions.ts +0 -25
  102. package/packages/react/src/functions/get-block-component-options.ts +0 -12
  103. package/packages/react/src/functions/get-block-properties.ts +0 -75
  104. package/packages/react/src/functions/get-builder-search-params/fn.test.ts +0 -19
  105. package/packages/react/src/functions/get-builder-search-params/index.ts +0 -50
  106. package/packages/react/src/functions/get-content/ab-testing.ts +0 -132
  107. package/packages/react/src/functions/get-content/generate-content-url.test.ts +0 -95
  108. package/packages/react/src/functions/get-content/generate-content-url.ts +0 -59
  109. package/packages/react/src/functions/get-content/index.ts +0 -62
  110. package/packages/react/src/functions/get-content/types.ts +0 -53
  111. package/packages/react/src/functions/get-fetch.ts +0 -18
  112. package/packages/react/src/functions/get-global-this.ts +0 -17
  113. package/packages/react/src/functions/get-processed-block.test.ts +0 -33
  114. package/packages/react/src/functions/get-processed-block.ts +0 -54
  115. package/packages/react/src/functions/get-react-native-block-styles.ts +0 -33
  116. package/packages/react/src/functions/if-target.ts +0 -20
  117. package/packages/react/src/functions/is-browser.ts +0 -3
  118. package/packages/react/src/functions/is-editing.ts +0 -10
  119. package/packages/react/src/functions/is-iframe.ts +0 -5
  120. package/packages/react/src/functions/is-previewing.ts +0 -14
  121. package/packages/react/src/functions/on-change.test.ts +0 -23
  122. package/packages/react/src/functions/on-change.ts +0 -29
  123. package/packages/react/src/functions/register-component.ts +0 -50
  124. package/packages/react/src/functions/register.ts +0 -46
  125. package/packages/react/src/functions/sanitize-react-native-block-styles.ts +0 -65
  126. package/packages/react/src/functions/set-editor-settings.ts +0 -16
  127. package/packages/react/src/functions/set.test.ts +0 -19
  128. package/packages/react/src/functions/set.ts +0 -26
  129. package/packages/react/src/functions/track/helpers.ts +0 -67
  130. package/packages/react/src/functions/track/index.ts +0 -136
  131. package/packages/react/src/functions/track/interaction.ts +0 -80
  132. package/packages/react/src/functions/transform-block-properties.ts +0 -3
  133. package/packages/react/src/functions/transform-block.ts +0 -6
  134. package/packages/react/src/helpers/ab-tests.ts +0 -22
  135. package/packages/react/src/helpers/cookie.ts +0 -107
  136. package/packages/react/src/helpers/css.ts +0 -42
  137. package/packages/react/src/helpers/flatten.ts +0 -24
  138. package/packages/react/src/helpers/localStorage.ts +0 -40
  139. package/packages/react/src/helpers/logger.ts +0 -6
  140. package/packages/react/src/helpers/nullable.ts +0 -4
  141. package/packages/react/src/helpers/sessionId.ts +0 -38
  142. package/packages/react/src/helpers/time.ts +0 -4
  143. package/packages/react/src/helpers/url.test.ts +0 -23
  144. package/packages/react/src/helpers/url.ts +0 -16
  145. package/packages/react/src/helpers/uuid.ts +0 -17
  146. package/packages/react/src/helpers/visitorId.ts +0 -40
  147. package/packages/react/src/index-helpers/blocks-exports.ts +0 -10
  148. package/packages/react/src/index-helpers/top-of-file.ts +0 -4
  149. package/packages/react/src/index.ts +0 -14
  150. package/packages/react/src/scripts/init-editing.ts +0 -123
  151. package/packages/react/src/types/api-version.ts +0 -2
  152. package/packages/react/src/types/builder-block.ts +0 -69
  153. package/packages/react/src/types/builder-content.ts +0 -46
  154. package/packages/react/src/types/can-track.ts +0 -3
  155. package/packages/react/src/types/components.ts +0 -117
  156. package/packages/react/src/types/deep-partial.ts +0 -7
  157. package/packages/react/src/types/element.ts +0 -60
  158. package/packages/react/src/types/input.ts +0 -125
  159. package/packages/react/src/types/targets.ts +0 -6
  160. package/packages/react/src/types/typescript.ts +0 -7
  161. package/packages/rsc/src/blocks/BaseText.jsx +0 -12
  162. package/packages/rsc/src/blocks/button/button.jsx +0 -37
  163. package/packages/rsc/src/blocks/button/component-info.js +0 -40
  164. package/packages/rsc/src/blocks/columns/columns.jsx +0 -172
  165. package/packages/rsc/src/blocks/columns/component-info.js +0 -241
  166. package/packages/rsc/src/blocks/custom-code/component-info.js +0 -30
  167. package/packages/rsc/src/blocks/custom-code/custom-code.jsx +0 -57
  168. package/packages/rsc/src/blocks/embed/component-info.js +0 -43
  169. package/packages/rsc/src/blocks/embed/embed.jsx +0 -45
  170. package/packages/rsc/src/blocks/embed/helpers.js +0 -9
  171. package/packages/rsc/src/blocks/form/component-info.js +0 -261
  172. package/packages/rsc/src/blocks/form/form.jsx +0 -260
  173. package/packages/rsc/src/blocks/fragment/component-info.js +0 -10
  174. package/packages/rsc/src/blocks/fragment/fragment.jsx +0 -9
  175. package/packages/rsc/src/blocks/image/component-info.js +0 -150
  176. package/packages/rsc/src/blocks/image/image.helpers.js +0 -48
  177. package/packages/rsc/src/blocks/image/image.jsx +0 -122
  178. package/packages/rsc/src/blocks/img/component-info.js +0 -19
  179. package/packages/rsc/src/blocks/img/img.jsx +0 -21
  180. package/packages/rsc/src/blocks/input/component-info.js +0 -73
  181. package/packages/rsc/src/blocks/input/input.jsx +0 -23
  182. package/packages/rsc/src/blocks/raw-text/component-info.js +0 -15
  183. package/packages/rsc/src/blocks/raw-text/raw-text.jsx +0 -14
  184. package/packages/rsc/src/blocks/section/component-info.js +0 -48
  185. package/packages/rsc/src/blocks/section/section.jsx +0 -27
  186. package/packages/rsc/src/blocks/select/component-info.js +0 -58
  187. package/packages/rsc/src/blocks/select/select.jsx +0 -24
  188. package/packages/rsc/src/blocks/submit-button/component-info.js +0 -27
  189. package/packages/rsc/src/blocks/submit-button/submit-button.jsx +0 -13
  190. package/packages/rsc/src/blocks/symbol/component-info.js +0 -42
  191. package/packages/rsc/src/blocks/symbol/symbol.jsx +0 -89
  192. package/packages/rsc/src/blocks/text/component-info.js +0 -23
  193. package/packages/rsc/src/blocks/text/text.jsx +0 -17
  194. package/packages/rsc/src/blocks/textarea/component-info.js +0 -46
  195. package/packages/rsc/src/blocks/textarea/textarea.jsx +0 -17
  196. package/packages/rsc/src/blocks/util.js +0 -8
  197. package/packages/rsc/src/blocks/video/component-info.js +0 -105
  198. package/packages/rsc/src/blocks/video/video.jsx +0 -63
  199. package/packages/rsc/src/components/render-block/block-styles.jsx +0 -88
  200. package/packages/rsc/src/components/render-block/render-block.helpers.js +0 -129
  201. package/packages/rsc/src/components/render-block/render-block.jsx +0 -201
  202. package/packages/rsc/src/components/render-block/render-component.jsx +0 -39
  203. package/packages/rsc/src/components/render-block/render-repeated-block.jsx +0 -27
  204. package/packages/rsc/src/components/render-block/types.js +0 -0
  205. package/packages/rsc/src/components/render-blocks.jsx +0 -92
  206. package/packages/rsc/src/components/render-content/builder-editing.jsx +0 -64
  207. package/packages/rsc/src/components/render-content/components/render-styles.helpers.js +0 -57
  208. package/packages/rsc/src/components/render-content/components/render-styles.jsx +0 -39
  209. package/packages/rsc/src/components/render-content/index.js +0 -4
  210. package/packages/rsc/src/components/render-content/render-content.helpers.js +0 -48
  211. package/packages/rsc/src/components/render-content/render-content.jsx +0 -253
  212. package/packages/rsc/src/components/render-content/render-content.types.js +0 -0
  213. package/packages/rsc/src/components/render-inlined-styles.jsx +0 -33
  214. package/packages/rsc/src/constants/builder-registered-components.js +0 -54
  215. package/packages/rsc/src/constants/device-sizes.js +0 -48
  216. package/packages/rsc/src/constants/target.js +0 -4
  217. package/packages/rsc/src/context/builder.context.js +0 -14
  218. package/packages/rsc/src/context/types.js +0 -0
  219. package/packages/rsc/src/functions/camel-to-kebab-case.js +0 -4
  220. package/packages/rsc/src/functions/evaluate.js +0 -6
  221. package/packages/rsc/src/functions/event-handler-name.js +0 -7
  222. package/packages/rsc/src/functions/extract-text-styles.js +0 -22
  223. package/packages/rsc/src/functions/fast-clone.js +0 -4
  224. package/packages/rsc/src/functions/get-block-actions-handler.js +0 -11
  225. package/packages/rsc/src/functions/get-block-actions.js +0 -18
  226. package/packages/rsc/src/functions/get-block-component-options.js +0 -28
  227. package/packages/rsc/src/functions/get-block-properties.js +0 -53
  228. package/packages/rsc/src/functions/get-builder-search-params/fn.test.js +0 -13
  229. package/packages/rsc/src/functions/get-builder-search-params/index.js +0 -38
  230. package/packages/rsc/src/functions/get-content/ab-testing.js +0 -99
  231. package/packages/rsc/src/functions/get-content/generate-content-url.js +0 -60
  232. package/packages/rsc/src/functions/get-content/generate-content-url.test.js +0 -82
  233. package/packages/rsc/src/functions/get-content/index.js +0 -83
  234. package/packages/rsc/src/functions/get-content/types.js +0 -0
  235. package/packages/rsc/src/functions/get-fetch.js +0 -14
  236. package/packages/rsc/src/functions/get-global-this.js +0 -18
  237. package/packages/rsc/src/functions/get-processed-block.js +0 -59
  238. package/packages/rsc/src/functions/get-processed-block.test.js +0 -32
  239. package/packages/rsc/src/functions/get-react-native-block-styles.js +0 -33
  240. package/packages/rsc/src/functions/if-target.js +0 -15
  241. package/packages/rsc/src/functions/is-browser.js +0 -6
  242. package/packages/rsc/src/functions/is-editing.js +0 -8
  243. package/packages/rsc/src/functions/is-iframe.js +0 -7
  244. package/packages/rsc/src/functions/is-previewing.js +0 -14
  245. package/packages/rsc/src/functions/on-change.js +0 -27
  246. package/packages/rsc/src/functions/on-change.test.js +0 -19
  247. package/packages/rsc/src/functions/register-component.js +0 -68
  248. package/packages/rsc/src/functions/register.js +0 -29
  249. package/packages/rsc/src/functions/sanitize-react-native-block-styles.js +0 -66
  250. package/packages/rsc/src/functions/set-editor-settings.js +0 -15
  251. package/packages/rsc/src/functions/set.js +0 -11
  252. package/packages/rsc/src/functions/set.test.js +0 -16
  253. package/packages/rsc/src/functions/track/helpers.js +0 -50
  254. package/packages/rsc/src/functions/track/index.js +0 -129
  255. package/packages/rsc/src/functions/track/interaction.js +0 -53
  256. package/packages/rsc/src/functions/transform-block-properties.js +0 -6
  257. package/packages/rsc/src/functions/transform-block.js +0 -6
  258. package/packages/rsc/src/helpers/ab-tests.js +0 -16
  259. package/packages/rsc/src/helpers/cookie.js +0 -81
  260. package/packages/rsc/src/helpers/css.js +0 -34
  261. package/packages/rsc/src/helpers/flatten.js +0 -34
  262. package/packages/rsc/src/helpers/localStorage.js +0 -35
  263. package/packages/rsc/src/helpers/logger.js +0 -9
  264. package/packages/rsc/src/helpers/nullable.js +0 -4
  265. package/packages/rsc/src/helpers/sessionId.js +0 -52
  266. package/packages/rsc/src/helpers/time.js +0 -5
  267. package/packages/rsc/src/helpers/url.js +0 -13
  268. package/packages/rsc/src/helpers/url.test.js +0 -21
  269. package/packages/rsc/src/helpers/uuid.js +0 -13
  270. package/packages/rsc/src/helpers/visitorId.js +0 -34
  271. package/packages/rsc/src/index-helpers/blocks-exports.js +0 -22
  272. package/packages/rsc/src/index-helpers/top-of-file.js +0 -2
  273. package/packages/rsc/src/index.js +0 -13
  274. package/packages/rsc/src/scripts/init-editing.js +0 -93
  275. package/packages/rsc/src/types/api-version.js +0 -4
  276. package/packages/rsc/src/types/builder-block.js +0 -0
  277. package/packages/rsc/src/types/builder-content.js +0 -0
  278. package/packages/rsc/src/types/can-track.js +0 -0
  279. package/packages/rsc/src/types/components.js +0 -0
  280. package/packages/rsc/src/types/deep-partial.js +0 -0
  281. package/packages/rsc/src/types/element.js +0 -0
  282. package/packages/rsc/src/types/input.js +0 -0
  283. package/packages/rsc/src/types/targets.js +0 -0
  284. package/packages/rsc/src/types/typescript.js +0 -0
  285. package/tsconfig.json +0 -28
  286. package/tsconfig.sdk.json +0 -10
  287. package/tsconfig.server.json +0 -10
  288. package/vite.config.ts +0 -91
@@ -0,0 +1,29 @@
1
+ 'use client';
2
+ import * as React from "react";
3
+ import { useState } from "react";
4
+ import { checkShouldRunVariants, getVariantsScriptString } from "./helpers";
5
+ import RenderContent from "../render-content/render-content";
6
+ import { handleABTestingSync } from "../../helpers/ab-tests";
7
+ import { getDefaultCanTrack } from "../../helpers/canTrack";
8
+ function RenderContentVariants(props) {
9
+ const [variantScriptStr, setVariantScriptStr] = useState(() => getVariantsScriptString(Object.values(props.content?.variations || {}).map((value) => ({
10
+ id: value.id,
11
+ testRatio: value.testRatio,
12
+ })), props.content?.id || ""));
13
+ const [shouldRenderVariants, setShouldRenderVariants] = useState(() => checkShouldRunVariants({
14
+ canTrack: getDefaultCanTrack(props.canTrack),
15
+ content: props.content,
16
+ }));
17
+ const [ScriptTag, setScriptTag] = useState(() => "script");
18
+ const [TemplateTag, setTemplateTag] = useState(() => "template");
19
+ return (React.createElement(React.Fragment, null, shouldRenderVariants ? (React.createElement(React.Fragment, null,
20
+ Object.values(props.content.variations)?.map((variant) => (React.createElement(state.TemplateTag, { key: variant?.id, "data-template-variant-id": variant?.id },
21
+ React.createElement(RenderContent, { content: variant, apiKey: props.apiKey, apiVersion: props.apiVersion, canTrack: props.canTrack, customComponents: props.customComponents })))),
22
+ React.createElement(state.ScriptTag, { id: `variants-script-${props.content?.id}`, dangerouslySetInnerHTML: { __html: variantScriptStr } }),
23
+ React.createElement(RenderContent, { content: props.content, apiKey: props.apiKey, apiVersion: props.apiVersion, canTrack: props.canTrack, customComponents: props.customComponents }))) : (React.createElement(React.Fragment, null,
24
+ React.createElement(RenderContent, { content: handleABTestingSync({
25
+ item: props.content,
26
+ canTrack: getDefaultCanTrack(props.canTrack),
27
+ }), apiKey: props.apiKey, apiVersion: props.apiVersion, canTrack: props.canTrack, customComponents: props.customComponents })))));
28
+ }
29
+ export default RenderContentVariants;
@@ -3,9 +3,9 @@ import * as React from "react";
3
3
  import { TARGET } from "../constants/target.js";
4
4
  function RenderInlinedStyles(props) {
5
5
  function tag() {
6
- // NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
6
+ // NOTE: we have to obfuscate the name of the tag due to a limitation in the svelte-preprocessor plugin.
7
7
  // https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
8
- return "style";
8
+ return ("sty" + "le");
9
9
  }
10
10
  function injectedStyleScript() {
11
11
  return `<${tag()}>${props.styles}</${tag()}>`;
@@ -0,0 +1 @@
1
+ export declare const SDK_VERSION = "0.3.1";
@@ -0,0 +1 @@
1
+ export const SDK_VERSION = "0.3.1";
@@ -2,8 +2,9 @@ import { createContext } from "react";
2
2
  export default createContext({
3
3
  content: null,
4
4
  context: {},
5
- state: {},
6
- setState() { },
5
+ localState: undefined,
6
+ rootSetState() { },
7
+ rootState: {},
7
8
  apiKey: null,
8
9
  apiVersion: undefined,
9
10
  registeredComponents: {},
@@ -11,8 +11,23 @@ export type BuilderRenderContext = Record<string, unknown>;
11
11
  export interface BuilderContextInterface {
12
12
  content: Nullable<BuilderContent>;
13
13
  context: BuilderRenderContext;
14
- state: BuilderRenderState;
15
- setState?: (state: BuilderRenderState) => void;
14
+ /**
15
+ * The state of the application.
16
+ *
17
+ * NOTE: see `localState` below to understand how it is different from `rootState`.
18
+ */
19
+ rootState: BuilderRenderState;
20
+ /**
21
+ * Some frameworks have a `setState` function which needs to be invoked to notify
22
+ * the framework of state change. (other frameworks don't in which case it is `undefined')
23
+ */
24
+ rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
25
+ /**
26
+ * The local state of the current component. This is different from `rootState` in that
27
+ * it can be a child state created by a repeater containing local state.
28
+ * The `rootState` is where all of the state mutations are actually stored.
29
+ */
30
+ localState: BuilderRenderState | undefined;
16
31
  apiKey: string | null;
17
32
  apiVersion: ApiVersion | undefined;
18
33
  registeredComponents: RegisteredComponents;
@@ -1,6 +1,7 @@
1
- import type { BuilderContextInterface } from '../context/types.js';
2
- export declare function evaluate({ code, context, state, event, isExpression, }: {
1
+ import type { BuilderContextInterface, BuilderRenderState } from '../context/types.js';
2
+ export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression, }: {
3
3
  code: string;
4
4
  event?: Event;
5
5
  isExpression?: boolean;
6
- } & Pick<BuilderContextInterface, 'state' | 'context'>): any;
6
+ } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): any;
7
+ export declare function flattenState(rootState: Record<string | symbol, any>, localState: Record<string | symbol, any> | undefined, rootSetState: ((rootState: BuilderRenderState) => void) | undefined): BuilderRenderState;
@@ -1,6 +1,6 @@
1
1
  import { isBrowser } from './is-browser.js';
2
2
  import { isEditing } from './is-editing.js';
3
- export function evaluate({ code, context, state, event, isExpression = true, }) {
3
+ export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true, }) {
4
4
  if (code === '') {
5
5
  console.warn('Skipping evaluation of empty code block.');
6
6
  return;
@@ -20,9 +20,30 @@ export function evaluate({ code, context, state, event, isExpression = true, })
20
20
  code.trim().startsWith('return '));
21
21
  const useCode = useReturn ? `return (${code});` : code;
22
22
  try {
23
- return new Function('builder', 'Builder' /* <- legacy */, 'state', 'context', 'event', useCode)(builder, builder, state, context, event);
23
+ return new Function('builder', 'Builder' /* <- legacy */, 'state', 'context', 'event', useCode)(builder, builder, flattenState(rootState, localState, rootSetState), context, event);
24
24
  }
25
25
  catch (e) {
26
26
  console.warn('Builder custom code error: \n While Evaluating: \n ', useCode, '\n', e);
27
27
  }
28
28
  }
29
+ export function flattenState(rootState, localState, rootSetState) {
30
+ if (rootState === localState) {
31
+ throw new Error('rootState === localState');
32
+ }
33
+ return new Proxy(rootState, {
34
+ get: (_, prop) => {
35
+ if (localState && prop in localState) {
36
+ return localState[prop];
37
+ }
38
+ return rootState[prop];
39
+ },
40
+ set: (_, prop, value) => {
41
+ if (localState && prop in localState) {
42
+ throw new Error('Writing to local state is not allowed as it is read-only.');
43
+ }
44
+ rootState[prop] = value;
45
+ rootSetState?.(rootState);
46
+ return true;
47
+ },
48
+ });
49
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { flattenState } from './evaluate';
2
+ describe('flatten state', () => {
3
+ it('should behave normally when no PROTO_STATE', () => {
4
+ const localState = {};
5
+ const rootState = { foo: 'bar' };
6
+ const flattened = flattenState(rootState, localState, undefined);
7
+ expect(flattened.foo).toEqual('bar');
8
+ flattened.foo = 'baz';
9
+ expect(rootState.foo).toEqual('baz');
10
+ });
11
+ it('should shadow write ', () => {
12
+ const rootState = { foo: 'foo' };
13
+ const localState = { foo: 'baz' };
14
+ const flattened = flattenState(rootState, localState, undefined);
15
+ expect(() => (flattened.foo = 'bar')).toThrow('Writing to local state is not allowed as it is read-only.');
16
+ });
17
+ });
@@ -2,7 +2,7 @@ import type { BuilderContextInterface } from '../context/types.js';
2
2
  import type { BuilderBlock } from '../types/builder-block.js';
3
3
  type Options = {
4
4
  block: BuilderBlock;
5
- } & Pick<BuilderContextInterface, 'state' | 'context'>;
5
+ } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
6
6
  type EventHandler = (event: Event) => any;
7
7
  export declare const createEventHandler: (value: string, options: Options) => EventHandler;
8
8
  export {};
@@ -2,7 +2,9 @@ import { evaluate } from './evaluate.js';
2
2
  export const createEventHandler = (value, options) => (event) => evaluate({
3
3
  code: value,
4
4
  context: options.context,
5
- state: options.state,
5
+ localState: options.localState,
6
+ rootState: options.rootState,
7
+ rootSetState: options.rootSetState,
6
8
  event,
7
9
  isExpression: false,
8
10
  });
@@ -5,5 +5,5 @@ type Actions = {
5
5
  };
6
6
  export declare function getBlockActions(options: {
7
7
  block: BuilderBlock;
8
- } & Pick<BuilderContextInterface, 'state' | 'context'>): Actions;
8
+ } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions;
9
9
  export {};
@@ -1,10 +1,10 @@
1
1
  import type { BuilderContextInterface } from '../context/types.js';
2
2
  import type { BuilderBlock } from '../types/builder-block.js';
3
- export declare function getProcessedBlock({ block, context, shouldEvaluateBindings, state, }: {
3
+ export declare function getProcessedBlock({ block, context, shouldEvaluateBindings, localState, rootState, rootSetState, }: {
4
4
  block: BuilderBlock;
5
5
  /**
6
6
  * In some cases, we want to avoid evaluating bindings and only want framework-specific block transformation. It is
7
7
  * also sometimes too early to consider bindings, e.g. when we might be looking at a repeated block.
8
8
  */
9
9
  shouldEvaluateBindings: boolean;
10
- } & Pick<BuilderContextInterface, 'state' | 'context'>): BuilderBlock;
10
+ } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): BuilderBlock;
@@ -2,7 +2,7 @@ import { evaluate } from './evaluate.js';
2
2
  import { fastClone } from './fast-clone.js';
3
3
  import { set } from './set.js';
4
4
  import { transformBlock } from './transform-block.js';
5
- const evaluateBindings = ({ block, context, state, }) => {
5
+ const evaluateBindings = ({ block, context, localState, rootState, rootSetState, }) => {
6
6
  if (!block.bindings) {
7
7
  return block;
8
8
  }
@@ -14,15 +14,27 @@ const evaluateBindings = ({ block, context, state, }) => {
14
14
  };
15
15
  for (const binding in block.bindings) {
16
16
  const expression = block.bindings[binding];
17
- const value = evaluate({ code: expression, state, context });
17
+ const value = evaluate({
18
+ code: expression,
19
+ localState,
20
+ rootState,
21
+ rootSetState,
22
+ context,
23
+ });
18
24
  set(copied, binding, value);
19
25
  }
20
26
  return copied;
21
27
  };
22
- export function getProcessedBlock({ block, context, shouldEvaluateBindings, state, }) {
28
+ export function getProcessedBlock({ block, context, shouldEvaluateBindings, localState, rootState, rootSetState, }) {
23
29
  const transformedBlock = transformBlock(block);
24
30
  if (shouldEvaluateBindings) {
25
- return evaluateBindings({ block: transformedBlock, state, context });
31
+ return evaluateBindings({
32
+ block: transformedBlock,
33
+ localState,
34
+ rootState,
35
+ rootSetState,
36
+ context,
37
+ });
26
38
  }
27
39
  else {
28
40
  return transformedBlock;
@@ -19,7 +19,9 @@ test('Can process bindings', () => {
19
19
  const processed = getProcessedBlock({
20
20
  block,
21
21
  context: {},
22
- state: { test: 'hello' },
22
+ rootState: { test: 'hello' },
23
+ rootSetState: undefined,
24
+ localState: undefined,
23
25
  shouldEvaluateBindings: true,
24
26
  });
25
27
  expect(processed).not.toEqual(block);
@@ -0,0 +1 @@
1
+ export declare const getDefaultCanTrack: (canTrack?: boolean) => boolean;
@@ -0,0 +1,2 @@
1
+ import { checkIsDefined } from './nullable';
2
+ export const getDefaultCanTrack = (canTrack) => checkIsDefined(canTrack) ? canTrack : true;
@@ -10,3 +10,4 @@ export * from './functions/get-builder-search-params/index.js';
10
10
  export { track } from './functions/track/index.js';
11
11
  export type { RegisteredComponent } from './context/types';
12
12
  export type { ComponentInfo } from './types/components';
13
+ export type { RenderContentProps } from './components/render-content/render-content.types.js';
@@ -1,3 +1,4 @@
1
+ import { SDK_VERSION } from '../constants/sdk-version.js';
1
2
  import { TARGET } from '../constants/target.js';
2
3
  import { isBrowser } from '../functions/is-browser.js';
3
4
  import { register } from '../functions/register.js';
@@ -32,6 +33,7 @@ export const setupBrowserForEditing = (options = {}) => {
32
33
  type: 'builder.sdkInfo',
33
34
  data: {
34
35
  target: TARGET,
36
+ version: SDK_VERSION,
35
37
  // TODO: compile these in
36
38
  // type: process.env.SDK_TYPE,
37
39
  // version: process.env.SDK_VERSION,
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-react",
3
3
  "description": "Builder.io SDK for React",
4
- "version": "0.2.3-4",
4
+ "version": "0.3.1",
5
5
  "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "exports": {
7
10
  ".": "./dist/sdk/index.js",
8
11
  "./server": "./dist/sdk/functions/get-content/index.js"
@@ -11,10 +14,7 @@
11
14
  "build:types:server": "tsc -p ./tsconfig.server.json",
12
15
  "build:types:sdk": "tsc -p ./tsconfig.sdk.json",
13
16
  "build:types": "yarn build:types:sdk",
14
- "build": "yarn build:types",
15
- "release:patch": "yarn run build && npm version patch && npm publish",
16
- "release:minor": "yarn run build && npm version minor && npm publish",
17
- "release:dev": "yarn run build && npm version prerelease && npm publish --tag dev"
17
+ "build": "yarn build:types"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "react": "^18.2.0"
package/CHANGELOG.md DELETED
@@ -1,25 +0,0 @@
1
- ### 0.2.2
2
-
3
- - Fix: dynamic bindings for Link URLs.
4
- - Fix: previewing content that includes a symbol whose `model` property is a `page`.
5
- - Fix: "Show If"/"Hide If" bindings when the initial value is `undefined`.
6
-
7
- ### 0.2.1
8
-
9
- - No Changes.
10
-
11
- ### 0.2.0
12
-
13
- - Sets the default `apiVersion` to `v3`.
14
-
15
- In case you feel the need to use our older API Version `v2`, reach out to us at support@builder.io first. But you can override the default by setting `apiVersion` explicitly to `v2` as follows:
16
-
17
- ```jsx
18
- <RenderContent apiVersion="v2" />
19
- ```
20
-
21
- ```js
22
- getContent({ apiVersion: 'v2' });
23
- ```
24
-
25
- More details on the Builder API Versions visit [this link](https://www.builder.io/c/docs/content-api-versions).
@@ -1,19 +0,0 @@
1
- "use client";
2
- import * as React from "react";
3
- import { useContext } from "react";
4
-
5
- export interface BaseTextProps {
6
- text: string;
7
- }
8
-
9
- import BuilderContext from "../context/builder.context.js";
10
-
11
- function BaseText(props: BaseTextProps) {
12
- const builderContext = useContext(BuilderContext);
13
-
14
- return (
15
- <span style={builderContext.inheritedStyles as any}>{props.text}</span>
16
- );
17
- }
18
-
19
- export default BaseText;
@@ -1,45 +0,0 @@
1
- "use client";
2
- import * as React from "react";
3
-
4
- export interface ButtonProps {
5
- attributes?: any;
6
- text?: string;
7
- link?: string;
8
- openLinkInNewTab?: boolean;
9
- }
10
-
11
- function Button(props: ButtonProps) {
12
- return (
13
- <>
14
- {props.link ? (
15
- <>
16
- <a
17
- role="button"
18
- {...props.attributes}
19
- href={props.link}
20
- target={props.openLinkInNewTab ? "_blank" : undefined}
21
- >
22
- {props.text}
23
- </a>
24
- </>
25
- ) : (
26
- <>
27
- <button
28
- className={
29
- /** * We have to explicitly provide `class` so that Mitosis knows to merge it with `css`. */
30
- props.attributes.class + " button-2b50f164"
31
- }
32
- {...props.attributes}
33
- >
34
- {props.text}
35
- </button>
36
- </>
37
- )}
38
- <style>{`.button-2b50f164 {
39
- all: unset;
40
- }`}</style>
41
- </>
42
- );
43
- }
44
-
45
- export default Button;
@@ -1,42 +0,0 @@
1
- import type { ComponentInfo } from '../../types/components';
2
-
3
- export const componentInfo: ComponentInfo = {
4
- name: 'Core:Button',
5
-
6
- image:
7
- 'https://cdn.builder.io/api/v1/image/assets%2FIsxPKMo2gPRRKeakUztj1D6uqed2%2F81a15681c3e74df09677dfc57a615b13',
8
- defaultStyles: {
9
- // TODO: make min width more intuitive and set one
10
- appearance: 'none',
11
- paddingTop: '15px',
12
- paddingBottom: '15px',
13
- paddingLeft: '25px',
14
- paddingRight: '25px',
15
- backgroundColor: '#000000',
16
- color: 'white',
17
- borderRadius: '4px',
18
- textAlign: 'center',
19
- cursor: 'pointer',
20
- },
21
- inputs: [
22
- {
23
- name: 'text',
24
- type: 'text',
25
- defaultValue: 'Click me!',
26
- bubble: true,
27
- },
28
- {
29
- name: 'link',
30
- type: 'url',
31
- bubble: true,
32
- },
33
- {
34
- name: 'openLinkInNewTab',
35
- type: 'boolean',
36
- defaultValue: false,
37
- friendlyName: 'Open link in new tab',
38
- },
39
- ],
40
- static: true,
41
- noWrap: true,
42
- };
@@ -1,211 +0,0 @@
1
- "use client";
2
- import * as React from "react";
3
- import { useState, useContext } from "react";
4
-
5
- type Column = {
6
- blocks: BuilderBlock[];
7
- width?: number;
8
- };
9
- type CSSVal = string | number;
10
- type StackColumnsAt = "tablet" | "mobile" | "never";
11
- export interface ColumnProps {
12
- columns?: Column[];
13
- builderBlock: BuilderBlock; // TODO: Implement this when support for dynamic CSS lands
14
-
15
- space?: number; // TODO: Implement this when support for dynamic CSS lands
16
-
17
- stackColumnsAt?: StackColumnsAt; // TODO: Implement this when support for dynamic CSS lands
18
-
19
- reverseColumnsWhenStacked?: boolean;
20
- }
21
-
22
- import RenderBlocks from "../../components/render-blocks";
23
- import type { BuilderBlock } from "../../types/builder-block";
24
- import { getSizesForBreakpoints } from "../../constants/device-sizes";
25
- import type { SizeName } from "../../constants/device-sizes";
26
- import RenderInlinedStyles from "../../components/render-inlined-styles";
27
- import { TARGET } from "../../constants/target.js";
28
- import BuilderContext from "../../context/builder.context.js";
29
- import type { Dictionary } from "../../types/typescript";
30
-
31
- function Columns(props: ColumnProps) {
32
- const [gutterSize, setGutterSize] = useState(() =>
33
- typeof props.space === "number" ? props.space || 0 : 20
34
- );
35
-
36
- const [cols, setCols] = useState(() => props.columns || []);
37
-
38
- const [stackAt, setStackAt] = useState(
39
- () => props.stackColumnsAt || "tablet"
40
- );
41
-
42
- function getWidth(index: number) {
43
- return cols[index]?.width || 100 / cols.length;
44
- }
45
-
46
- function getColumnCssWidth(index: number) {
47
- const subtractWidth = (gutterSize * (cols.length - 1)) / cols.length;
48
- return `calc(${getWidth(index)}% - ${subtractWidth}px)`;
49
- }
50
-
51
- function getTabletStyle({
52
- stackedStyle,
53
- desktopStyle,
54
- }: {
55
- stackedStyle: CSSVal;
56
- desktopStyle: CSSVal;
57
- }) {
58
- return stackAt === "tablet" ? stackedStyle : desktopStyle;
59
- }
60
-
61
- function getMobileStyle({
62
- stackedStyle,
63
- desktopStyle,
64
- }: {
65
- stackedStyle: CSSVal;
66
- desktopStyle: CSSVal;
67
- }) {
68
- return stackAt === "never" ? desktopStyle : stackedStyle;
69
- }
70
-
71
- const [flexDir, setFlexDir] = useState(() =>
72
- props.stackColumnsAt === "never"
73
- ? "row"
74
- : props.reverseColumnsWhenStacked
75
- ? "column-reverse"
76
- : "column"
77
- );
78
-
79
- function columnsCssVars() {
80
- if (TARGET === "reactNative") {
81
- return {
82
- flexDirection: flexDir,
83
- } as Dictionary<string>;
84
- }
85
-
86
- return {
87
- "--flex-dir": flexDir,
88
- "--flex-dir-tablet": getTabletStyle({
89
- stackedStyle: flexDir,
90
- desktopStyle: "row",
91
- }),
92
- } as Dictionary<string>;
93
- }
94
-
95
- function columnCssVars(index: number) {
96
- const gutter = index === 0 ? 0 : gutterSize;
97
-
98
- if (TARGET === "reactNative") {
99
- return {
100
- marginLeft: props.stackColumnsAt === "never" ? gutter : 0,
101
- } as any as Dictionary<string>;
102
- }
103
-
104
- const width = getColumnCssWidth(index);
105
- const gutterPixels = `${gutterSize}px`;
106
- const mobileWidth = "100%";
107
- const mobileMarginLeft = 0;
108
- return {
109
- width,
110
- "margin-left": gutterPixels,
111
- "--column-width-mobile": getMobileStyle({
112
- stackedStyle: mobileWidth,
113
- desktopStyle: width,
114
- }),
115
- "--column-margin-left-mobile": getMobileStyle({
116
- stackedStyle: mobileMarginLeft,
117
- desktopStyle: gutterPixels,
118
- }),
119
- "--column-width-tablet": getTabletStyle({
120
- stackedStyle: mobileWidth,
121
- desktopStyle: width,
122
- }),
123
- "--column-margin-left-tablet": getTabletStyle({
124
- stackedStyle: mobileMarginLeft,
125
- desktopStyle: gutterPixels,
126
- }),
127
- } as any as Dictionary<string>;
128
- }
129
-
130
- function getWidthForBreakpointSize(size: SizeName) {
131
- const breakpointSizes = getSizesForBreakpoints(
132
- builderContext.content?.meta?.breakpoints || {}
133
- );
134
- return breakpointSizes[size].max;
135
- }
136
-
137
- function columnsStyles() {
138
- return `
139
- @media (max-width: ${getWidthForBreakpointSize("medium")}px) {
140
- .${props.builderBlock.id}-breakpoints {
141
- flex-direction: var(--flex-dir-tablet);
142
- align-items: stretch;
143
- }
144
-
145
- .${props.builderBlock.id}-breakpoints > .builder-column {
146
- width: var(--column-width-tablet) !important;
147
- margin-left: var(--column-margin-left-tablet) !important;
148
- }
149
- }
150
-
151
- @media (max-width: ${getWidthForBreakpointSize("small")}px) {
152
- .${props.builderBlock.id}-breakpoints {
153
- flex-direction: var(--flex-dir);
154
- align-items: stretch;
155
- }
156
-
157
- .${props.builderBlock.id}-breakpoints > .builder-column {
158
- width: var(--column-width-mobile) !important;
159
- margin-left: var(--column-margin-left-mobile) !important;
160
- }
161
- },
162
- `;
163
- }
164
-
165
- const builderContext = useContext(BuilderContext);
166
-
167
- return (
168
- <>
169
- <div
170
- className={
171
- `builder-columns ${props.builderBlock.id}-breakpoints` +
172
- " div-66c71955"
173
- }
174
- style={columnsCssVars()}
175
- >
176
- {TARGET !== "reactNative" ? (
177
- <>
178
- <RenderInlinedStyles styles={columnsStyles()} />
179
- </>
180
- ) : null}
181
-
182
- {props.columns?.map((column, index) => (
183
- <div
184
- className="builder-column div-66c71955-2"
185
- style={columnCssVars(index)}
186
- key={index}
187
- >
188
- <RenderBlocks
189
- blocks={column.blocks}
190
- path={`component.options.columns.${index}.blocks`}
191
- parent={props.builderBlock.id}
192
- styleProp={{
193
- flexGrow: "1",
194
- }}
195
- />
196
- </div>
197
- ))}
198
- </div>
199
- <style>{`.div-66c71955 {
200
- display: flex;
201
- line-height: normal;
202
- }.div-66c71955-2 {
203
- display: flex;
204
- flex-direction: column;
205
- align-items: stretch;
206
- }`}</style>
207
- </>
208
- );
209
- }
210
-
211
- export default Columns;