@gem-sdk/core 1.30.18 → 1.31.0

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.
@@ -7,25 +7,32 @@ var react = require('react');
7
7
  var composeAdvanceStyle = require('../helpers/compose-advance-style.js');
8
8
  var constant = require('./constant.js');
9
9
  var RenderCustomCode = require('./RenderCustomCode.js');
10
+ var AnimationWrapper = require('./animation/AnimationWrapper.js');
10
11
 
11
12
  const ComponentWrapper = ({ children, ...props })=>{
12
13
  if (props.type === 'section') return null;
13
14
  const style = composeAdvanceStyle.composeAdvanceStyle(props.advanced, props.tag);
14
15
  const advanced = props.advanced;
16
+ const { animation } = advanced ?? {};
17
+ const enableAnimation = animation?.desktop?.enabled || animation?.tablet?.enabled || animation?.mobile?.enabled;
18
+ let dynamicChildren = /*#__PURE__*/ jsxRuntime.jsx("div", {
19
+ style: style,
20
+ className: `${props.uid} ${advanced?.cssClass}`,
21
+ children: children
22
+ });
15
23
  if (constant.disableWrap.includes(props.tag) && /*#__PURE__*/ react.isValidElement(children)) {
16
- return /*#__PURE__*/ jsxRuntime.jsxs(jsxRuntime.Fragment, {
17
- children: [
18
- /*#__PURE__*/ jsxRuntime.jsx(RenderCustomCode.default, {
19
- uid: props.uid,
20
- advanced: advanced
21
- }),
22
- /*#__PURE__*/ jsxRuntime.jsx(children.type, {
23
- className: advanced?.cssClass,
24
- ...children.props,
25
- style,
26
- advanced
27
- })
28
- ]
24
+ dynamicChildren = /*#__PURE__*/ jsxRuntime.jsx(children.type, {
25
+ className: advanced?.cssClass,
26
+ ...children.props,
27
+ style,
28
+ advanced
29
+ });
30
+ }
31
+ if (enableAnimation) {
32
+ dynamicChildren = /*#__PURE__*/ jsxRuntime.jsx(AnimationWrapper.default, {
33
+ children: dynamicChildren,
34
+ settings: animation,
35
+ tag: props.tag
29
36
  });
30
37
  }
31
38
  return /*#__PURE__*/ jsxRuntime.jsxs(jsxRuntime.Fragment, {
@@ -34,11 +41,7 @@ const ComponentWrapper = ({ children, ...props })=>{
34
41
  uid: props.uid,
35
42
  advanced: advanced
36
43
  }),
37
- /*#__PURE__*/ jsxRuntime.jsx("div", {
38
- style: style,
39
- className: `${props.uid} ${advanced?.cssClass}`,
40
- children: children
41
- })
44
+ dynamicChildren
42
45
  ]
43
46
  });
44
47
  };
