@kontextso/sdk-react-native 2.0.0-rc.7 → 2.1.0-rc.1

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2025 Kontext
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # @kontextso/sdk-react-native
2
+
3
+ A lightweight React Native SDK for integrating Kontext's AI-powered ads into your iOS and Android apps.
4
+
5
+ ---
6
+
7
+ ## Quick Setup
8
+
9
+ ### Prerequisites
10
+
11
+ To get started, you will need to set up a publisher account to get a [`publisherToken` and `code`](https://docs.kontext.so/publishers#getting-started-is-easy).
12
+
13
+ ### 1. Installation
14
+
15
+ ```bash
16
+ # Install the SDK
17
+ npm install @kontextso/sdk-react-native
18
+
19
+ # Install required dependencies
20
+ npm install react-native-device-info
21
+ npm install react-native-webview
22
+ ```
23
+
24
+ ### 2. Initialize AdsProvider
25
+
26
+ Set up the ad provider early in your component tree:
27
+
28
+ ```tsx
29
+ import React, { useState } from 'react';
30
+ import { AdsProvider } from '@kontextso/sdk-react-native';
31
+
32
+ interface Message {
33
+ id: string;
34
+ role: 'user' | 'assistant';
35
+ content: string;
36
+ createdAt: Date;
37
+ }
38
+
39
+ function App() {
40
+ const [messages, setMessages] = useState<Message[]>([]);
41
+
42
+ return (
43
+ <AdsProvider
44
+ publisherToken="<your-publisher-token>"
45
+ messages={messages}
46
+ userId="<unique-user-id>"
47
+ conversationId="<conversation-id>"
48
+ enabledPlacementCodes={["<placement-code>"]}
49
+ character={{
50
+ name: "<name>",
51
+ avatarUrl: "<url>",
52
+ isNsfw: <boolean>,
53
+ id: "<character-id>",
54
+ greeting: "<greeting text>",
55
+ persona: "<persona description>",
56
+ tags: ["tag1", "tag2"],
57
+ }}
58
+ >
59
+ {/* Rest of your app components */}
60
+ </AdsProvider>
61
+ );
62
+ }
63
+ ```
64
+
65
+ ### 3. Display Your First Ad
66
+
67
+ Insert ads inline within your UI, tied to messages:
68
+
69
+ ```tsx
70
+ function MessageList({ messages }: { messages: Message[] }) {
71
+ return (
72
+ <View>
73
+ {messages.map((m) => (
74
+ <View key={m.id}>
75
+ {/* Your message component */}
76
+ <InlineAd code="<placement-code>" messageId={m.id} />
77
+ </View>
78
+ ))}
79
+ </View>
80
+ );
81
+ }
82
+ ```
83
+
84
+ ## Additional Resources
85
+
86
+ * Explore a working [Demo Project featuring SDK integration](https://github.com/kontextso/kontext-react-native-demo).
87
+ * Full documentation: [Kontext React Native SDK](https://docs.kontext.so/sdk/react-native).
package/dist/index.js CHANGED
@@ -36,29 +36,66 @@ __export(index_exports, {
36
36
  module.exports = __toCommonJS(index_exports);
37
37
 
38
38
  // src/formats/Format.tsx
39
- var import_react = require("react");
39
+ var import_react2 = require("react");
40
40
  var import_sdk_react = require("@kontextso/sdk-react");
41
- var import_react_native_webview = require("react-native-webview");
42
41
  var import_react_native = require("react-native");
43
42
 
44
43
  // ../sdk-common/dist/index.mjs
45
44
  function makeIframeMessage(type, opts) {
46
- const { data, code } = opts;
47
- const message = { type, data: { ...data || {}, code } };
45
+ const { data, code, component } = opts;
46
+ const message = { type, data: { ...data || {}, code, component } };
48
47
  return message;
49
48
  }
50
49
  function handleIframeMessage(handler, opts) {
51
- const { origin, code } = opts;
50
+ const { origin, code, component } = opts;
52
51
  return (event) => {
53
52
  if (origin && event.origin !== origin) return;
54
53
  const eventCode = event.data.data?.code;
55
54
  if (eventCode && code && eventCode !== code) return;
55
+ const eventComponent = event.data.data?.component;
56
+ if (eventComponent && component && eventComponent !== component) return;
56
57
  handler(event.data);
57
58
  };
58
59
  }
59
60
 
60
- // src/formats/Format.tsx
61
+ // src/frame-webview.tsx
62
+ var import_react = require("react");
63
+ var import_react_native_webview = require("react-native-webview");
61
64
  var import_jsx_runtime = require("react/jsx-runtime");
65
+ var FrameWebView = (0, import_react.forwardRef)(
66
+ ({ iframeUrl, onMessage, style, onError, onLoad }, forwardedRef) => {
67
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
68
+ import_react_native_webview.WebView,
69
+ {
70
+ ref: forwardedRef,
71
+ source: {
72
+ uri: iframeUrl
73
+ },
74
+ onMessage,
75
+ style,
76
+ allowsInlineMediaPlayback: true,
77
+ mediaPlaybackRequiresUserAction: false,
78
+ javaScriptEnabled: true,
79
+ domStorageEnabled: true,
80
+ allowsFullscreenVideo: false,
81
+ injectedJavaScript: `
82
+ window.addEventListener("message", function(event) {
83
+ if (window.ReactNativeWebView && event.data) {
84
+ // ReactNativeWebView.postMessage only supports string data
85
+ window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
86
+ }
87
+ }, false);
88
+ `,
89
+ onError,
90
+ onLoad
91
+ }
92
+ );
93
+ }
94
+ );
95
+ var frame_webview_default = FrameWebView;
96
+
97
+ // src/formats/Format.tsx
98
+ var import_jsx_runtime2 = require("react/jsx-runtime");
62
99
  var sendMessage = (webViewRef, type, code, data) => {
63
100
  const message = makeIframeMessage(type, {
64
101
  data,
@@ -71,16 +108,22 @@ var sendMessage = (webViewRef, type, code, data) => {
71
108
  `);
72
109
  };
73
110
  var Format = ({ code, messageId, wrapper, ...otherParams }) => {
74
- const context = (0, import_react.useContext)(import_sdk_react.AdsContext);
111
+ const context = (0, import_react2.useContext)(import_sdk_react.AdsContext);
75
112
  const bid = (0, import_sdk_react.useBid)({ code, messageId });
76
- const [height, setHeight] = (0, import_react.useState)(0);
113
+ const [height, setHeight] = (0, import_react2.useState)(0);
77
114
  const iframeUrl = (0, import_sdk_react.useIframeUrl)(context, bid, code, messageId);
78
- const [showIframe, setShowIframe] = (0, import_react.useState)(false);
79
- const [iframeLoaded, setIframeLoaded] = (0, import_react.useState)(false);
80
- const [containerStyles, setContainerStyles] = (0, import_react.useState)({});
81
- const [iframeStyles, setIframeStyles] = (0, import_react.useState)({});
82
- const containerRef = (0, import_react.useRef)(null);
83
- const webViewRef = (0, import_react.useRef)(null);
115
+ const modalUrl = iframeUrl.replace("/api/frame/", "/api/modal/");
116
+ const [showIframe, setShowIframe] = (0, import_react2.useState)(false);
117
+ const [iframeLoaded, setIframeLoaded] = (0, import_react2.useState)(false);
118
+ const [modalOpen, setModalOpen] = (0, import_react2.useState)(false);
119
+ const [modalShown, setModalShown] = (0, import_react2.useState)(false);
120
+ const [modalLoaded, setModalLoaded] = (0, import_react2.useState)(false);
121
+ const [containerStyles, setContainerStyles] = (0, import_react2.useState)({});
122
+ const [iframeStyles, setIframeStyles] = (0, import_react2.useState)({});
123
+ const containerRef = (0, import_react2.useRef)(null);
124
+ const webViewRef = (0, import_react2.useRef)(null);
125
+ const modalWebViewRef = (0, import_react2.useRef)(null);
126
+ const modalInitTimeoutRef = (0, import_react2.useRef)(null);
84
127
  const { height: windowHeight, width: windowWidth } = (0, import_react_native.useWindowDimensions)();
85
128
  const reset = () => {
86
129
  setHeight(0);
@@ -88,9 +131,19 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
88
131
  setContainerStyles({});
89
132
  setIframeStyles({});
90
133
  setIframeLoaded(false);
134
+ resetModal();
91
135
  context?.resetAll();
92
136
  context?.captureError(new Error("Processing iframe error"));
93
137
  };
138
+ const resetModal = () => {
139
+ if (modalInitTimeoutRef.current) {
140
+ clearTimeout(modalInitTimeoutRef.current);
141
+ modalInitTimeoutRef.current = null;
142
+ }
143
+ setModalOpen(false);
144
+ setModalLoaded(false);
145
+ setModalShown(false);
146
+ };
94
147
  const debug = (name, data = {}) => {
95
148
  context?.onDebugEventInternal?.(name, {
96
149
  code,
@@ -106,6 +159,19 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
106
159
  ...data
107
160
  });
108
161
  };
162
+ const debugModal = (name, data = {}) => {
163
+ context?.onDebugEventInternal?.(name, {
164
+ code,
165
+ messageId,
166
+ otherParams,
167
+ bid,
168
+ modalUrl,
169
+ modalOpen,
170
+ modalShown,
171
+ modalLoaded,
172
+ ...data
173
+ });
174
+ };
109
175
  debug("format-update-state");
110
176
  const onMessage = (event) => {
111
177
  try {
@@ -153,6 +219,10 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
153
219
  setContainerStyles(message.data.containerStyles);
154
220
  setIframeStyles(message.data.iframeStyles);
155
221
  break;
222
+ case "open-component-iframe":
223
+ setModalOpen(true);
224
+ modalInitTimeoutRef.current = setTimeout(resetModal, message.data.timeout ?? 5e3);
225
+ break;
156
226
  }
157
227
  },
158
228
  {
@@ -168,8 +238,55 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
168
238
  reset();
169
239
  }
170
240
  };
241
+ const onModalMessage = (event) => {
242
+ try {
243
+ const data = JSON.parse(event.nativeEvent.data);
244
+ debugModal("modal-iframe-message", {
245
+ message: data
246
+ });
247
+ const messageHandler = handleIframeMessage(
248
+ (message) => {
249
+ switch (message.type) {
250
+ case "close-component-iframe":
251
+ resetModal();
252
+ break;
253
+ case "init-component-iframe":
254
+ if (modalInitTimeoutRef.current) {
255
+ clearTimeout(modalInitTimeoutRef.current);
256
+ modalInitTimeoutRef.current = null;
257
+ }
258
+ setModalShown(true);
259
+ break;
260
+ case "error-component-iframe":
261
+ case "error-iframe":
262
+ resetModal();
263
+ break;
264
+ case "click-iframe":
265
+ if (message.data.url) {
266
+ import_react_native.Linking.openURL(`${context?.adServerUrl}${message.data.url}`).catch(
267
+ (err) => console.error("error opening url", err)
268
+ );
269
+ }
270
+ context?.onAdClickInternal(message.data);
271
+ break;
272
+ }
273
+ },
274
+ {
275
+ code,
276
+ component: "modal"
277
+ }
278
+ );
279
+ messageHandler({ data });
280
+ } catch (e) {
281
+ debugModal("modal-iframe-message-error", {
282
+ error: e
283
+ });
284
+ console.error("error parsing message from webview", e);
285
+ resetModal();
286
+ }
287
+ };
171
288
  const paramsString = (0, import_sdk_react.convertParamsToString)(otherParams);
172
- (0, import_react.useEffect)(() => {
289
+ (0, import_react2.useEffect)(() => {
173
290
  if (!iframeLoaded || !context?.adServerUrl || !bid || !webViewRef.current) {
174
291
  return;
175
292
  }
@@ -192,7 +309,7 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
192
309
  });
193
310
  });
194
311
  };
195
- (0, import_react.useEffect)(() => {
312
+ (0, import_react2.useEffect)(() => {
196
313
  const interval = setInterval(() => {
197
314
  checkIfInViewport();
198
315
  }, 250);
@@ -213,59 +330,71 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
213
330
  }
214
331
  return 0;
215
332
  };
216
- const content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
217
- import_react_native.View,
218
- {
219
- style: containerStyles,
220
- ref: containerRef,
221
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
222
- import_react_native_webview.WebView,
223
- {
224
- ref: webViewRef,
225
- source: {
226
- uri: iframeUrl
227
- },
228
- onMessage,
229
- style: {
230
- height: getHeight(),
231
- width: getWidth(),
232
- ...iframeStyles
233
- },
234
- injectedJavaScript: `
235
- function sendToLog(data) {
236
- window.ReactNativeWebView.postMessage(JSON.stringify({
237
- type: 'log-iframe',
238
- data: data
239
- }));
240
- }
241
-
242
- window.addEventListener("message", function(event) {
243
- if (window.ReactNativeWebView && event.data) {
244
- // ReactNativeWebView.postMessage only supports string data
245
- window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
333
+ const content = /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
334
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native.Modal, { visible: modalOpen, transparent: true, onRequestClose: resetModal, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
335
+ import_react_native.View,
336
+ {
337
+ style: {
338
+ flex: 1,
339
+ // Don't show the modal until the modal page is loaded and sends 'init-component-iframe' message back to SDK
340
+ ...modalShown ? { opacity: 1, pointerEvents: "auto" } : { opacity: 0, pointerEvents: "none" }
341
+ },
342
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
343
+ frame_webview_default,
344
+ {
345
+ ref: modalWebViewRef,
346
+ iframeUrl: modalUrl,
347
+ onMessage: onModalMessage,
348
+ style: {
349
+ backgroundColor: "transparent",
350
+ height: "100%",
351
+ width: "100%",
352
+ borderWidth: 0
353
+ },
354
+ onError: () => {
355
+ debug("modal-error");
356
+ resetModal();
357
+ },
358
+ onLoad: () => {
359
+ debug("modal-load");
360
+ setModalLoaded(true);
246
361
  }
247
- }, false);
248
- `,
249
- onError: () => {
250
- debug("iframe-error");
251
- reset();
252
- },
253
- onLoad: () => {
254
- debug("iframe-load");
255
362
  }
363
+ )
364
+ }
365
+ ) }),
366
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native.View, { style: containerStyles, ref: containerRef, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
367
+ frame_webview_default,
368
+ {
369
+ ref: webViewRef,
370
+ iframeUrl,
371
+ onMessage,
372
+ style: {
373
+ height: getHeight(),
374
+ width: getWidth(),
375
+ background: "transparent",
376
+ borderWidth: 0,
377
+ ...iframeStyles
378
+ },
379
+ onError: () => {
380
+ debug("iframe-error");
381
+ reset();
382
+ },
383
+ onLoad: () => {
384
+ debug("iframe-load");
256
385
  }
257
- )
258
- }
259
- );
386
+ }
387
+ ) })
388
+ ] });
260
389
  return wrapper ? wrapper(content) : content;
261
390
  };
262
- var FormatWithErrorBoundary = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_sdk_react.ErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Format, { ...props }) });
391
+ var FormatWithErrorBoundary = (props) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_sdk_react.ErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Format, { ...props }) });
263
392
  var Format_default = FormatWithErrorBoundary;
264
393
 
265
394
  // src/formats/InlineAd.tsx
266
- var import_jsx_runtime2 = require("react/jsx-runtime");
395
+ var import_jsx_runtime3 = require("react/jsx-runtime");
267
396
  var InlineAd = ({ code, messageId, wrapper, ...props }) => {
268
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Format_default, { code, messageId, wrapper, ...props });
397
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Format_default, { code, messageId, wrapper, ...props });
269
398
  };
270
399
  var InlineAd_default = InlineAd;
271
400
 
@@ -273,7 +402,7 @@ var InlineAd_default = InlineAd;
273
402
  var import_sdk_react2 = require("@kontextso/sdk-react");
274
403
  var import_react_native2 = require("react-native");
275
404
  var import_react_native_device_info = __toESM(require("react-native-device-info"));
276
- var import_jsx_runtime3 = require("react/jsx-runtime");
405
+ var import_jsx_runtime4 = require("react/jsx-runtime");
277
406
  ErrorUtils.setGlobalHandler((error, isFatal) => {
278
407
  if (!isFatal) {
279
408
  import_sdk_react2.log.warn(error);
@@ -310,7 +439,7 @@ var getDevice = () => {
310
439
  return {};
311
440
  };
312
441
  var AdsProvider = (props) => {
313
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_sdk_react2.AdsProviderInternal, { ...props, getDevice });
442
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_sdk_react2.AdsProviderInternal, { ...props, getDevice });
314
443
  };
315
444
  // Annotate the CommonJS export names for ESM import in node:
316
445
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,27 +1,70 @@
1
1
  // src/formats/Format.tsx
2
2
  import { useContext, useEffect, useRef, useState } from "react";
3
- import { AdsContext, convertParamsToString, ErrorBoundary, useBid, useIframeUrl } from "@kontextso/sdk-react";
4
- import { WebView } from "react-native-webview";
5
- import { Linking, View, useWindowDimensions } from "react-native";
3
+ import {
4
+ AdsContext,
5
+ convertParamsToString,
6
+ ErrorBoundary,
7
+ useBid,
8
+ useIframeUrl
9
+ } from "@kontextso/sdk-react";
10
+ import { Linking, Modal, View, useWindowDimensions } from "react-native";
6
11
 
7
12
  // ../sdk-common/dist/index.mjs
8
13
  function makeIframeMessage(type, opts) {
9
- const { data, code } = opts;
10
- const message = { type, data: { ...data || {}, code } };
14
+ const { data, code, component } = opts;
15
+ const message = { type, data: { ...data || {}, code, component } };
11
16
  return message;
12
17
  }
13
18
  function handleIframeMessage(handler, opts) {
14
- const { origin, code } = opts;
19
+ const { origin, code, component } = opts;
15
20
  return (event) => {
16
21
  if (origin && event.origin !== origin) return;
17
22
  const eventCode = event.data.data?.code;
18
23
  if (eventCode && code && eventCode !== code) return;
24
+ const eventComponent = event.data.data?.component;
25
+ if (eventComponent && component && eventComponent !== component) return;
19
26
  handler(event.data);
20
27
  };
21
28
  }
22
29
 
23
- // src/formats/Format.tsx
30
+ // src/frame-webview.tsx
31
+ import { forwardRef } from "react";
32
+ import { WebView } from "react-native-webview";
24
33
  import { jsx } from "react/jsx-runtime";
34
+ var FrameWebView = forwardRef(
35
+ ({ iframeUrl, onMessage, style, onError, onLoad }, forwardedRef) => {
36
+ return /* @__PURE__ */ jsx(
37
+ WebView,
38
+ {
39
+ ref: forwardedRef,
40
+ source: {
41
+ uri: iframeUrl
42
+ },
43
+ onMessage,
44
+ style,
45
+ allowsInlineMediaPlayback: true,
46
+ mediaPlaybackRequiresUserAction: false,
47
+ javaScriptEnabled: true,
48
+ domStorageEnabled: true,
49
+ allowsFullscreenVideo: false,
50
+ injectedJavaScript: `
51
+ window.addEventListener("message", function(event) {
52
+ if (window.ReactNativeWebView && event.data) {
53
+ // ReactNativeWebView.postMessage only supports string data
54
+ window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
55
+ }
56
+ }, false);
57
+ `,
58
+ onError,
59
+ onLoad
60
+ }
61
+ );
62
+ }
63
+ );
64
+ var frame_webview_default = FrameWebView;
65
+
66
+ // src/formats/Format.tsx
67
+ import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
25
68
  var sendMessage = (webViewRef, type, code, data) => {
26
69
  const message = makeIframeMessage(type, {
27
70
  data,
@@ -38,12 +81,18 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
38
81
  const bid = useBid({ code, messageId });
39
82
  const [height, setHeight] = useState(0);
40
83
  const iframeUrl = useIframeUrl(context, bid, code, messageId);
84
+ const modalUrl = iframeUrl.replace("/api/frame/", "/api/modal/");
41
85
  const [showIframe, setShowIframe] = useState(false);
42
86
  const [iframeLoaded, setIframeLoaded] = useState(false);
87
+ const [modalOpen, setModalOpen] = useState(false);
88
+ const [modalShown, setModalShown] = useState(false);
89
+ const [modalLoaded, setModalLoaded] = useState(false);
43
90
  const [containerStyles, setContainerStyles] = useState({});
44
91
  const [iframeStyles, setIframeStyles] = useState({});
45
92
  const containerRef = useRef(null);
46
93
  const webViewRef = useRef(null);
94
+ const modalWebViewRef = useRef(null);
95
+ const modalInitTimeoutRef = useRef(null);
47
96
  const { height: windowHeight, width: windowWidth } = useWindowDimensions();
48
97
  const reset = () => {
49
98
  setHeight(0);
@@ -51,9 +100,19 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
51
100
  setContainerStyles({});
52
101
  setIframeStyles({});
53
102
  setIframeLoaded(false);
103
+ resetModal();
54
104
  context?.resetAll();
55
105
  context?.captureError(new Error("Processing iframe error"));
56
106
  };
107
+ const resetModal = () => {
108
+ if (modalInitTimeoutRef.current) {
109
+ clearTimeout(modalInitTimeoutRef.current);
110
+ modalInitTimeoutRef.current = null;
111
+ }
112
+ setModalOpen(false);
113
+ setModalLoaded(false);
114
+ setModalShown(false);
115
+ };
57
116
  const debug = (name, data = {}) => {
58
117
  context?.onDebugEventInternal?.(name, {
59
118
  code,
@@ -69,6 +128,19 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
69
128
  ...data
70
129
  });
71
130
  };
131
+ const debugModal = (name, data = {}) => {
132
+ context?.onDebugEventInternal?.(name, {
133
+ code,
134
+ messageId,
135
+ otherParams,
136
+ bid,
137
+ modalUrl,
138
+ modalOpen,
139
+ modalShown,
140
+ modalLoaded,
141
+ ...data
142
+ });
143
+ };
72
144
  debug("format-update-state");
73
145
  const onMessage = (event) => {
74
146
  try {
@@ -116,6 +188,10 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
116
188
  setContainerStyles(message.data.containerStyles);
117
189
  setIframeStyles(message.data.iframeStyles);
118
190
  break;
191
+ case "open-component-iframe":
192
+ setModalOpen(true);
193
+ modalInitTimeoutRef.current = setTimeout(resetModal, message.data.timeout ?? 5e3);
194
+ break;
119
195
  }
120
196
  },
121
197
  {
@@ -131,6 +207,53 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
131
207
  reset();
132
208
  }
133
209
  };
210
+ const onModalMessage = (event) => {
211
+ try {
212
+ const data = JSON.parse(event.nativeEvent.data);
213
+ debugModal("modal-iframe-message", {
214
+ message: data
215
+ });
216
+ const messageHandler = handleIframeMessage(
217
+ (message) => {
218
+ switch (message.type) {
219
+ case "close-component-iframe":
220
+ resetModal();
221
+ break;
222
+ case "init-component-iframe":
223
+ if (modalInitTimeoutRef.current) {
224
+ clearTimeout(modalInitTimeoutRef.current);
225
+ modalInitTimeoutRef.current = null;
226
+ }
227
+ setModalShown(true);
228
+ break;
229
+ case "error-component-iframe":
230
+ case "error-iframe":
231
+ resetModal();
232
+ break;
233
+ case "click-iframe":
234
+ if (message.data.url) {
235
+ Linking.openURL(`${context?.adServerUrl}${message.data.url}`).catch(
236
+ (err) => console.error("error opening url", err)
237
+ );
238
+ }
239
+ context?.onAdClickInternal(message.data);
240
+ break;
241
+ }
242
+ },
243
+ {
244
+ code,
245
+ component: "modal"
246
+ }
247
+ );
248
+ messageHandler({ data });
249
+ } catch (e) {
250
+ debugModal("modal-iframe-message-error", {
251
+ error: e
252
+ });
253
+ console.error("error parsing message from webview", e);
254
+ resetModal();
255
+ }
256
+ };
134
257
  const paramsString = convertParamsToString(otherParams);
135
258
  useEffect(() => {
136
259
  if (!iframeLoaded || !context?.adServerUrl || !bid || !webViewRef.current) {
@@ -176,59 +299,71 @@ var Format = ({ code, messageId, wrapper, ...otherParams }) => {
176
299
  }
177
300
  return 0;
178
301
  };
179
- const content = /* @__PURE__ */ jsx(
180
- View,
181
- {
182
- style: containerStyles,
183
- ref: containerRef,
184
- children: /* @__PURE__ */ jsx(
185
- WebView,
186
- {
187
- ref: webViewRef,
188
- source: {
189
- uri: iframeUrl
190
- },
191
- onMessage,
192
- style: {
193
- height: getHeight(),
194
- width: getWidth(),
195
- ...iframeStyles
196
- },
197
- injectedJavaScript: `
198
- function sendToLog(data) {
199
- window.ReactNativeWebView.postMessage(JSON.stringify({
200
- type: 'log-iframe',
201
- data: data
202
- }));
203
- }
204
-
205
- window.addEventListener("message", function(event) {
206
- if (window.ReactNativeWebView && event.data) {
207
- // ReactNativeWebView.postMessage only supports string data
208
- window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
302
+ const content = /* @__PURE__ */ jsxs(Fragment, { children: [
303
+ /* @__PURE__ */ jsx2(Modal, { visible: modalOpen, transparent: true, onRequestClose: resetModal, children: /* @__PURE__ */ jsx2(
304
+ View,
305
+ {
306
+ style: {
307
+ flex: 1,
308
+ // Don't show the modal until the modal page is loaded and sends 'init-component-iframe' message back to SDK
309
+ ...modalShown ? { opacity: 1, pointerEvents: "auto" } : { opacity: 0, pointerEvents: "none" }
310
+ },
311
+ children: /* @__PURE__ */ jsx2(
312
+ frame_webview_default,
313
+ {
314
+ ref: modalWebViewRef,
315
+ iframeUrl: modalUrl,
316
+ onMessage: onModalMessage,
317
+ style: {
318
+ backgroundColor: "transparent",
319
+ height: "100%",
320
+ width: "100%",
321
+ borderWidth: 0
322
+ },
323
+ onError: () => {
324
+ debug("modal-error");
325
+ resetModal();
326
+ },
327
+ onLoad: () => {
328
+ debug("modal-load");
329
+ setModalLoaded(true);
209
330
  }
210
- }, false);
211
- `,
212
- onError: () => {
213
- debug("iframe-error");
214
- reset();
215
- },
216
- onLoad: () => {
217
- debug("iframe-load");
218
331
  }
332
+ )
333
+ }
334
+ ) }),
335
+ /* @__PURE__ */ jsx2(View, { style: containerStyles, ref: containerRef, children: /* @__PURE__ */ jsx2(
336
+ frame_webview_default,
337
+ {
338
+ ref: webViewRef,
339
+ iframeUrl,
340
+ onMessage,
341
+ style: {
342
+ height: getHeight(),
343
+ width: getWidth(),
344
+ background: "transparent",
345
+ borderWidth: 0,
346
+ ...iframeStyles
347
+ },
348
+ onError: () => {
349
+ debug("iframe-error");
350
+ reset();
351
+ },
352
+ onLoad: () => {
353
+ debug("iframe-load");
219
354
  }
220
- )
221
- }
222
- );
355
+ }
356
+ ) })
357
+ ] });
223
358
  return wrapper ? wrapper(content) : content;
224
359
  };
225
- var FormatWithErrorBoundary = (props) => /* @__PURE__ */ jsx(ErrorBoundary, { children: /* @__PURE__ */ jsx(Format, { ...props }) });
360
+ var FormatWithErrorBoundary = (props) => /* @__PURE__ */ jsx2(ErrorBoundary, { children: /* @__PURE__ */ jsx2(Format, { ...props }) });
226
361
  var Format_default = FormatWithErrorBoundary;
227
362
 
228
363
  // src/formats/InlineAd.tsx
229
- import { jsx as jsx2 } from "react/jsx-runtime";
364
+ import { jsx as jsx3 } from "react/jsx-runtime";
230
365
  var InlineAd = ({ code, messageId, wrapper, ...props }) => {
231
- return /* @__PURE__ */ jsx2(Format_default, { code, messageId, wrapper, ...props });
366
+ return /* @__PURE__ */ jsx3(Format_default, { code, messageId, wrapper, ...props });
232
367
  };
233
368
  var InlineAd_default = InlineAd;
234
369
 
@@ -236,7 +371,7 @@ var InlineAd_default = InlineAd;
236
371
  import { AdsProviderInternal, log } from "@kontextso/sdk-react";
237
372
  import { Platform } from "react-native";
238
373
  import DeviceInfo from "react-native-device-info";
239
- import { jsx as jsx3 } from "react/jsx-runtime";
374
+ import { jsx as jsx4 } from "react/jsx-runtime";
240
375
  ErrorUtils.setGlobalHandler((error, isFatal) => {
241
376
  if (!isFatal) {
242
377
  log.warn(error);
@@ -273,7 +408,7 @@ var getDevice = () => {
273
408
  return {};
274
409
  };
275
410
  var AdsProvider = (props) => {
276
- return /* @__PURE__ */ jsx3(AdsProviderInternal, { ...props, getDevice });
411
+ return /* @__PURE__ */ jsx4(AdsProviderInternal, { ...props, getDevice });
277
412
  };
278
413
  export {
279
414
  AdsProvider,
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@kontextso/sdk-react-native",
3
- "version": "2.0.0-rc.7",
3
+ "version": "2.1.0-rc.1",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
7
7
  "type": "commonjs",
8
+ "license": "Apache-2.0",
8
9
  "scripts": {
9
10
  "dev:js": "tsup --watch",
10
11
  "dev": "npm-run-all dev:js",
@@ -51,9 +52,10 @@
51
52
  "react-native-webview": "^13.15.0"
52
53
  },
53
54
  "dependencies": {
54
- "@kontextso/sdk-react": "^1.2.2"
55
+ "@kontextso/sdk-react": "^1.2.3"
55
56
  },
56
57
  "files": [
57
- "dist/*"
58
+ "dist/*",
59
+ "LICENSE"
58
60
  ]
59
61
  }