@builder.io/sdk-solid 0.12.5 → 0.12.7

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.
@@ -99,7 +99,7 @@ import { createContext as createContext2 } from "solid-js";
99
99
  var components_context_default = createContext2({ registeredComponents: {} });
100
100
 
101
101
  // src/components/block/block.tsx
102
- import { Show as Show4, For as For2, createSignal as createSignal4 } from "solid-js";
102
+ import { Show as Show4, For as For2, onMount, createSignal as createSignal4 } from "solid-js";
103
103
 
104
104
  // src/functions/get-block-component-options.ts
105
105
  function getBlockComponentOptions(block) {
@@ -229,27 +229,43 @@ var runInBrowser = ({
229
229
  builder,
230
230
  context,
231
231
  event,
232
- state: flattenState(rootState, localState, rootSetState)
232
+ state: flattenState({
233
+ rootState,
234
+ localState,
235
+ rootSetState
236
+ })
233
237
  });
234
238
  return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
235
239
  };
236
- function flattenState(rootState, localState, rootSetState) {
237
- if (rootState === localState) {
238
- throw new Error("rootState === localState");
239
- }
240
+ function flattenState({
241
+ rootState,
242
+ localState,
243
+ rootSetState
244
+ }) {
240
245
  return new Proxy(rootState, {
241
- get: (_, prop) => {
246
+ get: (target, prop) => {
242
247
  if (localState && prop in localState) {
243
248
  return localState[prop];
244
249
  }
245
- return rootState[prop];
250
+ const val = target[prop];
251
+ if (typeof val === "object") {
252
+ return flattenState({
253
+ rootState: val,
254
+ localState: void 0,
255
+ rootSetState: rootSetState ? (subState) => {
256
+ target[prop] = subState;
257
+ rootSetState(target);
258
+ } : void 0
259
+ });
260
+ }
261
+ return val;
246
262
  },
247
- set: (_, prop, value) => {
263
+ set: (target, prop, value) => {
248
264
  if (localState && prop in localState) {
249
265
  throw new Error("Writing to local state is not allowed as it is read-only.");
250
266
  }
251
- rootState[prop] = value;
252
- rootSetState?.(rootState);
267
+ target[prop] = value;
268
+ rootSetState?.(target);
253
269
  return true;
254
270
  }
255
271
  });
@@ -517,6 +533,211 @@ function getProcessedBlock({
517
533
  }
518
534
  }
519
535
 
536
+ // src/components/block/animator.ts
537
+ function throttle(func, wait, options = {}) {
538
+ let context;
539
+ let args;
540
+ let result;
541
+ let timeout = null;
542
+ let previous = 0;
543
+ const later = function() {
544
+ previous = options.leading === false ? 0 : Date.now();
545
+ timeout = null;
546
+ result = func.apply(context, args);
547
+ if (!timeout)
548
+ context = args = null;
549
+ };
550
+ return function() {
551
+ const now = Date.now();
552
+ if (!previous && options.leading === false)
553
+ previous = now;
554
+ const remaining = wait - (now - previous);
555
+ context = this;
556
+ args = arguments;
557
+ if (remaining <= 0 || remaining > wait) {
558
+ if (timeout) {
559
+ clearTimeout(timeout);
560
+ timeout = null;
561
+ }
562
+ previous = now;
563
+ result = func.apply(context, args);
564
+ if (!timeout)
565
+ context = args = null;
566
+ } else if (!timeout && options.trailing !== false) {
567
+ timeout = setTimeout(later, remaining);
568
+ }
569
+ return result;
570
+ };
571
+ }
572
+ function assign(target, ..._args) {
573
+ const to = Object(target);
574
+ for (let index = 1; index < arguments.length; index++) {
575
+ const nextSource = arguments[index];
576
+ if (nextSource != null) {
577
+ for (const nextKey in nextSource) {
578
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
579
+ to[nextKey] = nextSource[nextKey];
580
+ }
581
+ }
582
+ }
583
+ }
584
+ return to;
585
+ }
586
+ var camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
587
+ function bindAnimations(animations) {
588
+ for (const animation of animations) {
589
+ switch (animation.trigger) {
590
+ case "pageLoad":
591
+ triggerAnimation(animation);
592
+ break;
593
+ case "hover":
594
+ bindHoverAnimation(animation);
595
+ break;
596
+ case "scrollInView":
597
+ bindScrollInViewAnimation(animation);
598
+ break;
599
+ }
600
+ }
601
+ }
602
+ function warnElementNotPresent(id) {
603
+ }
604
+ function augmentAnimation(animation, element) {
605
+ const stylesUsed = getAllStylesUsed(animation);
606
+ const computedStyle = getComputedStyle(element);
607
+ const firstStyles = animation.steps[0].styles;
608
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
609
+ const bothStyles = [firstStyles, lastStyles];
610
+ for (const styles of bothStyles) {
611
+ for (const style of stylesUsed) {
612
+ if (!(style in styles)) {
613
+ styles[style] = computedStyle[style];
614
+ }
615
+ }
616
+ }
617
+ }
618
+ function getAllStylesUsed(animation) {
619
+ const properties = [];
620
+ for (const step of animation.steps) {
621
+ for (const key in step.styles) {
622
+ if (properties.indexOf(key) === -1) {
623
+ properties.push(key);
624
+ }
625
+ }
626
+ }
627
+ return properties;
628
+ }
629
+ function triggerAnimation(animation) {
630
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
631
+ if (!elements.length) {
632
+ warnElementNotPresent(animation.elementId || animation.id || "");
633
+ return;
634
+ }
635
+ Array.from(elements).forEach((element) => {
636
+ augmentAnimation(animation, element);
637
+ element.style.transition = "none";
638
+ element.style.transitionDelay = "0";
639
+ assign(element.style, animation.steps[0].styles);
640
+ setTimeout(() => {
641
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
642
+ if (animation.delay) {
643
+ element.style.transitionDelay = animation.delay + "s";
644
+ }
645
+ assign(element.style, animation.steps[1].styles);
646
+ setTimeout(() => {
647
+ element.style.transition = "";
648
+ element.style.transitionDelay = "";
649
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
650
+ });
651
+ });
652
+ }
653
+ function bindHoverAnimation(animation) {
654
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
655
+ if (!elements.length) {
656
+ warnElementNotPresent(animation.elementId || animation.id || "");
657
+ return;
658
+ }
659
+ Array.from(elements).forEach((element) => {
660
+ augmentAnimation(animation, element);
661
+ const defaultState = animation.steps[0].styles;
662
+ const hoverState = animation.steps[1].styles;
663
+ function attachDefaultState() {
664
+ assign(element.style, defaultState);
665
+ }
666
+ function attachHoverState() {
667
+ assign(element.style, hoverState);
668
+ }
669
+ attachDefaultState();
670
+ element.addEventListener("mouseenter", attachHoverState);
671
+ element.addEventListener("mouseleave", attachDefaultState);
672
+ setTimeout(() => {
673
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
674
+ if (animation.delay) {
675
+ element.style.transitionDelay = animation.delay + "s";
676
+ }
677
+ });
678
+ });
679
+ }
680
+ function bindScrollInViewAnimation(animation) {
681
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
682
+ if (!elements.length) {
683
+ warnElementNotPresent(animation.elementId || animation.id || "");
684
+ return;
685
+ }
686
+ Array.from(elements).forEach((element) => {
687
+ augmentAnimation(animation, element);
688
+ let triggered = false;
689
+ let pendingAnimation = false;
690
+ function immediateOnScroll() {
691
+ if (!triggered && isScrolledIntoView(element)) {
692
+ triggered = true;
693
+ pendingAnimation = true;
694
+ setTimeout(() => {
695
+ assign(element.style, animation.steps[1].styles);
696
+ if (!animation.repeat) {
697
+ document.removeEventListener("scroll", onScroll);
698
+ }
699
+ setTimeout(() => {
700
+ pendingAnimation = false;
701
+ if (!animation.repeat) {
702
+ element.style.transition = "";
703
+ element.style.transitionDelay = "";
704
+ }
705
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
706
+ });
707
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
708
+ triggered = false;
709
+ assign(element.style, animation.steps[0].styles);
710
+ }
711
+ }
712
+ const onScroll = throttle(immediateOnScroll, 200, {
713
+ leading: false
714
+ });
715
+ function isScrolledIntoView(elem) {
716
+ const rect = elem.getBoundingClientRect();
717
+ const windowHeight = window.innerHeight;
718
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
719
+ const threshold = thresholdPercent * windowHeight;
720
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
721
+ }
722
+ const defaultState = animation.steps[0].styles;
723
+ function attachDefaultState() {
724
+ assign(element.style, defaultState);
725
+ }
726
+ attachDefaultState();
727
+ setTimeout(() => {
728
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
729
+ if (animation.delay) {
730
+ element.style.transitionDelay = animation.delay + "s";
731
+ }
732
+ });
733
+ document.addEventListener("scroll", onScroll, {
734
+ capture: true,
735
+ passive: true
736
+ });
737
+ immediateOnScroll();
738
+ });
739
+ }
740
+
520
741
  // src/components/block/block.helpers.ts
521
742
  var getComponent = ({
522
743
  block,
@@ -1036,6 +1257,18 @@ function Block(props) {
1036
1257
  isInteractive: !blockComponent()?.isRSC
1037
1258
  };
1038
1259
  }
1260
+ onMount(() => {
1261
+ const blockId = processedBlock().id;
1262
+ const animations = processedBlock().animations;
1263
+ if (animations && blockId) {
1264
+ bindAnimations(
1265
+ animations.filter((item) => item.trigger !== "hover").map((animation) => ({
1266
+ ...animation,
1267
+ elementId: blockId
1268
+ }))
1269
+ );
1270
+ }
1271
+ });
1039
1272
  return <Show4 when={canShowBlock()}>
1040
1273
  <Block_styles_default block={props.block} context={props.context} />
1041
1274
  <Show4
@@ -1483,10 +1716,10 @@ function SectionComponent(props) {
1483
1716
  var section_default = SectionComponent;
1484
1717
 
1485
1718
  // src/blocks/symbol/symbol.tsx
1486
- import { onMount as onMount4, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
1719
+ import { onMount as onMount5, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
1487
1720
 
1488
1721
  // src/components/content-variants/content-variants.tsx
1489
- import { Show as Show11, For as For5, onMount as onMount3, createSignal as createSignal13 } from "solid-js";
1722
+ import { Show as Show11, For as For5, onMount as onMount4, createSignal as createSignal13 } from "solid-js";
1490
1723
 
1491
1724
  // src/helpers/url.ts
1492
1725
  var getTopLevelDomain = (host) => {
@@ -1964,12 +2197,12 @@ var componentInfo3 = {
1964
2197
  };
1965
2198
 
1966
2199
  // src/blocks/custom-code/custom-code.tsx
1967
- import { onMount, createSignal as createSignal7 } from "solid-js";
2200
+ import { onMount as onMount2, createSignal as createSignal7 } from "solid-js";
1968
2201
  function CustomCode(props) {
1969
2202
  const [scriptsInserted, setScriptsInserted] = createSignal7([]);
1970
2203
  const [scriptsRun, setScriptsRun] = createSignal7([]);
1971
2204
  let elementRef;
1972
- onMount(() => {
2205
+ onMount2(() => {
1973
2206
  if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
1974
2207
  return;
1975
2208
  }
@@ -2687,7 +2920,7 @@ function InlinedScript(props) {
2687
2920
  var Inlined_script_default = InlinedScript;
2688
2921
 
2689
2922
  // src/components/content/components/enable-editor.tsx
2690
- import { Show as Show9, onMount as onMount2, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
2923
+ import { Show as Show9, onMount as onMount3, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
2691
2924
  import { Dynamic as Dynamic5 } from "solid-js/web";
2692
2925
 
2693
2926
  // src/helpers/preview-lru-cache/get.ts
@@ -3177,7 +3410,7 @@ var getInteractionPropertiesForEvent = (event) => {
3177
3410
  };
3178
3411
 
3179
3412
  // src/constants/sdk-version.ts
3180
- var SDK_VERSION = "0.12.5";
3413
+ var SDK_VERSION = "0.12.7";
3181
3414
 
3182
3415
  // src/functions/register.ts
3183
3416
  var registry = {};
@@ -3380,6 +3613,10 @@ function EnableEditor(props) {
3380
3613
  setForceReRenderCount(forceReRenderCount() + 1);
3381
3614
  break;
3382
3615
  }
3616
+ case "builder.triggerAnimation": {
3617
+ triggerAnimation(data.data);
3618
+ break;
3619
+ }
3383
3620
  case "builder.contentUpdate": {
3384
3621
  const messageContent = data.data;
3385
3622
  const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
@@ -3476,7 +3713,7 @@ function EnableEditor(props) {
3476
3713
  }
3477
3714
  }
3478
3715
  let elementRef;
3479
- onMount2(() => {
3716
+ onMount3(() => {
3480
3717
  if (isBrowser()) {
3481
3718
  if (isEditing() && true) {
3482
3719
  setForceReRenderCount(forceReRenderCount() + 1);
@@ -3541,7 +3778,7 @@ function EnableEditor(props) {
3541
3778
  }
3542
3779
  }
3543
3780
  });
3544
- onMount2(() => {
3781
+ onMount3(() => {
3545
3782
  if (!props.apiKey) {
3546
3783
  logger.error(
3547
3784
  "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
@@ -3564,13 +3801,7 @@ function EnableEditor(props) {
3564
3801
  evaluateJsCode();
3565
3802
  }
3566
3803
  createEffect2(
3567
- on2(
3568
- () => [
3569
- props.builderContextSignal.content?.data?.jsCode,
3570
- props.builderContextSignal.rootState
3571
- ],
3572
- onUpdateFn_2
3573
- )
3804
+ on2(() => [props.builderContextSignal.content?.data?.jsCode], onUpdateFn_2)
3574
3805
  );
3575
3806
  function onUpdateFn_3() {
3576
3807
  runHttpRequests();
@@ -3887,7 +4118,7 @@ function ContentVariants(props) {
3887
4118
  canTrack: getDefaultCanTrack(props.canTrack)
3888
4119
  });
3889
4120
  }
3890
- onMount3(() => {
4121
+ onMount4(() => {
3891
4122
  setShouldRenderVariants(false);
3892
4123
  });
3893
4124
  return <>
@@ -3999,7 +4230,7 @@ function Symbol(props) {
3999
4230
  }
4000
4231
  });
4001
4232
  }
4002
- onMount4(() => {
4233
+ onMount5(() => {
4003
4234
  setContent();
4004
4235
  });
4005
4236
  function onUpdateFn_0() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-solid",
3
- "version": "0.12.5",
3
+ "version": "0.12.7",
4
4
  "description": "",
5
5
  "files": [
6
6
  "dist",