@builder.io/sdk-solid 0.12.6 → 0.12.8

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/edge/dev.jsx CHANGED
@@ -107,7 +107,7 @@ import { createContext as createContext2 } from "solid-js";
107
107
  var components_context_default = createContext2({ registeredComponents: {} });
108
108
 
109
109
  // src/components/block/block.tsx
110
- import { Show as Show4, For as For2, createSignal as createSignal4 } from "solid-js";
110
+ import { Show as Show4, For as For2, onMount, createSignal as createSignal4 } from "solid-js";
111
111
 
112
112
  // src/functions/get-block-component-options.ts
113
113
  function getBlockComponentOptions(block) {
@@ -3565,6 +3565,212 @@ function getProcessedBlock({
3565
3565
  }
3566
3566
  }
3567
3567
 
3568
+ // src/components/block/animator.ts
3569
+ function throttle(func, wait, options = {}) {
3570
+ let context;
3571
+ let args;
3572
+ let result;
3573
+ let timeout = null;
3574
+ let previous = 0;
3575
+ const later = function() {
3576
+ previous = options.leading === false ? 0 : Date.now();
3577
+ timeout = null;
3578
+ result = func.apply(context, args);
3579
+ if (!timeout)
3580
+ context = args = null;
3581
+ };
3582
+ return function() {
3583
+ const now = Date.now();
3584
+ if (!previous && options.leading === false)
3585
+ previous = now;
3586
+ const remaining = wait - (now - previous);
3587
+ context = this;
3588
+ args = arguments;
3589
+ if (remaining <= 0 || remaining > wait) {
3590
+ if (timeout) {
3591
+ clearTimeout(timeout);
3592
+ timeout = null;
3593
+ }
3594
+ previous = now;
3595
+ result = func.apply(context, args);
3596
+ if (!timeout)
3597
+ context = args = null;
3598
+ } else if (!timeout && options.trailing !== false) {
3599
+ timeout = setTimeout(later, remaining);
3600
+ }
3601
+ return result;
3602
+ };
3603
+ }
3604
+ function assign(target, ..._args) {
3605
+ const to = Object(target);
3606
+ for (let index = 1; index < arguments.length; index++) {
3607
+ const nextSource = arguments[index];
3608
+ if (nextSource != null) {
3609
+ for (const nextKey in nextSource) {
3610
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
3611
+ to[nextKey] = nextSource[nextKey];
3612
+ }
3613
+ }
3614
+ }
3615
+ }
3616
+ return to;
3617
+ }
3618
+ var camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
3619
+ function bindAnimations(animations) {
3620
+ for (const animation of animations) {
3621
+ switch (animation.trigger) {
3622
+ case "pageLoad":
3623
+ triggerAnimation(animation);
3624
+ break;
3625
+ case "hover":
3626
+ bindHoverAnimation(animation);
3627
+ break;
3628
+ case "scrollInView":
3629
+ bindScrollInViewAnimation(animation);
3630
+ break;
3631
+ }
3632
+ }
3633
+ }
3634
+ function warnElementNotPresent(id2) {
3635
+ console.warn(`Cannot animate element: element with ID ${id2} not found!`);
3636
+ }
3637
+ function augmentAnimation(animation, element) {
3638
+ const stylesUsed = getAllStylesUsed(animation);
3639
+ const computedStyle = getComputedStyle(element);
3640
+ const firstStyles = animation.steps[0].styles;
3641
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
3642
+ const bothStyles = [firstStyles, lastStyles];
3643
+ for (const styles of bothStyles) {
3644
+ for (const style of stylesUsed) {
3645
+ if (!(style in styles)) {
3646
+ styles[style] = computedStyle[style];
3647
+ }
3648
+ }
3649
+ }
3650
+ }
3651
+ function getAllStylesUsed(animation) {
3652
+ const properties = [];
3653
+ for (const step of animation.steps) {
3654
+ for (const key in step.styles) {
3655
+ if (properties.indexOf(key) === -1) {
3656
+ properties.push(key);
3657
+ }
3658
+ }
3659
+ }
3660
+ return properties;
3661
+ }
3662
+ function triggerAnimation(animation) {
3663
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3664
+ if (!elements.length) {
3665
+ warnElementNotPresent(animation.elementId || animation.id || "");
3666
+ return;
3667
+ }
3668
+ Array.from(elements).forEach((element) => {
3669
+ augmentAnimation(animation, element);
3670
+ element.style.transition = "none";
3671
+ element.style.transitionDelay = "0";
3672
+ assign(element.style, animation.steps[0].styles);
3673
+ setTimeout(() => {
3674
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3675
+ if (animation.delay) {
3676
+ element.style.transitionDelay = animation.delay + "s";
3677
+ }
3678
+ assign(element.style, animation.steps[1].styles);
3679
+ setTimeout(() => {
3680
+ element.style.transition = "";
3681
+ element.style.transitionDelay = "";
3682
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
3683
+ });
3684
+ });
3685
+ }
3686
+ function bindHoverAnimation(animation) {
3687
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3688
+ if (!elements.length) {
3689
+ warnElementNotPresent(animation.elementId || animation.id || "");
3690
+ return;
3691
+ }
3692
+ Array.from(elements).forEach((element) => {
3693
+ augmentAnimation(animation, element);
3694
+ const defaultState = animation.steps[0].styles;
3695
+ const hoverState = animation.steps[1].styles;
3696
+ function attachDefaultState() {
3697
+ assign(element.style, defaultState);
3698
+ }
3699
+ function attachHoverState() {
3700
+ assign(element.style, hoverState);
3701
+ }
3702
+ attachDefaultState();
3703
+ element.addEventListener("mouseenter", attachHoverState);
3704
+ element.addEventListener("mouseleave", attachDefaultState);
3705
+ setTimeout(() => {
3706
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3707
+ if (animation.delay) {
3708
+ element.style.transitionDelay = animation.delay + "s";
3709
+ }
3710
+ });
3711
+ });
3712
+ }
3713
+ function bindScrollInViewAnimation(animation) {
3714
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3715
+ if (!elements.length) {
3716
+ warnElementNotPresent(animation.elementId || animation.id || "");
3717
+ return;
3718
+ }
3719
+ Array.from(elements).forEach((element) => {
3720
+ augmentAnimation(animation, element);
3721
+ let triggered = false;
3722
+ let pendingAnimation = false;
3723
+ function immediateOnScroll() {
3724
+ if (!triggered && isScrolledIntoView(element)) {
3725
+ triggered = true;
3726
+ pendingAnimation = true;
3727
+ setTimeout(() => {
3728
+ assign(element.style, animation.steps[1].styles);
3729
+ if (!animation.repeat) {
3730
+ document.removeEventListener("scroll", onScroll);
3731
+ }
3732
+ setTimeout(() => {
3733
+ pendingAnimation = false;
3734
+ if (!animation.repeat) {
3735
+ element.style.transition = "";
3736
+ element.style.transitionDelay = "";
3737
+ }
3738
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
3739
+ });
3740
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
3741
+ triggered = false;
3742
+ assign(element.style, animation.steps[0].styles);
3743
+ }
3744
+ }
3745
+ const onScroll = throttle(immediateOnScroll, 200, {
3746
+ leading: false
3747
+ });
3748
+ function isScrolledIntoView(elem) {
3749
+ const rect = elem.getBoundingClientRect();
3750
+ const windowHeight = window.innerHeight;
3751
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
3752
+ const threshold = thresholdPercent * windowHeight;
3753
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
3754
+ }
3755
+ const defaultState = animation.steps[0].styles;
3756
+ function attachDefaultState() {
3757
+ assign(element.style, defaultState);
3758
+ }
3759
+ attachDefaultState();
3760
+ setTimeout(() => {
3761
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3762
+ if (animation.delay) {
3763
+ element.style.transitionDelay = animation.delay + "s";
3764
+ }
3765
+ });
3766
+ document.addEventListener("scroll", onScroll, {
3767
+ capture: true,
3768
+ passive: true
3769
+ });
3770
+ immediateOnScroll();
3771
+ });
3772
+ }
3773
+
3568
3774
  // src/components/block/block.helpers.ts
3569
3775
  var getComponent = ({
3570
3776
  block,
@@ -4087,6 +4293,18 @@ function Block(props) {
4087
4293
  isInteractive: !blockComponent()?.isRSC
4088
4294
  };
4089
4295
  }
4296
+ onMount(() => {
4297
+ const blockId = processedBlock().id;
4298
+ const animations = processedBlock().animations;
4299
+ if (animations && blockId) {
4300
+ bindAnimations(
4301
+ animations.filter((item) => item.trigger !== "hover").map((animation) => ({
4302
+ ...animation,
4303
+ elementId: blockId
4304
+ }))
4305
+ );
4306
+ }
4307
+ });
4090
4308
  return <Show4 when={canShowBlock()}>
4091
4309
  <Block_styles_default block={props.block} context={props.context} />
4092
4310
  <Show4
@@ -4535,10 +4753,10 @@ function SectionComponent(props) {
4535
4753
  var section_default = SectionComponent;
4536
4754
 
4537
4755
  // src/blocks/symbol/symbol.tsx
4538
- import { onMount as onMount4, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
4756
+ import { onMount as onMount5, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
4539
4757
 
4540
4758
  // src/components/content-variants/content-variants.tsx
4541
- import { Show as Show11, For as For5, onMount as onMount3, createSignal as createSignal13 } from "solid-js";
4759
+ import { Show as Show11, For as For5, onMount as onMount4, createSignal as createSignal13 } from "solid-js";
4542
4760
 
4543
4761
  // src/helpers/url.ts
4544
4762
  var getTopLevelDomain = (host) => {
@@ -5016,12 +5234,12 @@ var componentInfo3 = {
5016
5234
  };
5017
5235
 
5018
5236
  // src/blocks/custom-code/custom-code.tsx
5019
- import { onMount, createSignal as createSignal7 } from "solid-js";
5237
+ import { onMount as onMount2, createSignal as createSignal7 } from "solid-js";
5020
5238
  function CustomCode(props) {
5021
5239
  const [scriptsInserted, setScriptsInserted] = createSignal7([]);
5022
5240
  const [scriptsRun, setScriptsRun] = createSignal7([]);
5023
5241
  let elementRef;
5024
- onMount(() => {
5242
+ onMount2(() => {
5025
5243
  if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5026
5244
  return;
5027
5245
  }
@@ -5742,7 +5960,7 @@ function InlinedScript(props) {
5742
5960
  var Inlined_script_default = InlinedScript;
5743
5961
 
5744
5962
  // src/components/content/components/enable-editor.tsx
5745
- import { Show as Show9, onMount as onMount2, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
5963
+ import { Show as Show9, onMount as onMount3, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
5746
5964
  import { Dynamic as Dynamic5 } from "solid-js/web";
5747
5965
 
5748
5966
  // src/helpers/preview-lru-cache/get.ts
@@ -5979,13 +6197,6 @@ async function fetchEntries(options) {
5979
6197
  }
5980
6198
  var getAllContent = fetchEntries;
5981
6199
 
5982
- // src/functions/is-from-trusted-host.ts
5983
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
5984
- function isFromTrustedHost(trustedHosts, e) {
5985
- const url = new URL(e.origin), hostname = url.hostname;
5986
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
5987
- }
5988
-
5989
6200
  // src/functions/is-previewing.ts
5990
6201
  function isPreviewing() {
5991
6202
  if (!isBrowser()) {
@@ -6236,8 +6447,15 @@ var getInteractionPropertiesForEvent = (event) => {
6236
6447
  };
6237
6448
  };
6238
6449
 
6450
+ // src/functions/is-from-trusted-host.ts
6451
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
6452
+ function isFromTrustedHost(trustedHosts, e) {
6453
+ const url = new URL(e.origin), hostname = url.hostname;
6454
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6455
+ }
6456
+
6239
6457
  // src/constants/sdk-version.ts
6240
- var SDK_VERSION = "0.12.6";
6458
+ var SDK_VERSION = "0.12.8";
6241
6459
 
6242
6460
  // src/functions/register.ts
6243
6461
  var registry = {};
@@ -6374,6 +6592,66 @@ var setupBrowserForEditing = (options = {}) => {
6374
6592
  }
6375
6593
  };
6376
6594
 
6595
+ // src/helpers/subscribe-to-editor.ts
6596
+ var createEditorListener = ({
6597
+ model,
6598
+ trustedHosts,
6599
+ callbacks
6600
+ }) => {
6601
+ return (event) => {
6602
+ if (!isFromTrustedHost(trustedHosts, event)) {
6603
+ return;
6604
+ }
6605
+ const {
6606
+ data
6607
+ } = event;
6608
+ if (data) {
6609
+ switch (data.type) {
6610
+ case "builder.configureSdk": {
6611
+ callbacks.configureSdk(data.data);
6612
+ break;
6613
+ }
6614
+ case "builder.triggerAnimation": {
6615
+ callbacks.animation(data.data);
6616
+ break;
6617
+ }
6618
+ case "builder.contentUpdate": {
6619
+ const messageContent = data.data;
6620
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6621
+ const contentData = messageContent.data;
6622
+ if (key === model) {
6623
+ callbacks.contentUpdate(contentData);
6624
+ }
6625
+ break;
6626
+ }
6627
+ }
6628
+ }
6629
+ };
6630
+ };
6631
+ var subscribeToEditor = (model, callback, options) => {
6632
+ if (!isBrowser) {
6633
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
6634
+ return () => {
6635
+ };
6636
+ }
6637
+ setupBrowserForEditing();
6638
+ const listener = createEditorListener({
6639
+ callbacks: {
6640
+ contentUpdate: callback,
6641
+ animation: () => {
6642
+ },
6643
+ configureSdk: () => {
6644
+ }
6645
+ },
6646
+ model,
6647
+ trustedHosts: options?.trustedHosts
6648
+ });
6649
+ window.addEventListener("message", listener);
6650
+ return () => {
6651
+ window.removeEventListener("message", listener);
6652
+ };
6653
+ };
6654
+
6377
6655
  // src/components/content/components/enable-editor.tsx
6378
6656
  function EnableEditor(props) {
6379
6657
  const [forceReRenderCount, setForceReRenderCount] = createSignal10(0);
@@ -6419,14 +6697,11 @@ function EnableEditor(props) {
6419
6697
  }));
6420
6698
  }
6421
6699
  function processMessage(event) {
6422
- if (!isFromTrustedHost(props.trustedHosts, event)) {
6423
- return;
6424
- }
6425
- const { data } = event;
6426
- if (data) {
6427
- switch (data.type) {
6428
- case "builder.configureSdk": {
6429
- const messageContent = data.data;
6700
+ return createEditorListener({
6701
+ model: props.model,
6702
+ trustedHosts: props.trustedHosts,
6703
+ callbacks: {
6704
+ configureSdk: (messageContent) => {
6430
6705
  const { breakpoints, contentId } = messageContent;
6431
6706
  if (!contentId || contentId !== props.builderContextSignal.content?.id) {
6432
6707
  return;
@@ -6437,22 +6712,18 @@ function EnableEditor(props) {
6437
6712
  breakpoints
6438
6713
  }
6439
6714
  });
6440
- }
6441
- setForceReRenderCount(forceReRenderCount() + 1);
6442
- break;
6443
- }
6444
- case "builder.contentUpdate": {
6445
- const messageContent = data.data;
6446
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6447
- const contentData = messageContent.data;
6448
- if (key === props.model) {
6449
- mergeNewContent(contentData);
6450
6715
  setForceReRenderCount(forceReRenderCount() + 1);
6451
6716
  }
6452
- break;
6717
+ },
6718
+ animation: (animation) => {
6719
+ triggerAnimation(animation);
6720
+ },
6721
+ contentUpdate: (newContent) => {
6722
+ mergeNewContent(newContent);
6723
+ setForceReRenderCount(forceReRenderCount() + 1);
6453
6724
  }
6454
6725
  }
6455
- }
6726
+ })(event);
6456
6727
  }
6457
6728
  function evaluateJsCode() {
6458
6729
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -6538,7 +6809,7 @@ function EnableEditor(props) {
6538
6809
  }
6539
6810
  }
6540
6811
  let elementRef;
6541
- onMount2(() => {
6812
+ onMount3(() => {
6542
6813
  if (isBrowser()) {
6543
6814
  if (isEditing() && true) {
6544
6815
  setForceReRenderCount(forceReRenderCount() + 1);
@@ -6603,7 +6874,7 @@ function EnableEditor(props) {
6603
6874
  }
6604
6875
  }
6605
6876
  });
6606
- onMount2(() => {
6877
+ onMount3(() => {
6607
6878
  if (!props.apiKey) {
6608
6879
  logger.error(
6609
6880
  "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
@@ -6943,7 +7214,7 @@ function ContentVariants(props) {
6943
7214
  canTrack: getDefaultCanTrack(props.canTrack)
6944
7215
  });
6945
7216
  }
6946
- onMount3(() => {
7217
+ onMount4(() => {
6947
7218
  setShouldRenderVariants(false);
6948
7219
  });
6949
7220
  return <>
@@ -7055,7 +7326,7 @@ function Symbol2(props) {
7055
7326
  }
7056
7327
  });
7057
7328
  }
7058
- onMount4(() => {
7329
+ onMount5(() => {
7059
7330
  setContent();
7060
7331
  });
7061
7332
  function onUpdateFn_0() {
@@ -7146,5 +7417,6 @@ export {
7146
7417
  isPreviewing,
7147
7418
  register,
7148
7419
  setEditorSettings,
7420
+ subscribeToEditor,
7149
7421
  track
7150
7422
  };