@builder.io/sdk-svelte 0.12.6 → 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.
@@ -4,6 +4,23 @@ interface JSONObject {
4
4
  }
5
5
  interface JSONArray extends Array<JSONValue> {
6
6
  }
7
+ export interface AnimationStep {
8
+ styles: {
9
+ [key: string]: string;
10
+ };
11
+ delay?: number;
12
+ }
13
+ export interface BuilderAnimation {
14
+ elementId: string;
15
+ trigger: string;
16
+ steps: AnimationStep[];
17
+ duration: number;
18
+ delay?: number;
19
+ easing?: string;
20
+ id?: string;
21
+ repeat?: boolean;
22
+ thresholdPercent?: number;
23
+ }
7
24
  /** @todo typedoc this */
8
25
  export interface BuilderBlock {
9
26
  '@type': '@builder.io/sdk:Element';
@@ -52,7 +69,7 @@ export interface BuilderBlock {
52
69
  collection: string;
53
70
  itemName?: string;
54
71
  } | null;
55
- animations?: any[];
72
+ animations?: BuilderAnimation[];
56
73
  style?: Partial<CSSStyleDeclaration>;
57
74
  href?: string;
58
75
  /**
@@ -0,0 +1,5 @@
1
+ import type { BuilderAnimation } from '../../types/builder-block.js';
2
+ export declare function bindAnimations(animations: BuilderAnimation[]): void;
3
+ export declare function triggerAnimation(animation: BuilderAnimation): void;
4
+ export declare function bindHoverAnimation(animation: BuilderAnimation): void;
5
+ export declare function bindScrollInViewAnimation(animation: BuilderAnimation): void;
@@ -0,0 +1,243 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-types
2
+ function throttle(func, wait, options = {}) {
3
+ let context;
4
+ let args;
5
+ let result;
6
+ let timeout = null;
7
+ let previous = 0;
8
+ const later = function () {
9
+ previous = options.leading === false ? 0 : Date.now();
10
+ timeout = null;
11
+ result = func.apply(context, args);
12
+ if (!timeout)
13
+ context = args = null;
14
+ };
15
+ return function () {
16
+ const now = Date.now();
17
+ if (!previous && options.leading === false)
18
+ previous = now;
19
+ const remaining = wait - (now - previous);
20
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
21
+ context = this;
22
+ // eslint-disable-next-line prefer-rest-params
23
+ args = arguments;
24
+ if (remaining <= 0 || remaining > wait) {
25
+ if (timeout) {
26
+ clearTimeout(timeout);
27
+ timeout = null;
28
+ }
29
+ previous = now;
30
+ result = func.apply(context, args);
31
+ if (!timeout)
32
+ context = args = null;
33
+ }
34
+ else if (!timeout && options.trailing !== false) {
35
+ timeout = setTimeout(later, remaining);
36
+ }
37
+ return result;
38
+ };
39
+ }
40
+ function assign(target, ..._args) {
41
+ const to = Object(target);
42
+ for (let index = 1; index < arguments.length; index++) {
43
+ // eslint-disable-next-line prefer-rest-params
44
+ const nextSource = arguments[index];
45
+ if (nextSource != null) {
46
+ // Skip over if undefined or null
47
+ for (const nextKey in nextSource) {
48
+ // Avoid bugs when hasOwnProperty is shadowed
49
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
50
+ to[nextKey] = nextSource[nextKey];
51
+ }
52
+ }
53
+ }
54
+ }
55
+ return to;
56
+ }
57
+ const camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`) : '';
58
+ export function bindAnimations(animations) {
59
+ for (const animation of animations) {
60
+ switch (animation.trigger) {
61
+ case 'pageLoad':
62
+ triggerAnimation(animation);
63
+ break;
64
+ case 'hover':
65
+ bindHoverAnimation(animation);
66
+ break;
67
+ case 'scrollInView':
68
+ bindScrollInViewAnimation(animation);
69
+ break;
70
+ }
71
+ }
72
+ }
73
+ function warnElementNotPresent(id) {
74
+ console.warn(`Cannot animate element: element with ID ${id} not found!`);
75
+ }
76
+ function augmentAnimation(animation, element) {
77
+ const stylesUsed = getAllStylesUsed(animation);
78
+ const computedStyle = getComputedStyle(element);
79
+ // const computedStyle = getComputedStyle(element);
80
+ // // FIXME: this will break if original load is in one reponsive size then resize to another hmmm
81
+ // Need to use transform instead of left since left can change on screen sizes
82
+ const firstStyles = animation.steps[0].styles;
83
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
84
+ const bothStyles = [firstStyles, lastStyles];
85
+ // FIXME: this won't work as expected for augmented animations - may need the editor itself to manage this
86
+ for (const styles of bothStyles) {
87
+ for (const style of stylesUsed) {
88
+ if (!(style in styles)) {
89
+ styles[style] = computedStyle[style];
90
+ }
91
+ }
92
+ }
93
+ }
94
+ function getAllStylesUsed(animation) {
95
+ const properties = [];
96
+ for (const step of animation.steps) {
97
+ for (const key in step.styles) {
98
+ if (properties.indexOf(key) === -1) {
99
+ properties.push(key);
100
+ }
101
+ }
102
+ }
103
+ return properties;
104
+ }
105
+ export function triggerAnimation(animation) {
106
+ // TODO: do for ALL elements
107
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ''));
108
+ if (!elements.length) {
109
+ warnElementNotPresent(animation.elementId || animation.id || '');
110
+ return;
111
+ }
112
+ Array.from(elements).forEach(element => {
113
+ augmentAnimation(animation, element);
114
+ // TODO: do this properly, may have other animations of different properties
115
+ // TODO: only override the properties
116
+ // TODO: if there is an entrance and hover animation, the transition duration will get effed
117
+ // element.setAttribute('style', '');
118
+ // const styledUsed = this.getAllStylesUsed(animation);
119
+ element.style.transition = 'none';
120
+ element.style.transitionDelay = '0';
121
+ assign(element.style, animation.steps[0].styles);
122
+ // TODO: queue/batch these timeouts
123
+ // TODO: only include properties explicitly set in the animation
124
+ // using Object.keys(styles)
125
+ setTimeout(() => {
126
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
127
+ if (animation.delay) {
128
+ element.style.transitionDelay = animation.delay + 's';
129
+ }
130
+ assign(element.style, animation.steps[1].styles);
131
+ // TODO: maybe remove/reset transitoin property after animation duration
132
+ // TODO: queue timers
133
+ setTimeout(() => {
134
+ // TODO: what if has other transition (reset back to what it was)
135
+ element.style.transition = '';
136
+ element.style.transitionDelay = '';
137
+ }, (animation.delay || 0) * 1000 + animation.duration * 1000 + 100);
138
+ });
139
+ });
140
+ }
141
+ export function bindHoverAnimation(animation) {
142
+ // TODO: is it multiple binding when editing...?
143
+ // TODO: unbind on element remove
144
+ // TODO: apply to ALL elements
145
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ''));
146
+ if (!elements.length) {
147
+ warnElementNotPresent(animation.elementId || animation.id || '');
148
+ return;
149
+ }
150
+ Array.from(elements).forEach(element => {
151
+ augmentAnimation(animation, element);
152
+ const defaultState = animation.steps[0].styles;
153
+ const hoverState = animation.steps[1].styles;
154
+ function attachDefaultState() {
155
+ assign(element.style, defaultState);
156
+ }
157
+ function attachHoverState() {
158
+ assign(element.style, hoverState);
159
+ }
160
+ attachDefaultState();
161
+ element.addEventListener('mouseenter', attachHoverState);
162
+ element.addEventListener('mouseleave', attachDefaultState);
163
+ // TODO: queue/batch these timeouts
164
+ setTimeout(() => {
165
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
166
+ if (animation.delay) {
167
+ element.style.transitionDelay = animation.delay + 's';
168
+ }
169
+ });
170
+ });
171
+ }
172
+ // TODO: unbind on element remove
173
+ export function bindScrollInViewAnimation(animation) {
174
+ // TODO: apply to ALL matching elements
175
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ''));
176
+ if (!elements.length) {
177
+ warnElementNotPresent(animation.elementId || animation.id || '');
178
+ return;
179
+ }
180
+ // TODO: if server side rendered and scrolled into view don't animate...
181
+ Array.from(elements).forEach(element => {
182
+ augmentAnimation(animation, element);
183
+ let triggered = false;
184
+ let pendingAnimation = false;
185
+ function immediateOnScroll() {
186
+ if (!triggered && isScrolledIntoView(element)) {
187
+ triggered = true;
188
+ pendingAnimation = true;
189
+ setTimeout(() => {
190
+ assign(element.style, animation.steps[1].styles);
191
+ if (!animation.repeat) {
192
+ document.removeEventListener('scroll', onScroll);
193
+ }
194
+ setTimeout(() => {
195
+ pendingAnimation = false;
196
+ if (!animation.repeat) {
197
+ element.style.transition = '';
198
+ element.style.transitionDelay = '';
199
+ }
200
+ }, (animation.duration + (animation.delay || 0)) * 1000 + 100);
201
+ });
202
+ }
203
+ else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
204
+ // we want to repeat the animation every time the the element is out of view and back again
205
+ triggered = false;
206
+ assign(element.style, animation.steps[0].styles);
207
+ }
208
+ }
209
+ // TODO: roll all of these in one for more efficiency of checking all the rects
210
+ const onScroll = throttle(immediateOnScroll, 200, {
211
+ leading: false
212
+ });
213
+ // TODO: fully in view or partially
214
+ function isScrolledIntoView(elem) {
215
+ const rect = elem.getBoundingClientRect();
216
+ const windowHeight = window.innerHeight;
217
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
218
+ const threshold = thresholdPercent * windowHeight;
219
+ // TODO: partial in view? or what if element is larger than screen itself
220
+ return rect.bottom > threshold && rect.top < windowHeight - threshold // Element is peeking top or bottom
221
+ ;
222
+ }
223
+ const defaultState = animation.steps[0].styles;
224
+ function attachDefaultState() {
225
+ assign(element.style, defaultState);
226
+ }
227
+ attachDefaultState();
228
+ // TODO: queue/batch these timeouts!
229
+ setTimeout(() => {
230
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
231
+ if (animation.delay) {
232
+ element.style.transitionDelay = animation.delay + 's';
233
+ }
234
+ });
235
+ // TODO: one listener for everything
236
+ document.addEventListener('scroll', onScroll, {
237
+ capture: true,
238
+ passive: true
239
+ });
240
+ // Do an initial check
241
+ immediateOnScroll();
242
+ });
243
+ }
@@ -1,10 +1,12 @@
1
1
  <script context="module"></script>
2
2
 
3
- <script>import { writable } from "svelte/store";
3
+ <script>import { onMount } from "svelte";
4
+ import { writable } from "svelte/store";
4
5
  import "../../functions/extract-text-styles.js";
5
6
  import { getBlockComponentOptions } from "../../functions/get-block-component-options.js";
6
7
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
7
8
  import "../../functions/get-style.js";
9
+ import { bindAnimations } from "./animator.js";
8
10
  import { getComponent, getRepeatItemData } from "./block.helpers.js";
9
11
  import BlockStyles from "./components/block-styles.svelte";
10
12
  import BlockWrapper from "./components/block-wrapper.svelte";
@@ -100,6 +102,18 @@ $: componentRefProps = () => {
100
102
  };
101
103
  };
102
104
  let childrenContext = writable($context);
105
+ onMount(() => {
106
+ const blockId = processedBlock().id;
107
+ const animations = processedBlock().animations;
108
+ if (animations && blockId) {
109
+ bindAnimations(animations
110
+ .filter((item) => item.trigger !== "hover")
111
+ .map((animation) => ({
112
+ ...animation,
113
+ elementId: blockId,
114
+ })));
115
+ }
116
+ });
103
117
  </script>
104
118
 
105
119
  {#if canShowBlock()}
@@ -17,6 +17,7 @@ import { getDefaultCanTrack } from "../../../helpers/canTrack.js";
17
17
  import { logger } from "../../../helpers/logger.js";
18
18
  import "../../../helpers/preview-lru-cache/set.js";
19
19
  import { registerInsertMenu, setupBrowserForEditing, } from "../../../scripts/init-editing.js";
20
+ import { triggerAnimation } from "../../block/animator.js";
20
21
  export let builderContextSignal;
21
22
  export let contentWrapper = undefined;
22
23
  export let trustedHosts = undefined;
@@ -83,6 +84,10 @@ function processMessage(event) {
83
84
  forceReRenderCount = forceReRenderCount + 1; // This is a hack to force Qwik to re-render.
84
85
  break;
85
86
  }
87
+ case "builder.triggerAnimation": {
88
+ triggerAnimation(data.data);
89
+ break;
90
+ }
86
91
  case "builder.contentUpdate": {
87
92
  const messageContent = data.data;
88
93
  const key = messageContent.key ||
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.12.6";
1
+ export declare const SDK_VERSION = "0.12.7";
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.12.6";
1
+ export const SDK_VERSION = "0.12.7";
@@ -4,6 +4,23 @@ interface JSONObject {
4
4
  }
5
5
  interface JSONArray extends Array<JSONValue> {
6
6
  }
7
+ export interface AnimationStep {
8
+ styles: {
9
+ [key: string]: string;
10
+ };
11
+ delay?: number;
12
+ }
13
+ export interface BuilderAnimation {
14
+ elementId: string;
15
+ trigger: string;
16
+ steps: AnimationStep[];
17
+ duration: number;
18
+ delay?: number;
19
+ easing?: string;
20
+ id?: string;
21
+ repeat?: boolean;
22
+ thresholdPercent?: number;
23
+ }
7
24
  /** @todo typedoc this */
8
25
  export interface BuilderBlock {
9
26
  '@type': '@builder.io/sdk:Element';
@@ -52,7 +69,7 @@ export interface BuilderBlock {
52
69
  collection: string;
53
70
  itemName?: string;
54
71
  } | null;
55
- animations?: any[];
72
+ animations?: BuilderAnimation[];
56
73
  style?: Partial<CSSStyleDeclaration>;
57
74
  href?: string;
58
75
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.12.6",
4
+ "version": "0.12.7",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "lib",
@@ -0,0 +1,254 @@
1
+ import type { BuilderAnimation } from '../../types/builder-block.js';
2
+
3
+ // eslint-disable-next-line @typescript-eslint/ban-types
4
+ function throttle(func: Function, wait: number, options: any = {}) {
5
+ let context: any;
6
+ let args: any;
7
+ let result: any;
8
+ let timeout = (null as any);
9
+ let previous = 0;
10
+ const later = function () {
11
+ previous = options.leading === false ? 0 : Date.now();
12
+ timeout = null;
13
+ result = func.apply(context, args);
14
+ if (!timeout) context = args = null;
15
+ };
16
+ return function (this: any) {
17
+ const now = Date.now();
18
+ if (!previous && options.leading === false) previous = now;
19
+ const remaining = wait - (now - previous);
20
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
21
+ context = this;
22
+ // eslint-disable-next-line prefer-rest-params
23
+ args = arguments;
24
+ if (remaining <= 0 || remaining > wait) {
25
+ if (timeout) {
26
+ clearTimeout(timeout);
27
+ timeout = null;
28
+ }
29
+ previous = now;
30
+ result = func.apply(context, args);
31
+ if (!timeout) context = args = null;
32
+ } else if (!timeout && options.trailing !== false) {
33
+ timeout = setTimeout(later, remaining);
34
+ }
35
+ return result;
36
+ };
37
+ }
38
+ function assign(target: object, ..._args: any[]) {
39
+ const to = Object(target);
40
+ for (let index = 1; index < arguments.length; index++) {
41
+ // eslint-disable-next-line prefer-rest-params
42
+ const nextSource = arguments[index];
43
+ if (nextSource != null) {
44
+ // Skip over if undefined or null
45
+ for (const nextKey in nextSource) {
46
+ // Avoid bugs when hasOwnProperty is shadowed
47
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
48
+ to[nextKey] = nextSource[nextKey];
49
+ }
50
+ }
51
+ }
52
+ }
53
+ return to;
54
+ }
55
+ const camelCaseToKebabCase = (str?: string) => str ? str.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`) : '';
56
+ export function bindAnimations(animations: BuilderAnimation[]) {
57
+ for (const animation of animations) {
58
+ switch (animation.trigger) {
59
+ case 'pageLoad':
60
+ triggerAnimation(animation);
61
+ break;
62
+ case 'hover':
63
+ bindHoverAnimation(animation);
64
+ break;
65
+ case 'scrollInView':
66
+ bindScrollInViewAnimation(animation);
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ function warnElementNotPresent(id: string) {
72
+ console.warn(`Cannot animate element: element with ID ${id} not found!`);
73
+ }
74
+ function augmentAnimation(animation: BuilderAnimation, element: HTMLElement) {
75
+ const stylesUsed = getAllStylesUsed(animation);
76
+ const computedStyle: any = getComputedStyle(element);
77
+ // const computedStyle = getComputedStyle(element);
78
+ // // FIXME: this will break if original load is in one reponsive size then resize to another hmmm
79
+ // Need to use transform instead of left since left can change on screen sizes
80
+ const firstStyles = animation.steps[0].styles;
81
+ const lastStyles = animation.steps[animation.steps.length - 1]!.styles;
82
+ const bothStyles = [firstStyles, lastStyles];
83
+
84
+ // FIXME: this won't work as expected for augmented animations - may need the editor itself to manage this
85
+ for (const styles of bothStyles) {
86
+ for (const style of stylesUsed) {
87
+ if (!(style in styles)) {
88
+ styles[(style as any)] = computedStyle[style];
89
+ }
90
+ }
91
+ }
92
+ }
93
+ function getAllStylesUsed(animation: BuilderAnimation) {
94
+ const properties: (keyof CSSStyleDeclaration)[] = [];
95
+ for (const step of animation.steps) {
96
+ for (const key in step.styles) {
97
+ if (properties.indexOf((key as any)) === -1) {
98
+ properties.push((key as any));
99
+ }
100
+ }
101
+ }
102
+ return properties;
103
+ }
104
+ export function triggerAnimation(animation: BuilderAnimation) {
105
+ // TODO: do for ALL elements
106
+ const elements = (Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || '')) as HTMLElement[]);
107
+ if (!elements.length) {
108
+ warnElementNotPresent(animation.elementId || animation.id || '');
109
+ return;
110
+ }
111
+ Array.from(elements).forEach(element => {
112
+ augmentAnimation(animation, element);
113
+ // TODO: do this properly, may have other animations of different properties
114
+
115
+ // TODO: only override the properties
116
+ // TODO: if there is an entrance and hover animation, the transition duration will get effed
117
+ // element.setAttribute('style', '');
118
+
119
+ // const styledUsed = this.getAllStylesUsed(animation);
120
+ element.style.transition = 'none';
121
+ element.style.transitionDelay = '0';
122
+ assign(element.style, animation.steps[0].styles);
123
+ // TODO: queue/batch these timeouts
124
+ // TODO: only include properties explicitly set in the animation
125
+ // using Object.keys(styles)
126
+ setTimeout(() => {
127
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
128
+ if (animation.delay) {
129
+ element.style.transitionDelay = animation.delay + 's';
130
+ }
131
+ assign(element.style, animation.steps[1].styles);
132
+ // TODO: maybe remove/reset transitoin property after animation duration
133
+
134
+ // TODO: queue timers
135
+ setTimeout(() => {
136
+ // TODO: what if has other transition (reset back to what it was)
137
+ element.style.transition = '';
138
+ element.style.transitionDelay = '';
139
+ }, (animation.delay || 0) * 1000 + animation.duration * 1000 + 100);
140
+ });
141
+ });
142
+ }
143
+ export function bindHoverAnimation(animation: BuilderAnimation) {
144
+ // TODO: is it multiple binding when editing...?
145
+ // TODO: unbind on element remove
146
+ // TODO: apply to ALL elements
147
+ const elements = (Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || '')) as HTMLElement[]);
148
+ if (!elements.length) {
149
+ warnElementNotPresent(animation.elementId || animation.id || '');
150
+ return;
151
+ }
152
+ Array.from(elements).forEach(element => {
153
+ augmentAnimation(animation, element);
154
+ const defaultState = animation.steps[0].styles;
155
+ const hoverState = animation.steps[1].styles;
156
+ function attachDefaultState() {
157
+ assign(element!.style, defaultState);
158
+ }
159
+ function attachHoverState() {
160
+ assign(element!.style, hoverState);
161
+ }
162
+ attachDefaultState();
163
+ element.addEventListener('mouseenter', attachHoverState);
164
+ element.addEventListener('mouseleave', attachDefaultState);
165
+ // TODO: queue/batch these timeouts
166
+ setTimeout(() => {
167
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
168
+ if (animation.delay) {
169
+ element.style.transitionDelay = animation.delay + 's';
170
+ }
171
+ });
172
+ });
173
+ }
174
+
175
+ // TODO: unbind on element remove
176
+ export function bindScrollInViewAnimation(animation: BuilderAnimation) {
177
+ // TODO: apply to ALL matching elements
178
+ const elements = (Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || '')) as HTMLElement[]);
179
+ if (!elements.length) {
180
+ warnElementNotPresent(animation.elementId || animation.id || '');
181
+ return;
182
+ }
183
+
184
+ // TODO: if server side rendered and scrolled into view don't animate...
185
+ Array.from(elements).forEach(element => {
186
+ augmentAnimation(animation, element);
187
+ let triggered = false;
188
+ let pendingAnimation = false;
189
+ function immediateOnScroll() {
190
+ if (!triggered && isScrolledIntoView(element)) {
191
+ triggered = true;
192
+ pendingAnimation = true;
193
+ setTimeout(() => {
194
+ assign(element!.style, animation.steps[1].styles);
195
+ if (!animation.repeat) {
196
+ document.removeEventListener('scroll', onScroll);
197
+ }
198
+ setTimeout(() => {
199
+ pendingAnimation = false;
200
+ if (!animation.repeat) {
201
+ element.style.transition = '';
202
+ element.style.transitionDelay = '';
203
+ }
204
+ }, (animation.duration + (animation.delay || 0)) * 1000 + 100);
205
+ });
206
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
207
+ // we want to repeat the animation every time the the element is out of view and back again
208
+ triggered = false;
209
+ assign(element!.style, animation.steps[0].styles);
210
+ }
211
+ }
212
+
213
+ // TODO: roll all of these in one for more efficiency of checking all the rects
214
+ const onScroll = throttle(immediateOnScroll, 200, {
215
+ leading: false
216
+ });
217
+
218
+ // TODO: fully in view or partially
219
+ function isScrolledIntoView(elem: HTMLElement) {
220
+ const rect = elem.getBoundingClientRect();
221
+ const windowHeight = window.innerHeight;
222
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
223
+ const threshold = thresholdPercent * windowHeight;
224
+
225
+ // TODO: partial in view? or what if element is larger than screen itself
226
+ return rect.bottom > threshold && rect.top < windowHeight - threshold // Element is peeking top or bottom
227
+ // (rect.top > 0 && rect.bottom < window.innerHeight) || // element fits within the screen and is fully on screen (not hanging off at all)
228
+ // (rect.top < 0 && rect.bottom > window.innerHeight) // element is larger than the screen and hangs over the top and bottom
229
+ ;
230
+ }
231
+ const defaultState = animation.steps[0].styles;
232
+ function attachDefaultState() {
233
+ assign(element!.style, defaultState);
234
+ }
235
+ attachDefaultState();
236
+
237
+ // TODO: queue/batch these timeouts!
238
+ setTimeout(() => {
239
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
240
+ if (animation.delay) {
241
+ element.style.transitionDelay = animation.delay + 's';
242
+ }
243
+ });
244
+
245
+ // TODO: one listener for everything
246
+ document.addEventListener('scroll', onScroll, ({
247
+ capture: true,
248
+ passive: true
249
+ } as any));
250
+
251
+ // Do an initial check
252
+ immediateOnScroll();
253
+ });
254
+ }
@@ -8,6 +8,7 @@
8
8
  </script>
9
9
 
10
10
  <script lang="ts">
11
+ import { onMount } from "svelte";
11
12
  import { writable } from "svelte/store";
12
13
 
13
14
  import type {
@@ -19,6 +20,7 @@
19
20
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
20
21
  import { getStyle } from "../../functions/get-style.js";
21
22
  import type { BuilderBlock } from "../../types/builder-block.js";
23
+ import { bindAnimations } from "./animator.js";
22
24
  import { getComponent, getRepeatItemData } from "./block.helpers.js";
23
25
  import BlockStyles from "./components/block-styles.svelte";
24
26
  import BlockWrapper from "./components/block-wrapper.svelte";
@@ -122,6 +124,21 @@
122
124
  };
123
125
 
124
126
  let childrenContext = writable($context);
127
+
128
+ onMount(() => {
129
+ const blockId = processedBlock().id;
130
+ const animations = processedBlock().animations;
131
+ if (animations && blockId) {
132
+ bindAnimations(
133
+ animations
134
+ .filter((item) => item.trigger !== "hover")
135
+ .map((animation) => ({
136
+ ...animation,
137
+ elementId: blockId,
138
+ }))
139
+ );
140
+ }
141
+ });
125
142
  </script>
126
143
 
127
144
  {#if canShowBlock()}