@gem-sdk/core 1.31.0 → 1.31.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.
@@ -1,228 +0,0 @@
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 };