@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,230 +0,0 @@
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;
@@ -1,46 +0,0 @@
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 };
@@ -1,225 +0,0 @@
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 };