@adsterra-ad/react 0.1.0

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/dist/index.cjs ADDED
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.tsx
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AdBanner: () => AdBanner,
24
+ AdContainer: () => AdContainer,
25
+ configureAds: () => import_core.configureAds,
26
+ default: () => index_default
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_react = require("react");
30
+ var import_core = require("@adsterra-ad/core");
31
+ var import_jsx_runtime = require("react/jsx-runtime");
32
+ function AdBanner({
33
+ format,
34
+ provider = "adsterra",
35
+ className,
36
+ adKey,
37
+ adLabel = "Advertisement",
38
+ showAdLabel = true,
39
+ adLabelPosition = "top-left",
40
+ onLoad,
41
+ onError
42
+ }) {
43
+ const bannerId = (0, import_react.useRef)((0, import_core.createBannerId)());
44
+ const [activeProvider, setActiveProvider] = (0, import_react.useState)(provider);
45
+ const [adLoaded, setAdLoaded] = (0, import_react.useState)(false);
46
+ const [adFailed, setAdFailed] = (0, import_react.useState)(false);
47
+ (0, import_react.useEffect)(() => setActiveProvider(provider), [provider]);
48
+ const config = (0, import_core.resolveAdConfig)(format, adKey);
49
+ const srcDoc = (0, import_react.useMemo)(
50
+ () => (0, import_core.buildSrcDoc)({ format, provider: activeProvider, config, bannerId: bannerId.current }),
51
+ [format, activeProvider, config]
52
+ );
53
+ (0, import_react.useEffect)(() => {
54
+ const handler = (event) => {
55
+ const data = event.data;
56
+ if (!data || data.bannerId !== bannerId.current) return;
57
+ if (data.type === "ad-load-success") {
58
+ setAdLoaded(true);
59
+ onLoad?.();
60
+ return;
61
+ }
62
+ if (data.type === "ad-load-error") {
63
+ setAdFailed(true);
64
+ onError?.();
65
+ }
66
+ };
67
+ window.addEventListener("message", handler);
68
+ return () => window.removeEventListener("message", handler);
69
+ }, [onLoad, onError]);
70
+ if (adFailed) return null;
71
+ const wrapperStyle = getWrapperStyle(adLabelPosition);
72
+ const labelStyle = getLabelStyle(adLabelPosition);
73
+ const visibilityStyle = getVisibilityStyle(adLoaded);
74
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `${className ?? ""}`.trim(), style: { ...wrapperStyle, ...visibilityStyle }, children: [
75
+ showAdLabel && adLabelPosition.startsWith("top") ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: labelStyle, children: adLabel }) : null,
76
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { "data-testid": "ad-banner", style: { ...parseStyle((0, import_core.getBannerStyle)(config)), overflow: "hidden", borderRadius: 6, border: "1px solid #e5e7eb", background: "#f3f4f6" }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
77
+ "iframe",
78
+ {
79
+ title: "Advertisement",
80
+ srcDoc,
81
+ width: typeof config.width === "number" ? config.width : void 0,
82
+ height: typeof config.height === "number" ? config.height : void 0,
83
+ frameBorder: 0,
84
+ scrolling: "no",
85
+ sandbox: "allow-scripts allow-forms allow-same-origin allow-popups allow-modals"
86
+ }
87
+ ) }),
88
+ showAdLabel && adLabelPosition.startsWith("bottom") ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: labelStyle, children: adLabel }) : null
89
+ ] });
90
+ }
91
+ function AdContainer({
92
+ format,
93
+ provider = "adsterra",
94
+ className,
95
+ adKey,
96
+ adLabel,
97
+ showAdLabel,
98
+ adLabelPosition,
99
+ containerClassName
100
+ }) {
101
+ const [adLoaded, setAdLoaded] = (0, import_react.useState)(false);
102
+ const [adFailed, setAdFailed] = (0, import_react.useState)(false);
103
+ if (adFailed) return null;
104
+ const visibilityStyle = getVisibilityStyle(adLoaded);
105
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `${containerClassName ?? ""}`.trim(), style: visibilityStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
106
+ AdBanner,
107
+ {
108
+ format,
109
+ provider,
110
+ className,
111
+ adKey,
112
+ adLabel,
113
+ showAdLabel,
114
+ adLabelPosition,
115
+ onLoad: () => setAdLoaded(true),
116
+ onError: () => setAdFailed(true)
117
+ }
118
+ ) });
119
+ }
120
+ function parseStyle(styleString) {
121
+ const widthMatch = styleString.match(/width:\s*([^;]+);/);
122
+ const heightMatch = styleString.match(/height:\s*([^;]+);/);
123
+ return {
124
+ width: widthMatch ? widthMatch[1] : void 0,
125
+ height: heightMatch ? heightMatch[1] : void 0,
126
+ display: "flex",
127
+ alignItems: "center",
128
+ justifyContent: "center"
129
+ };
130
+ }
131
+ function getWrapperStyle(position) {
132
+ return {
133
+ display: "inline-flex",
134
+ flexDirection: "column",
135
+ alignItems: position.endsWith("left") ? "flex-start" : position.endsWith("right") ? "flex-end" : "center"
136
+ };
137
+ }
138
+ function getLabelStyle(position) {
139
+ return {
140
+ fontSize: 10,
141
+ fontWeight: 600,
142
+ letterSpacing: "0.1em",
143
+ textTransform: "uppercase",
144
+ color: "#9ca3af",
145
+ marginBottom: position.startsWith("top") ? 4 : 0,
146
+ marginTop: position.startsWith("bottom") ? 4 : 0,
147
+ textAlign: position.endsWith("left") ? "left" : position.endsWith("right") ? "right" : "center",
148
+ width: "100%"
149
+ };
150
+ }
151
+ function getVisibilityStyle(isVisible) {
152
+ if (isVisible) return { opacity: 1, transform: "scale(1)" };
153
+ return {
154
+ opacity: 0,
155
+ transform: "scale(0.95)",
156
+ pointerEvents: "none",
157
+ position: "absolute",
158
+ width: 0,
159
+ height: 0,
160
+ overflow: "hidden"
161
+ };
162
+ }
163
+ var index_default = AdBanner;
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ AdBanner,
167
+ AdContainer,
168
+ configureAds
169
+ });
@@ -0,0 +1,23 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { AdFormat, AdProvider } from '@adsterra-ad/core';
3
+ export { configureAds } from '@adsterra-ad/core';
4
+
5
+ type AdLabelPosition = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
6
+ interface AdBannerProps {
7
+ format: AdFormat;
8
+ provider?: AdProvider;
9
+ className?: string;
10
+ adKey?: string;
11
+ adLabel?: string;
12
+ showAdLabel?: boolean;
13
+ adLabelPosition?: AdLabelPosition;
14
+ onLoad?: () => void;
15
+ onError?: () => void;
16
+ }
17
+ declare function AdBanner({ format, provider, className, adKey, adLabel, showAdLabel, adLabelPosition, onLoad, onError }: AdBannerProps): react_jsx_runtime.JSX.Element | null;
18
+ interface AdContainerProps extends Omit<AdBannerProps, 'onLoad' | 'onError'> {
19
+ containerClassName?: string;
20
+ }
21
+ declare function AdContainer({ format, provider, className, adKey, adLabel, showAdLabel, adLabelPosition, containerClassName }: AdContainerProps): react_jsx_runtime.JSX.Element | null;
22
+
23
+ export { AdBanner, type AdBannerProps, AdContainer, type AdContainerProps, type AdLabelPosition, AdBanner as default };
@@ -0,0 +1,23 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { AdFormat, AdProvider } from '@adsterra-ad/core';
3
+ export { configureAds } from '@adsterra-ad/core';
4
+
5
+ type AdLabelPosition = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
6
+ interface AdBannerProps {
7
+ format: AdFormat;
8
+ provider?: AdProvider;
9
+ className?: string;
10
+ adKey?: string;
11
+ adLabel?: string;
12
+ showAdLabel?: boolean;
13
+ adLabelPosition?: AdLabelPosition;
14
+ onLoad?: () => void;
15
+ onError?: () => void;
16
+ }
17
+ declare function AdBanner({ format, provider, className, adKey, adLabel, showAdLabel, adLabelPosition, onLoad, onError }: AdBannerProps): react_jsx_runtime.JSX.Element | null;
18
+ interface AdContainerProps extends Omit<AdBannerProps, 'onLoad' | 'onError'> {
19
+ containerClassName?: string;
20
+ }
21
+ declare function AdContainer({ format, provider, className, adKey, adLabel, showAdLabel, adLabelPosition, containerClassName }: AdContainerProps): react_jsx_runtime.JSX.Element | null;
22
+
23
+ export { AdBanner, type AdBannerProps, AdContainer, type AdContainerProps, type AdLabelPosition, AdBanner as default };
package/dist/index.js ADDED
@@ -0,0 +1,148 @@
1
+ // src/index.tsx
2
+ import { useEffect, useMemo, useRef, useState } from "react";
3
+ import {
4
+ buildSrcDoc,
5
+ configureAds,
6
+ createBannerId,
7
+ getBannerStyle,
8
+ resolveAdConfig
9
+ } from "@adsterra-ad/core";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ function AdBanner({
12
+ format,
13
+ provider = "adsterra",
14
+ className,
15
+ adKey,
16
+ adLabel = "Advertisement",
17
+ showAdLabel = true,
18
+ adLabelPosition = "top-left",
19
+ onLoad,
20
+ onError
21
+ }) {
22
+ const bannerId = useRef(createBannerId());
23
+ const [activeProvider, setActiveProvider] = useState(provider);
24
+ const [adLoaded, setAdLoaded] = useState(false);
25
+ const [adFailed, setAdFailed] = useState(false);
26
+ useEffect(() => setActiveProvider(provider), [provider]);
27
+ const config = resolveAdConfig(format, adKey);
28
+ const srcDoc = useMemo(
29
+ () => buildSrcDoc({ format, provider: activeProvider, config, bannerId: bannerId.current }),
30
+ [format, activeProvider, config]
31
+ );
32
+ useEffect(() => {
33
+ const handler = (event) => {
34
+ const data = event.data;
35
+ if (!data || data.bannerId !== bannerId.current) return;
36
+ if (data.type === "ad-load-success") {
37
+ setAdLoaded(true);
38
+ onLoad?.();
39
+ return;
40
+ }
41
+ if (data.type === "ad-load-error") {
42
+ setAdFailed(true);
43
+ onError?.();
44
+ }
45
+ };
46
+ window.addEventListener("message", handler);
47
+ return () => window.removeEventListener("message", handler);
48
+ }, [onLoad, onError]);
49
+ if (adFailed) return null;
50
+ const wrapperStyle = getWrapperStyle(adLabelPosition);
51
+ const labelStyle = getLabelStyle(adLabelPosition);
52
+ const visibilityStyle = getVisibilityStyle(adLoaded);
53
+ return /* @__PURE__ */ jsxs("div", { className: `${className ?? ""}`.trim(), style: { ...wrapperStyle, ...visibilityStyle }, children: [
54
+ showAdLabel && adLabelPosition.startsWith("top") ? /* @__PURE__ */ jsx("span", { style: labelStyle, children: adLabel }) : null,
55
+ /* @__PURE__ */ jsx("div", { "data-testid": "ad-banner", style: { ...parseStyle(getBannerStyle(config)), overflow: "hidden", borderRadius: 6, border: "1px solid #e5e7eb", background: "#f3f4f6" }, children: /* @__PURE__ */ jsx(
56
+ "iframe",
57
+ {
58
+ title: "Advertisement",
59
+ srcDoc,
60
+ width: typeof config.width === "number" ? config.width : void 0,
61
+ height: typeof config.height === "number" ? config.height : void 0,
62
+ frameBorder: 0,
63
+ scrolling: "no",
64
+ sandbox: "allow-scripts allow-forms allow-same-origin allow-popups allow-modals"
65
+ }
66
+ ) }),
67
+ showAdLabel && adLabelPosition.startsWith("bottom") ? /* @__PURE__ */ jsx("span", { style: labelStyle, children: adLabel }) : null
68
+ ] });
69
+ }
70
+ function AdContainer({
71
+ format,
72
+ provider = "adsterra",
73
+ className,
74
+ adKey,
75
+ adLabel,
76
+ showAdLabel,
77
+ adLabelPosition,
78
+ containerClassName
79
+ }) {
80
+ const [adLoaded, setAdLoaded] = useState(false);
81
+ const [adFailed, setAdFailed] = useState(false);
82
+ if (adFailed) return null;
83
+ const visibilityStyle = getVisibilityStyle(adLoaded);
84
+ return /* @__PURE__ */ jsx("div", { className: `${containerClassName ?? ""}`.trim(), style: visibilityStyle, children: /* @__PURE__ */ jsx(
85
+ AdBanner,
86
+ {
87
+ format,
88
+ provider,
89
+ className,
90
+ adKey,
91
+ adLabel,
92
+ showAdLabel,
93
+ adLabelPosition,
94
+ onLoad: () => setAdLoaded(true),
95
+ onError: () => setAdFailed(true)
96
+ }
97
+ ) });
98
+ }
99
+ function parseStyle(styleString) {
100
+ const widthMatch = styleString.match(/width:\s*([^;]+);/);
101
+ const heightMatch = styleString.match(/height:\s*([^;]+);/);
102
+ return {
103
+ width: widthMatch ? widthMatch[1] : void 0,
104
+ height: heightMatch ? heightMatch[1] : void 0,
105
+ display: "flex",
106
+ alignItems: "center",
107
+ justifyContent: "center"
108
+ };
109
+ }
110
+ function getWrapperStyle(position) {
111
+ return {
112
+ display: "inline-flex",
113
+ flexDirection: "column",
114
+ alignItems: position.endsWith("left") ? "flex-start" : position.endsWith("right") ? "flex-end" : "center"
115
+ };
116
+ }
117
+ function getLabelStyle(position) {
118
+ return {
119
+ fontSize: 10,
120
+ fontWeight: 600,
121
+ letterSpacing: "0.1em",
122
+ textTransform: "uppercase",
123
+ color: "#9ca3af",
124
+ marginBottom: position.startsWith("top") ? 4 : 0,
125
+ marginTop: position.startsWith("bottom") ? 4 : 0,
126
+ textAlign: position.endsWith("left") ? "left" : position.endsWith("right") ? "right" : "center",
127
+ width: "100%"
128
+ };
129
+ }
130
+ function getVisibilityStyle(isVisible) {
131
+ if (isVisible) return { opacity: 1, transform: "scale(1)" };
132
+ return {
133
+ opacity: 0,
134
+ transform: "scale(0.95)",
135
+ pointerEvents: "none",
136
+ position: "absolute",
137
+ width: 0,
138
+ height: 0,
139
+ overflow: "hidden"
140
+ };
141
+ }
142
+ var index_default = AdBanner;
143
+ export {
144
+ AdBanner,
145
+ AdContainer,
146
+ configureAds,
147
+ index_default as default
148
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@adsterra-ad/react",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "peerDependencies": {
22
+ "react": "^18.0.0 || ^19.0.0"
23
+ },
24
+ "dependencies": {
25
+ "@adsterra-ad/core": "0.1.0"
26
+ },
27
+ "scripts": {
28
+ "build": "tsup src/index.tsx --format esm,cjs --dts",
29
+ "typecheck": "tsc --noEmit",
30
+ "prepublishOnly": "npm run build && npm run typecheck"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react": "^18.3.3",
34
+ "tsup": "^8.2.4",
35
+ "typescript": "^5.5.4"
36
+ }
37
+ }