Skip to content

Commit bcaa3f1

Browse files
committed
Improve loading state of consent gate and layer
1 parent cdcaaf8 commit bcaa3f1

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

packages/react-native-contentpass-ui/src/components/ContentpassConsentGate.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useMemo, useState } from 'react';
2-
import { StyleSheet, View } from 'react-native';
2+
import { ActivityIndicator, StyleSheet, View } from 'react-native';
33
import {
44
ContentpassStateType,
55
useContentpassSdk,
@@ -35,6 +35,7 @@ export default function ContentpassConsentGate({
3535
const [isShowingSecondLayer, setIsShowingSecondLayer] = useState(false);
3636
const [isShowingContentpass, setIsShowingContentpass] = useState(false);
3737

38+
const [consentResolved, setConsentResolved] = useState(false);
3839
const [purposesList, setPurposesList] = useState<string[]>([]);
3940
const [vendorCount, setVendorCount] = useState(0);
4041

@@ -136,6 +137,7 @@ export default function ContentpassConsentGate({
136137
onVisibilityChange?.(visible);
137138
}
138139
setIsVisible(visible);
140+
setConsentResolved(true);
139141
}, [
140142
cmpReady,
141143
cpAuthState,
@@ -146,6 +148,14 @@ export default function ContentpassConsentGate({
146148
onVisibilityChange,
147149
]);
148150

151+
if (!consentResolved) {
152+
return (
153+
<View style={styles.loading}>
154+
<ActivityIndicator size="large" />
155+
</View>
156+
);
157+
}
158+
149159
if (!isVisible) {
150160
return <>{children}</>;
151161
}
@@ -176,6 +186,11 @@ export default function ContentpassConsentGate({
176186
}
177187

178188
const styles = StyleSheet.create({
189+
loading: {
190+
flex: 1,
191+
alignItems: 'center',
192+
justifyContent: 'center',
193+
},
179194
overlayContainer: {
180195
flex: 1,
181196
},

packages/react-native-contentpass-ui/src/components/ContentpassLayer.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
import { StyleSheet, Text, View } from 'react-native';
1+
import { ActivityIndicator, StyleSheet, Text, View } from 'react-native';
22
import { WebView, type WebViewMessageEvent } from 'react-native-webview';
33
import type { ContentpassLayerEvents } from './ContentpassLayerEvents';
44
import buildFirstLayerUrl from './buildFirstLayerUrl';
55
import { useMemo, useState } from 'react';
66

77
const MESSAGE_PROTOCOL = 'contentpass-first-layer';
88

9-
const DISABLE_ANIMATIONS_JS = `
9+
const EARLY_INJECT_JS = `
1010
(function () {
1111
var style = document.createElement('style');
1212
style.textContent = '*, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; } main, .backdrop { visibility: visible !important; transform: none !important; }';
1313
(document.head || document.documentElement).appendChild(style);
1414
15-
true;
16-
})();
17-
`;
18-
19-
const GLUE_CODE_JS = `
20-
(function () {
21-
const originalPostMessage = window.postMessage;
15+
var originalPostMessage = window.postMessage;
2216
window.postMessage = function (data) {
2317
try {
2418
window.ReactNativeWebView.postMessage(
@@ -37,7 +31,7 @@ const GLUE_CODE_JS = `
3731
const styles = StyleSheet.create({
3832
container: {
3933
flex: 1,
40-
backgroundColor: 'transparent',
34+
backgroundColor: '#fff',
4135
},
4236
webview: {
4337
flex: 1,
@@ -53,6 +47,11 @@ const styles = StyleSheet.create({
5347
fontSize: 14,
5448
textAlign: 'center',
5549
},
50+
loading: {
51+
...StyleSheet.absoluteFillObject,
52+
alignItems: 'center',
53+
justifyContent: 'center',
54+
},
5655
});
5756

5857
export default function ContentpassLayer({
@@ -163,8 +162,7 @@ export default function ContentpassLayer({
163162
javaScriptEnabled
164163
domStorageEnabled
165164
automaticallyAdjustContentInsets={false}
166-
injectedJavaScriptBeforeContentLoaded={DISABLE_ANIMATIONS_JS}
167-
injectedJavaScript={GLUE_CODE_JS}
165+
injectedJavaScriptBeforeContentLoaded={EARLY_INJECT_JS}
168166
onMessage={(event) => {
169167
handleMessage(event);
170168
}}
@@ -195,6 +193,11 @@ export default function ContentpassLayer({
195193
</View>
196194
)}
197195
/>
196+
{!ready && (
197+
<View style={styles.loading}>
198+
<ActivityIndicator size="large" />
199+
</View>
200+
)}
198201
</View>
199202
);
200203
}

0 commit comments

Comments
 (0)