@@ -14,50 +14,47 @@ const Render = ({ uid, ...passProps })=>{
14
14
  const item = BuilderContext.useBuilderStore((s)=>s.getItem(uid));
15
15
  const Component = BuilderComponent.useBuilderComponent(item?.tag);
16
16
  if (!item) return null;
17
- if (item?.type === 'section') {
18
- return /*#__PURE__*/ jsxRuntime.jsx(RenderSection.default, {
19
- sectionId: item.uid
20
- });
21
- } else {
22
- return /*#__PURE__*/ jsxRuntime.jsx(reactErrorBoundary.ErrorBoundary, {
23
- fallbackRender: ({ error, resetErrorBoundary })=>/*#__PURE__*/ jsxRuntime.jsxs(jsxRuntime.Fragment, {
24
- children: [
25
- /*#__PURE__*/ jsxRuntime.jsx("span", {
26
- "aria-label": "Error message",
27
- children: error.message
28
- }),
29
- /*#__PURE__*/ jsxRuntime.jsx("button", {
30
- type: "button",
31
- onClick: resetErrorBoundary,
32
- children: "Try again"
33
- })
34
- ]
35
- }),
36
- children: /*#__PURE__*/ jsxRuntime.jsx(ComponentWrapper.default, {
37
- ...item,
38
- children: item?.childrens?.length ? /*#__PURE__*/ jsxRuntime.jsx(Component, {
39
- builderProps: {
40
- uid,
41
- builderData: item
42
- },
43
- styles: item.styles,
44
- setting: item.settings,
45
- ...passProps,
46
- children: item.childrens.map((id)=>/*#__PURE__*/ jsxRuntime.jsx(RenderMemo, {
47
- uid: id
48
- }, id))
49
- }) : /*#__PURE__*/ jsxRuntime.jsx(Component, {
50
- builderProps: {
51
- uid,
52
- builderData: item
53
- },
54
- styles: item.styles,
55
- setting: item.settings,
56
- ...passProps
57
- })
17
+ if (item?.type === 'section') return /*#__PURE__*/ jsxRuntime.jsx(RenderSection.default, {
18
+ sectionId: item.uid
19
+ });
20
+ return /*#__PURE__*/ jsxRuntime.jsx(reactErrorBoundary.ErrorBoundary, {
21
+ fallbackRender: ({ error, resetErrorBoundary })=>/*#__PURE__*/ jsxRuntime.jsxs(jsxRuntime.Fragment, {
22
+ children: [
23
+ /*#__PURE__*/ jsxRuntime.jsx("span", {
24
+ "aria-label": "Error message",
25
+ children: error.message
26
+ }),
27
+ /*#__PURE__*/ jsxRuntime.jsx("button", {
28
+ type: "button",
29
+ onClick: resetErrorBoundary,
30
+ children: "Try again"
31
+ })
32
+ ]
33
+ }),
34
+ children: /*#__PURE__*/ jsxRuntime.jsx(ComponentWrapper.default, {
35
+ ...item,
36
+ children: item?.childrens?.length ? /*#__PURE__*/ jsxRuntime.jsx(Component, {
37
+ builderProps: {
38
+ uid,
39
+ builderData: item
40
+ },
41
+ styles: item.styles,
42
+ setting: item.settings,
43
+ ...passProps,
44
+ children: item.childrens.map((id)=>/*#__PURE__*/ jsxRuntime.jsx(RenderMemo, {
45
+ uid: id
46
+ }, id))
47
+ }) : /*#__PURE__*/ jsxRuntime.jsx(Component, {
48
+ builderProps: {
49
+ uid,
50
+ builderData: item
51
+ },
52
+ styles: item.styles,
53
+ setting: item.settings,
54
+ ...passProps
58
55
  })
59
- }, item.uid);
60
- }
56
+ })
57
+ }, item.uid);
61
58
  };
62
59
  const RenderMemo = /*#__PURE__*/ react.memo(Render);
63
60
  RenderMemo.displayName = 'RenderMemo';
@@ -225,7 +225,8 @@ const appendAnimation = (props, liquid)=>{
225
225
  mobile: getInitOpacity('mobile')
226
226
  };
227
227
  const inheritParentStyle = {
228
- all: tag === 'Section' ? '' : 'inherit'
228
+ all: tag === 'Section' ? '' : 'inherit',
229
+ position: 'static'
229
230
  };
230
231
  return render.template`
231
232
  ${render.RenderIf(convert.isLocalEnv, `<script src="{{ 'gp-animation.js' | asset_url }}" defer="defer"></script>`, `<script src="${convert.baseAssetURL}/assets/gp-animation.js" defer="defer"></script>`)}
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var makeStyle = require('../../helpers/make-style.js');
7
+ var react = require('react');
8
+ require('zustand');
9
+ require('swr');
10
+ require('@gem-sdk/adapter-shopify');
11
+ require('swr/mutation');
12
+ require('vanilla-lazyload');
13
+ require('../../hooks/useCartUI.js');
14
+ require('react-transition-group');
15
+ require('@gem-sdk/core');
16
+ require('../../helpers/convert.js');
17
+ var useLivePageAnimation = require('../../hooks/animation/useLivePageAnimation.js');
18
+
19
+ const AnimationWrapper = ({ children, settings, tag })=>{
20
+ const wrapperRef = react.useRef(null);
21
+ const getAnimationType = (settings, type)=>{
22
+ return settings?.triggerConfig?.[type]?.animation ?? 'none';
23
+ };
24
+ const getSettingsByDevice = (device)=>{
25
+ if (device === 'desktop') return settings?.desktop;
26
+ else if (device === 'tablet') return settings?.tablet ?? settings?.desktop;
27
+ else if (device === 'mobile') return settings?.mobile ?? settings?.tablet ?? settings?.desktop;
28
+ return settings?.desktop;
29
+ };
30
+ const getInitOpacity = (device)=>+!(getSettingsByDevice(device)?.enabled && ![
31
+ 'shake',
32
+ 'none'
33
+ ].includes(getAnimationType(getSettingsByDevice(device), 'appear')));
34
+ const opacityObject = {
35
+ desktop: getInitOpacity('desktop'),
36
+ tablet: getInitOpacity('tablet'),
37
+ mobile: getInitOpacity('mobile')
38
+ };
39
+ // const inheritParentStyle = { all: tag === 'Section' ? '' : 'inherit' };
40
+ useLivePageAnimation.useLivePageAnimation(wrapperRef, settings, tag);
41
+ return /*#__PURE__*/ jsxRuntime.jsx("div", {
42
+ ref: wrapperRef,
43
+ style: {
44
+ ...makeStyle.makeStyleResponsive('op', opacityObject)
45
+ },
46
+ children: children
47
+ });
48
+ };
49
+
50
+ exports.default = AnimationWrapper;
@@ -61,7 +61,7 @@ const composeTypographyV2 = (value, attrs)=>{
61
61
  return makeStyle.removeNullUndefined({
62
62
  ...makeStyle.makeStyle({
63
63
  fs: !attrs?.italic ? value?.fontStyle : undefined,
64
- ff: value?.fontFamily ? `var(--g-font-${value.fontFamily.replace(/ /g, '-')}, ${value.fontFamily})` : undefined,
64
+ ff: value?.fontFamily ? `var(--g-font-${value.fontFamily.replace(/ /g, '-')}, '${value.fontFamily}')` : undefined,
65
65
  weight: !attrs?.bold ? value?.fontWeight : undefined,
66
66
  ls: value?.letterSpacing,
67
67
  lh: value?.lineHeight
@@ -0,0 +1,227 @@
1
+ 'use strict';
2
+
3
+ // Any logic changes made to this file must also be applied to
4
+ // packages/web-components/src/helpers/animations.ts, and vice versa.
5
+ const EASING = {
6
+ ease: 'cubic-bezier(0.46,0.03,0.52,0.96)',
7
+ 'ease-in': 'cubic-bezier(0.55,0.08,0.68,0.53)',
8
+ 'ease-out': 'cubic-bezier(0.46,0.03,0.52,0.96)',
9
+ linear: 'linear'
10
+ };
11
+ const animations = ()=>{
12
+ const normalizeOptions = (options)=>{
13
+ const cloneOptions = JSON.parse(JSON.stringify(options));
14
+ const numberMember = [
15
+ 'delay',
16
+ 'speed',
17
+ 'scale',
18
+ 'intensity'
19
+ ];
20
+ numberMember.forEach((keyName)=>{
21
+ if (!cloneOptions?.[keyName]) return;
22
+ if (keyName === 'scale') {
23
+ if (!cloneOptions?.zoomDirection) throw new TypeError(`zoomDirection not found on zoom preset`);
24
+ const [i1, i2] = cloneOptions?.scale?.in ?? [
25
+ 1,
26
+ 1
27
+ ];
28
+ const [o1, o2] = cloneOptions?.scale?.out ?? [
29
+ 1,
30
+ 1
31
+ ];
32
+ cloneOptions[keyName] = {
33
+ in: [
34
+ Number(i1) / 100,
35
+ Number(i2) / 100
36
+ ],
37
+ out: [
38
+ Number(o1) / 100,
39
+ Number(o2) / 100
40
+ ]
41
+ };
42
+ return;
43
+ }
44
+ const num = Number(cloneOptions?.[keyName]);
45
+ if (keyName === 'delay') cloneOptions[keyName] = num * 1000;
46
+ else cloneOptions[keyName] = num;
47
+ if (!isFinite(num)) throw new TypeError(`${keyName} must be a number`);
48
+ });
49
+ return cloneOptions;
50
+ };
51
+ const slide = (target, options)=>{
52
+ const normalizedOptions = normalizeOptions(options);
53
+ const { direction, distance } = normalizedOptions;
54
+ const coordinate = [
55
+ 'left',
56
+ 'right'
57
+ ].includes(direction ?? '') ? 'X' : 'Y';
58
+ const isNeg = [
59
+ 'left',
60
+ 'down'
61
+ ].includes(direction ?? '') ? '-' : '';
62
+ const preset = [
63
+ {
64
+ opacity: 0,
65
+ transform: `translate${coordinate}(${isNeg}${distance ?? 50}%)`
66
+ },
67
+ {
68
+ opacity: 1,
69
+ transform: `translate${coordinate}(0)`
70
+ }
71
+ ];
72
+ return generateAnimationInstance(target, preset, {
73
+ ...generateOptions(normalizedOptions, 500),
74
+ fill: 'forwards'
75
+ });
76
+ };
77
+ const fade = (target, options)=>{
78
+ const normalizedOptions = normalizeOptions(options);
79
+ const preset = [
80
+ {
81
+ opacity: 0
82
+ },
83
+ {
84
+ opacity: 1
85
+ }
86
+ ];
87
+ return generateAnimationInstance(target, preset, {
88
+ ...generateOptions(normalizedOptions, 500)
89
+ });
90
+ };
91
+ const generateOptions = (options, defaultDuration)=>{
92
+ const convertSpeedToDuration = (speed)=>{
93
+ if (!speed) return defaultDuration;
94
+ return 1.5 / speed * 1000;
95
+ };
96
+ const { loop, delay, speed, easing } = options ?? {};
97
+ const duration = convertSpeedToDuration(speed);
98
+ const actualDelay = delay ?? 0;
99
+ const actualDuration = loop ? duration + actualDelay : duration;
100
+ return {
101
+ iterations: loop ? Infinity : 1,
102
+ duration: actualDuration,
103
+ delay: actualDelay,
104
+ easing: EASING[easing ?? 'linear']
105
+ };
106
+ };
107
+ const zoom = (target, options)=>{
108
+ const normalizedOptions = normalizeOptions(options);
109
+ const { scale, zoomDirection, isFade } = normalizedOptions;
110
+ const [i1, i2] = scale.in;
111
+ const [o1, o2] = scale.out;
112
+ const zoomInPreset = [
113
+ {
114
+ transform: `scale(${i1}, ${i1})`,
115
+ opacity: isFade ? 0 : 1
116
+ },
117
+ {
118
+ transform: `scale(${i2 ?? 1.2}, ${i2 ?? 1.2})`,
119
+ opacity: 1
120
+ }
121
+ ];
122
+ const zoomOutPreset = [
123
+ {
124
+ transform: `scale(${o1}, ${o1})`,
125
+ opacity: isFade ? 0 : 1
126
+ },
127
+ {
128
+ transform: `scale(${o2 ?? 0.8}, ${o2 ?? 0.8})`,
129
+ opacity: 1
130
+ }
131
+ ];
132
+ const zoomPreset = zoomDirection === 'in' ? zoomInPreset : zoomOutPreset;
133
+ return generateAnimationInstance(target, zoomPreset, {
134
+ ...generateOptions(normalizedOptions, 700),
135
+ fill: 'forwards'
136
+ });
137
+ };
138
+ const generateLoopDelayEffect = (keyframes, delay, duration)=>{
139
+ if (!delay) return keyframes;
140
+ const offset = delay / duration;
141
+ const keyframe = [
142
+ ...keyframes
143
+ ].pop();
144
+ return [
145
+ ...keyframes,
146
+ {
147
+ ...keyframe,
148
+ offset: 1 - offset
149
+ }
150
+ ];
151
+ };
152
+ const shake = (target, options)=>{
153
+ const normalizedOptions = normalizeOptions(options);
154
+ const { intensity } = normalizedOptions ?? {};
155
+ const zeroIntensity = [
156
+ {
157
+ transform: 'translate3d(0, 0, 0)',
158
+ opacity: 1
159
+ }
160
+ ];
161
+ const unitIntensity = [
162
+ {
163
+ transform: 'translate3d(4px, 0, 0)',
164
+ opacity: 1
165
+ },
166
+ {
167
+ transform: 'translate3d(-4px, 0, 0)',
168
+ opacity: 1
169
+ },
170
+ {
171
+ transform: 'translate3d(4px, 0, 0)',
172
+ opacity: 1
173
+ }
174
+ ];
175
+ let keyframes = [];
176
+ Array.from(Array(intensity ?? 1).keys()).forEach(()=>{
177
+ keyframes = [
178
+ ...keyframes,
179
+ ...unitIntensity
180
+ ];
181
+ });
182
+ const shakePreset = [
183
+ {
184
+ transform: 'translate3d(-1px, 0, 0)',
185
+ opacity: 1
186
+ },
187
+ {
188
+ transform: 'translate3d(2px, 0, 0)',
189
+ opacity: 1
190
+ },
191
+ {
192
+ transform: 'translate3d(-4px, 0, 0)',
193
+ opacity: 1
194
+ },
195
+ ...keyframes,
196
+ {
197
+ transform: 'translate3d(-4px, 0, 0)',
198
+ opacity: 1
199
+ },
200
+ {
201
+ transform: 'translate3d(2px, 0, 0)',
202
+ opacity: 1
203
+ },
204
+ {
205
+ transform: 'translate3d(-1px, 0, 0)',
206
+ opacity: 1
207
+ }
208
+ ];
209
+ return generateAnimationInstance(target, intensity ? shakePreset : zeroIntensity, {
210
+ ...generateOptions(normalizedOptions ?? {}, 820)
211
+ });
212
+ };
213
+ const generateAnimationInstance = (target, effectPreset, options)=>{
214
+ const effect = new KeyframeEffect(target, options.iterations && options.iterations === 1 ? effectPreset : generateLoopDelayEffect(effectPreset, options?.delay ?? 0, (options?.duration) ?? 0), {
215
+ ...options
216
+ });
217
+ return new Animation(effect, document.timeline);
218
+ };
219
+ return {
220
+ zoom,
221
+ shake,
222
+ fade,
223
+ slide
224
+ };
225
+ };
226
+
227
+ exports.animations = animations;
@@ -0,0 +1,230 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var animations = require('./animations.js');
5
+
6
+ // Any logic changes made to this file must also be applied to
7
+ // packages/web-components/src/gp-animation.ts, and vice versa.
8
+ const TAGS_SELECTOR = {
9
+ Button: '.gp-button-base',
10
+ Sticky: 'gp-sticky',
11
+ Dialog: 'div[role=dialog]',
12
+ Icon: 'div div',
13
+ ProductTag: 'div div div'
14
+ };
15
+ const useLivePageAnimation = (wrapperRef, settings, tag)=>{
16
+ const animation = animations.animations();
17
+ const [isAppearLoop, setIsAppearLoop] = react.useState(false);
18
+ const observer = react.useRef(undefined);
19
+ const appearAfterHoverTimer = react.useRef(undefined);
20
+ const animationElement = react.useRef(null);
21
+ const animationAppearType = react.useRef('none');
22
+ const animationHoverType = react.useRef('none');
23
+ const animationAppearInstance = react.useRef(null);
24
+ const animationHoverInstance = react.useRef(null);
25
+ const currentDevice = react.useRef(null);
26
+ const wrapperId = react.useRef('animation-wrapper-');
27
+ const appearPlay = react.useCallback((rate)=>{
28
+ if (!animationAppearInstance.current) return;
29
+ animationAppearInstance.current.playbackRate = rate ?? 1;
30
+ animationAppearInstance.current.play();
31
+ }, [
32
+ animationAppearInstance
33
+ ]);
34
+ const hoverPlay = react.useCallback((rate)=>{
35
+ if (!animationHoverInstance.current) return;
36
+ animationHoverInstance.current.playbackRate = rate ?? 1;
37
+ animationHoverInstance.current.play();
38
+ }, [
39
+ animationHoverInstance
40
+ ]);
41
+ const removeZeroOpacity = react.useCallback(()=>{
42
+ if (wrapperRef.current) wrapperRef.current.style.opacity = '1';
43
+ if (animationElement.current) animationElement.current.style.opacity = '1';
44
+ }, [
45
+ wrapperRef
46
+ ]);
47
+ const applyInitOpacity = react.useCallback(()=>{
48
+ if (wrapperRef.current) wrapperRef.current.style.opacity = '1';
49
+ if (animationElement.current) animationElement.current.style.opacity = '0';
50
+ }, [
51
+ wrapperRef
52
+ ]);
53
+ const playAppear = react.useCallback((e)=>{
54
+ e && e.stopPropagation();
55
+ applyInitOpacity();
56
+ if (!animationAppearInstance.current) return;
57
+ animationHoverInstance.current?.cancel();
58
+ appearPlay();
59
+ animationAppearInstance.current.finished.then(()=>{
60
+ if (!animationElement.current) return;
61
+ removeZeroOpacity();
62
+ });
63
+ animationElement.current && observer?.current?.unobserve(animationElement.current);
64
+ }, [
65
+ applyInitOpacity,
66
+ appearPlay,
67
+ removeZeroOpacity
68
+ ]);
69
+ const cancelAppear = react.useCallback(()=>{
70
+ if (!animationAppearInstance.current) return;
71
+ animationAppearInstance.current.cancel();
72
+ }, [
73
+ animationAppearInstance
74
+ ]);
75
+ const playHover = react.useCallback((e)=>{
76
+ e && e.stopPropagation();
77
+ if (!animationHoverInstance.current) return;
78
+ cancelAppear();
79
+ hoverPlay();
80
+ }, [
81
+ animationHoverInstance,
82
+ cancelAppear,
83
+ hoverPlay
84
+ ]);
85
+ const cancelHover = react.useCallback(()=>{
86
+ if (!animationHoverInstance.current) return;
87
+ if (animationHoverType.current === 'zoom') {
88
+ hoverPlay(-1);
89
+ if (!isAppearLoop) return;
90
+ animationHoverInstance.current.finished.then(()=>{
91
+ clearTimeout(appearAfterHoverTimer.current);
92
+ appearAfterHoverTimer.current = setTimeout(()=>{
93
+ playAppear();
94
+ }, 100);
95
+ });
96
+ return;
97
+ }
98
+ animationHoverInstance.current.cancel();
99
+ if (isAppearLoop) playAppear();
100
+ return;
101
+ }, [
102
+ hoverPlay,
103
+ isAppearLoop,
104
+ playAppear
105
+ ]);
106
+ const getSettingByDevice = react.useCallback(()=>{
107
+ if (currentDevice.current === 'desktop') return settings?.desktop;
108
+ else if (currentDevice.current === 'tablet') return settings?.tablet ?? settings?.desktop;
109
+ else if (currentDevice.current === 'mobile') return settings?.mobile ?? settings?.tablet ?? settings?.desktop;
110
+ return settings?.desktop;
111
+ }, [
112
+ settings?.desktop,
113
+ settings?.mobile,
114
+ settings?.tablet
115
+ ]);
116
+ const getAnimationType = (settings, type)=>{
117
+ return settings?.triggerConfig?.[type]?.animation ?? 'none';
118
+ };
119
+ const initAppearInstance = react.useCallback((settings)=>{
120
+ if (animationAppearType.current === 'none') return;
121
+ const animationConfig = settings?.triggerConfig?.['appear']?.setting?.[animationAppearType.current];
122
+ setIsAppearLoop(animationConfig.loop);
123
+ animationAppearInstance.current = animation?.[animationAppearType.current]?.(animationElement.current, animationConfig);
124
+ }, [
125
+ animation
126
+ ]);
127
+ const initHoverInstance = react.useCallback((settings)=>{
128
+ if (animationHoverType.current === 'none') return;
129
+ const animationConfig = settings?.triggerConfig?.['hover']?.setting?.[animationHoverType.current];
130
+ animationHoverInstance.current = animation?.[animationHoverType.current]?.(animationElement.current, animationConfig);
131
+ }, [
132
+ animation
133
+ ]);
134
+ const reset = react.useCallback(()=>{
135
+ if (animationAppearInstance.current) {
136
+ animationAppearInstance.current = null;
137
+ if (animationElement.current) {
138
+ observer.current?.unobserve(animationElement.current);
139
+ }
140
+ }
141
+ if (animationHoverInstance) {
142
+ animationHoverInstance.current = null;
143
+ if (animationElement.current) {
144
+ animationElement.current.removeEventListener('mouseenter', playHover);
145
+ animationElement.current.removeEventListener('mouseleave', cancelHover);
146
+ }
147
+ }
148
+ }, [
149
+ cancelHover,
150
+ playHover
151
+ ]);
152
+ const initAnimation = react.useCallback((settings)=>{
153
+ reset();
154
+ if (!animationElement.current) return;
155
+ if (!settings?.enabled) {
156
+ removeZeroOpacity();
157
+ return;
158
+ }
159
+ animationAppearType.current = getAnimationType(settings, 'appear');
160
+ animationHoverType.current = getAnimationType(settings, 'hover');
161
+ if (animationAppearType.current === 'none') removeZeroOpacity();
162
+ if (animationHoverType.current !== 'none' && currentDevice.current === 'desktop') {
163
+ initHoverInstance(settings);
164
+ animationElement.current.addEventListener('mouseenter', playHover);
165
+ animationElement.current.addEventListener('mouseleave', cancelHover);
166
+ }
167
+ if (animationAppearType.current !== 'none') {
168
+ initAppearInstance(settings);
169
+ observer.current = new IntersectionObserver((entries)=>{
170
+ entries.forEach((entry)=>{
171
+ if (entry.intersectionRatio > 0.1) playAppear();
172
+ });
173
+ }, {
174
+ root: null,
175
+ rootMargin: '0px',
176
+ threshold: [
177
+ 0.1
178
+ ]
179
+ });
180
+ observer.current.observe(animationElement.current);
181
+ } else removeZeroOpacity();
182
+ }, [
183
+ cancelHover,
184
+ initAppearInstance,
185
+ initHoverInstance,
186
+ playAppear,
187
+ playHover,
188
+ removeZeroOpacity,
189
+ reset
190
+ ]);
191
+ const initResizeObserver = react.useCallback(()=>{
192
+ const screen = document.querySelector('html');
193
+ if (!animationElement.current || !screen) return;
194
+ const resizeObserver = new ResizeObserver(([entry])=>{
195
+ const deviceWidth = entry?.target.getBoundingClientRect().width ?? 0;
196
+ let device;
197
+ if (deviceWidth >= 1024) device = 'desktop';
198
+ else if (deviceWidth >= 768) device = 'tablet';
199
+ else device = 'mobile';
200
+ if (currentDevice.current === device) return;
201
+ currentDevice.current = device;
202
+ initAnimation(getSettingByDevice());
203
+ });
204
+ resizeObserver.observe(screen);
205
+ return ()=>resizeObserver.disconnect();
206
+ }, [
207
+ getSettingByDevice,
208
+ initAnimation
209
+ ]);
210
+ react.useEffect(()=>{
211
+ setTimeout(()=>{
212
+ if (!settings || !wrapperRef.current) return;
213
+ wrapperId.current = `animation-wrapper-${Math.random().toString(36).substr(2, 5)}`;
214
+ wrapperRef.current.setAttribute('data-animationid', wrapperId.current);
215
+ if (Object.keys(TAGS_SELECTOR).includes(tag)) animationElement.current = document.querySelector(`[data-animationid='${wrapperId.current}'] ${TAGS_SELECTOR?.[tag] ?? ''}`);
216
+ else animationElement.current = document.querySelector(`[data-animationid='${wrapperId.current}'] > :first-child`);
217
+ return initResizeObserver();
218
+ }, 100);
219
+ }, [
220
+ initResizeObserver,
221
+ settings,
222
+ tag,
223
+ wrapperRef
224
+ ]);
225
+ return {
226
+ initAnimation
227
+ };
228
+ };
229
+
230
+ exports.useLivePageAnimation = useLivePageAnimation;