-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathContainer.jsx
More file actions
125 lines (117 loc) · 3.85 KB
/
Container.jsx
File metadata and controls
125 lines (117 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/** @jsx h */
import { h } from 'preact';
import { useEffect, useState, useRef } from 'preact/hooks';
import { getOrCreateDeviceID, getTsCookieFromStorage } from '../../../../utils';
import {
useTransitionState,
ScrollProvider,
useServerData,
useXProps,
useDidUpdateEffect,
getContent,
setupTabTrap,
isLander,
isIframe
} from '../lib';
import Overlay from './Overlay';
const Container = ({ children }) => {
const contentWrapperRef = useRef();
const { type, views, meta, setServerData } = useServerData();
const {
onReady,
currency,
amount,
payerId,
clientId,
merchantId,
customerId,
buyerCountry,
ignoreCache,
version,
env,
stageTag,
channel,
ecToken,
contextualComponents,
disableSetCookie,
features
} = useXProps();
const [transitionState] = useTransitionState();
const [loading, setLoading] = useState(false);
const useNewCheckoutDesign = features?.includes('new-checkout-design') ? 'true' : 'false';
useEffect(() => {
if (transitionState === 'CLOSED') {
contentWrapperRef.current.scrollTop = 0;
}
}, [transitionState]);
useEffect(() => {
if (typeof onReady === 'function') {
onReady({
type,
products: views
.filter(({ meta: productMeta }) => productMeta?.product)
.map(({ meta: productMeta }) => productMeta.product),
meta,
ts: getTsCookieFromStorage()
});
}
}, [meta.messageRequestId]);
useDidUpdateEffect(() => {
setLoading(true);
getContent({
currency,
amount: amount === '' ? undefined : amount,
payerId,
clientId,
merchantId,
customerId,
buyerCountry,
ignoreCache,
version,
env,
stageTag,
channel,
ecToken,
contextualComponents,
deviceID: getOrCreateDeviceID(),
disableSetCookie,
features
}).then(data => {
setServerData(data);
setLoading(false);
});
}, [currency, amount, payerId, clientId, merchantId, buyerCountry]);
useEffect(() => {
setupTabTrap();
}, []);
return (
<ScrollProvider containerRef={contentWrapperRef}>
<div className="modal-wrapper">
<div className="spinner" style={{ opacity: loading ? '1' : '0' }} />
<Overlay />
{/* Presentational div to clip scrollbars with a rounded border */}
{/* Lander variant uses the div with className content__wrapper-overflow as the contentWrapperRef */}
<div
className={`content__wrapper-overflow ${loading ? 'loading' : ''}${
useNewCheckoutDesign === 'true' && !isLander ? 'checkout' : ''
}`}
ref={!!(isLander && !isIframe) && contentWrapperRef}
>
{/* Scrollable content */}
{/* Iframe variants use the div with className content__wrapper as the contentWrapperRef */}
<div
tabindex="-1"
className="content__wrapper"
ref={!!(!isLander || isIframe) && contentWrapperRef}
role={isIframe ? 'dialog' : undefined}
aria-modal={isIframe ? 'true' : undefined}
aria-labelledby={isIframe ? 'header__headline' : undefined}
>
{children}
</div>
</div>
</div>
</ScrollProvider>
);
};
export default Container;