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