@folklore/ads 0.0.64 → 0.0.65

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.
Files changed (3) hide show
  1. package/dist/cjs.js +85 -15
  2. package/dist/es.js +84 -16
  3. package/package.json +4 -3
package/dist/cjs.js CHANGED
@@ -14,6 +14,7 @@ var debounce = require('lodash/debounce');
14
14
  var createDebug = require('debug');
15
15
  var EventEmitter = require('wolfy87-eventemitter');
16
16
  var jsxRuntime = require('react/jsx-runtime');
17
+ var utils = require('@folklore/utils');
17
18
  var hooks = require('@folklore/hooks');
18
19
  var tracking = require('@folklore/tracking');
19
20
 
@@ -44,7 +45,7 @@ const adTargeting = PropTypes__default["default"].shape({
44
45
  domain: PropTypes__default["default"].string
45
46
  });
46
47
 
47
- var propTypes$3 = /*#__PURE__*/Object.freeze({
48
+ var propTypes$4 = /*#__PURE__*/Object.freeze({
48
49
  __proto__: null,
49
50
  adPath: adPath,
50
51
  adSize: adSize,
@@ -673,7 +674,7 @@ const AdsContext = /*#__PURE__*/React__default["default"].createContext({
673
674
  ready: false
674
675
  });
675
676
  const useAdsContext = () => React.useContext(AdsContext);
676
- const propTypes$2 = {
677
+ const propTypes$3 = {
677
678
  children: PropTypes__default["default"].node.isRequired,
678
679
  defaultSlotPath: PropTypes__default["default"].string,
679
680
  slotsPath: PropTypes__default["default"].objectOf(PropTypes__default["default"].string),
@@ -688,10 +689,11 @@ const propTypes$2 = {
688
689
  fetchMarginPercent: PropTypes__default["default"].number,
689
690
  slots: adSlots,
690
691
  viewports: adViewports,
692
+ richAdComponents: PropTypes__default["default"].objectOf(PropTypes__default["default"].elementType),
691
693
  disabled: PropTypes__default["default"].bool,
692
694
  disableTracking: PropTypes__default["default"].bool
693
695
  };
694
- const defaultProps$2 = {
696
+ const defaultProps$3 = {
695
697
  defaultSlotPath: null,
696
698
  slotsPath: null,
697
699
  disableSingleRequest: false,
@@ -705,6 +707,7 @@ const defaultProps$2 = {
705
707
  fetchMarginPercent: 300,
706
708
  slots: slots,
707
709
  viewports: viewports,
710
+ richAdComponents: null,
708
711
  disabled: false,
709
712
  disableTracking: false
710
713
  };
@@ -724,6 +727,7 @@ function AdsProvider(_ref) {
724
727
  fetchMarginPercent,
725
728
  viewports,
726
729
  slots,
730
+ richAdComponents,
727
731
  disabled,
728
732
  disableTracking
729
733
  } = _ref;
@@ -796,25 +800,26 @@ function AdsProvider(_ref) {
796
800
  viewports,
797
801
  slots: slotsWithSizeMapping,
798
802
  slotsPath: finalSlotsPath,
799
- trackingDisabled: disableTracking
800
- }), [ready, ads, viewports, slotsWithSizeMapping, finalSlotsPath, disableTracking]);
803
+ trackingDisabled: disableTracking,
804
+ richAdComponents
805
+ }), [ready, ads, viewports, slotsWithSizeMapping, finalSlotsPath, disableTracking, richAdComponents]);
801
806
  return /*#__PURE__*/jsxRuntime.jsx(AdsContext.Provider, {
802
807
  value: value,
803
808
  children: children
804
809
  });
805
810
  }
806
- AdsProvider.propTypes = propTypes$2;
807
- AdsProvider.defaultProps = defaultProps$2;
811
+ AdsProvider.propTypes = propTypes$3;
812
+ AdsProvider.defaultProps = defaultProps$3;
808
813
 
809
814
  const AdsTargetingContext = /*#__PURE__*/React__default["default"].createContext(null);
810
815
  const useAdsTargeting = () => React.useContext(AdsTargetingContext);
811
- const propTypes$1 = {
816
+ const propTypes$2 = {
812
817
  children: PropTypes__default["default"].node.isRequired,
813
818
  // eslint-disable-next-line react/forbid-prop-types
814
819
  targeting: PropTypes__default["default"].object,
815
820
  replace: PropTypes__default["default"].bool
816
821
  };
817
- const defaultProps$1 = {
822
+ const defaultProps$2 = {
818
823
  targeting: {
819
824
  domain: typeof window !== 'undefined' ? `${window.location.protocol}//${window.location.host}` : null
820
825
  },
@@ -836,8 +841,34 @@ function AdsTargetingProvider(_ref) {
836
841
  children: children
837
842
  });
838
843
  }
839
- AdsTargetingProvider.propTypes = propTypes$1;
840
- AdsTargetingProvider.defaultProps = defaultProps$1;
844
+ AdsTargetingProvider.propTypes = propTypes$2;
845
+ AdsTargetingProvider.defaultProps = defaultProps$2;
846
+
847
+ /* eslint-disable react/jsx-props-no-spreading */
848
+ const propTypes$1 = {
849
+ richAd: PropTypes__default["default"].shape({
850
+ type: PropTypes__default["default"].string.isRequired
851
+ }).isRequired
852
+ };
853
+ const defaultProps$1 = {};
854
+ function RichAd(_ref) {
855
+ let {
856
+ richAd
857
+ } = _ref;
858
+ const {
859
+ type = null,
860
+ ...richAdProps
861
+ } = richAd;
862
+ const {
863
+ richAdComponents = null
864
+ } = useAdsContext();
865
+ const RichAdComponent = utils.getComponentFromName(richAdComponents, type);
866
+ return RichAdComponent !== null ? /*#__PURE__*/jsxRuntime.jsx(RichAdComponent, {
867
+ ...richAdProps
868
+ }) : null;
869
+ }
870
+ RichAd.propTypes = propTypes$1;
871
+ RichAd.defaultProps = defaultProps$1;
841
872
 
842
873
  function useAdsTracking() {
843
874
  const tracking$1 = tracking.useTracking() || null;
@@ -1032,6 +1063,34 @@ function useAd(path, size) {
1032
1063
  };
1033
1064
  }
1034
1065
 
1066
+ function useRichAd(containerRef, id) {
1067
+ const [richAd, setRichAd] = React.useState(null);
1068
+ React.useEffect(() => {
1069
+ setRichAd(null);
1070
+ window.addEventListener('message', event => {
1071
+ if (event.origin.match(/safeframe\.googlesyndication\.com/) !== null) {
1072
+ let newRichAd = null;
1073
+ try {
1074
+ const eventData = JSON.parse(event.data) || null;
1075
+ newRichAd = eventData !== null ? eventData.richAd || null : null;
1076
+ } catch (e) {}
1077
+ if (newRichAd !== null) {
1078
+ const container = containerRef.current || document;
1079
+ const iframes = container.querySelector('iframe');
1080
+ for (let i = 0; i < iframes.length; i += 1) {
1081
+ const iframe = iframes[i];
1082
+ if (iframe.contentWindow === event.source) {
1083
+ setRichAd(newRichAd);
1084
+ break;
1085
+ }
1086
+ }
1087
+ }
1088
+ }
1089
+ });
1090
+ }, [id]);
1091
+ return richAd;
1092
+ }
1093
+
1035
1094
  const propTypes = {
1036
1095
  slot: PropTypes__default["default"].string.isRequired,
1037
1096
  path: adPath,
@@ -1048,6 +1107,7 @@ const propTypes = {
1048
1107
  className: PropTypes__default["default"].string,
1049
1108
  emptyClassName: PropTypes__default["default"].string,
1050
1109
  adClassName: PropTypes__default["default"].string,
1110
+ richAdClassName: PropTypes__default["default"].string,
1051
1111
  onRender: PropTypes__default["default"].func,
1052
1112
  onDestroy: PropTypes__default["default"].func,
1053
1113
  slotRef: PropTypes__default["default"].oneOfType([PropTypes__default["default"].func, PropTypes__default["default"].object])
@@ -1067,6 +1127,7 @@ const defaultProps = {
1067
1127
  className: null,
1068
1128
  emptyClassName: null,
1069
1129
  adClassName: null,
1130
+ richAdClassName: null,
1070
1131
  onRender: null,
1071
1132
  onDestroy: null,
1072
1133
  slotRef: null
@@ -1088,6 +1149,7 @@ function Ad(_ref) {
1088
1149
  className,
1089
1150
  emptyClassName,
1090
1151
  adClassName,
1152
+ richAdClassName,
1091
1153
  onRender,
1092
1154
  onDestroy,
1093
1155
  slotRef
@@ -1187,6 +1249,8 @@ function Ad(_ref) {
1187
1249
  disabled: finalDisabled,
1188
1250
  disableTracking
1189
1251
  });
1252
+ const adContainerRef = React.useRef(null);
1253
+ const richAd = useRichAd(adContainerRef, id);
1190
1254
  if (slotRef !== null && isFunction__default["default"](slotRef)) {
1191
1255
  slotRef(slotObject);
1192
1256
  } else if (slotRef !== null && isObject__default["default"](slotRef)) {
@@ -1235,12 +1299,16 @@ function Ad(_ref) {
1235
1299
  }]),
1236
1300
  style: !withoutStyle ? containerStyle : null,
1237
1301
  ref: refObserver,
1238
- children: /*#__PURE__*/jsxRuntime.jsx("div", {
1302
+ children: /*#__PURE__*/jsxRuntime.jsxs("div", {
1239
1303
  className: adClassName,
1240
1304
  style: adStyle,
1241
- children: /*#__PURE__*/jsxRuntime.jsx("div", {
1305
+ ref: adContainerRef,
1306
+ children: [/*#__PURE__*/jsxRuntime.jsx("div", {
1242
1307
  id: id
1243
- })
1308
+ }), isRendered && richAd !== null ? /*#__PURE__*/jsxRuntime.jsx(RichAd, {
1309
+ richAd: richAd,
1310
+ className: richAdClassName
1311
+ }) : null]
1244
1312
  })
1245
1313
  });
1246
1314
  }
@@ -1252,7 +1320,8 @@ exports.AdSlot = AdSlot;
1252
1320
  exports.AdsManager = AdsManager;
1253
1321
  exports.AdsProvider = AdsProvider;
1254
1322
  exports.AdsTargetingProvider = AdsTargetingProvider;
1255
- exports.PropTypes = propTypes$3;
1323
+ exports.PropTypes = propTypes$4;
1324
+ exports.RichAd = RichAd;
1256
1325
  exports.buildSizeMappingFromSizes = buildSizeMappingFromSizes;
1257
1326
  exports.buildSizeMappingFromViewports = buildSizeMappingFromViewports;
1258
1327
  exports.getAdSizes = getAdSizes;
@@ -1265,4 +1334,5 @@ exports.useAd = useAd;
1265
1334
  exports.useAdsContext = useAdsContext;
1266
1335
  exports.useAdsTargeting = useAdsTargeting;
1267
1336
  exports.useAdsTracking = useAdsTracking;
1337
+ exports.useRichAd = useRichAd;
1268
1338
  exports.viewports = viewports;
package/dist/es.js CHANGED
@@ -9,7 +9,8 @@ import uniqBy from 'lodash/uniqBy';
9
9
  import debounce from 'lodash/debounce';
10
10
  import createDebug from 'debug';
11
11
  import EventEmitter from 'wolfy87-eventemitter';
12
- import { jsx } from 'react/jsx-runtime';
12
+ import { jsx, jsxs } from 'react/jsx-runtime';
13
+ import { getComponentFromName } from '@folklore/utils';
13
14
  import { useIntersectionObserver, useWindowEvent } from '@folklore/hooks';
14
15
  import { useTracking } from '@folklore/tracking';
15
16
 
@@ -26,7 +27,7 @@ const adTargeting = PropTypes.shape({
26
27
  domain: PropTypes.string
27
28
  });
28
29
 
29
- var propTypes$3 = /*#__PURE__*/Object.freeze({
30
+ var propTypes$4 = /*#__PURE__*/Object.freeze({
30
31
  __proto__: null,
31
32
  adPath: adPath,
32
33
  adSize: adSize,
@@ -655,7 +656,7 @@ const AdsContext = /*#__PURE__*/React.createContext({
655
656
  ready: false
656
657
  });
657
658
  const useAdsContext = () => useContext(AdsContext);
658
- const propTypes$2 = {
659
+ const propTypes$3 = {
659
660
  children: PropTypes.node.isRequired,
660
661
  defaultSlotPath: PropTypes.string,
661
662
  slotsPath: PropTypes.objectOf(PropTypes.string),
@@ -670,10 +671,11 @@ const propTypes$2 = {
670
671
  fetchMarginPercent: PropTypes.number,
671
672
  slots: adSlots,
672
673
  viewports: adViewports,
674
+ richAdComponents: PropTypes.objectOf(PropTypes.elementType),
673
675
  disabled: PropTypes.bool,
674
676
  disableTracking: PropTypes.bool
675
677
  };
676
- const defaultProps$2 = {
678
+ const defaultProps$3 = {
677
679
  defaultSlotPath: null,
678
680
  slotsPath: null,
679
681
  disableSingleRequest: false,
@@ -687,6 +689,7 @@ const defaultProps$2 = {
687
689
  fetchMarginPercent: 300,
688
690
  slots: slots,
689
691
  viewports: viewports,
692
+ richAdComponents: null,
690
693
  disabled: false,
691
694
  disableTracking: false
692
695
  };
@@ -706,6 +709,7 @@ function AdsProvider(_ref) {
706
709
  fetchMarginPercent,
707
710
  viewports,
708
711
  slots,
712
+ richAdComponents,
709
713
  disabled,
710
714
  disableTracking
711
715
  } = _ref;
@@ -778,25 +782,26 @@ function AdsProvider(_ref) {
778
782
  viewports,
779
783
  slots: slotsWithSizeMapping,
780
784
  slotsPath: finalSlotsPath,
781
- trackingDisabled: disableTracking
782
- }), [ready, ads, viewports, slotsWithSizeMapping, finalSlotsPath, disableTracking]);
785
+ trackingDisabled: disableTracking,
786
+ richAdComponents
787
+ }), [ready, ads, viewports, slotsWithSizeMapping, finalSlotsPath, disableTracking, richAdComponents]);
783
788
  return /*#__PURE__*/jsx(AdsContext.Provider, {
784
789
  value: value,
785
790
  children: children
786
791
  });
787
792
  }
788
- AdsProvider.propTypes = propTypes$2;
789
- AdsProvider.defaultProps = defaultProps$2;
793
+ AdsProvider.propTypes = propTypes$3;
794
+ AdsProvider.defaultProps = defaultProps$3;
790
795
 
791
796
  const AdsTargetingContext = /*#__PURE__*/React.createContext(null);
792
797
  const useAdsTargeting = () => useContext(AdsTargetingContext);
793
- const propTypes$1 = {
798
+ const propTypes$2 = {
794
799
  children: PropTypes.node.isRequired,
795
800
  // eslint-disable-next-line react/forbid-prop-types
796
801
  targeting: PropTypes.object,
797
802
  replace: PropTypes.bool
798
803
  };
799
- const defaultProps$1 = {
804
+ const defaultProps$2 = {
800
805
  targeting: {
801
806
  domain: typeof window !== 'undefined' ? `${window.location.protocol}//${window.location.host}` : null
802
807
  },
@@ -818,8 +823,34 @@ function AdsTargetingProvider(_ref) {
818
823
  children: children
819
824
  });
820
825
  }
821
- AdsTargetingProvider.propTypes = propTypes$1;
822
- AdsTargetingProvider.defaultProps = defaultProps$1;
826
+ AdsTargetingProvider.propTypes = propTypes$2;
827
+ AdsTargetingProvider.defaultProps = defaultProps$2;
828
+
829
+ /* eslint-disable react/jsx-props-no-spreading */
830
+ const propTypes$1 = {
831
+ richAd: PropTypes.shape({
832
+ type: PropTypes.string.isRequired
833
+ }).isRequired
834
+ };
835
+ const defaultProps$1 = {};
836
+ function RichAd(_ref) {
837
+ let {
838
+ richAd
839
+ } = _ref;
840
+ const {
841
+ type = null,
842
+ ...richAdProps
843
+ } = richAd;
844
+ const {
845
+ richAdComponents = null
846
+ } = useAdsContext();
847
+ const RichAdComponent = getComponentFromName(richAdComponents, type);
848
+ return RichAdComponent !== null ? /*#__PURE__*/jsx(RichAdComponent, {
849
+ ...richAdProps
850
+ }) : null;
851
+ }
852
+ RichAd.propTypes = propTypes$1;
853
+ RichAd.defaultProps = defaultProps$1;
823
854
 
824
855
  function useAdsTracking() {
825
856
  const tracking = useTracking() || null;
@@ -1014,6 +1045,34 @@ function useAd(path, size) {
1014
1045
  };
1015
1046
  }
1016
1047
 
1048
+ function useRichAd(containerRef, id) {
1049
+ const [richAd, setRichAd] = useState(null);
1050
+ useEffect(() => {
1051
+ setRichAd(null);
1052
+ window.addEventListener('message', event => {
1053
+ if (event.origin.match(/safeframe\.googlesyndication\.com/) !== null) {
1054
+ let newRichAd = null;
1055
+ try {
1056
+ const eventData = JSON.parse(event.data) || null;
1057
+ newRichAd = eventData !== null ? eventData.richAd || null : null;
1058
+ } catch (e) {}
1059
+ if (newRichAd !== null) {
1060
+ const container = containerRef.current || document;
1061
+ const iframes = container.querySelector('iframe');
1062
+ for (let i = 0; i < iframes.length; i += 1) {
1063
+ const iframe = iframes[i];
1064
+ if (iframe.contentWindow === event.source) {
1065
+ setRichAd(newRichAd);
1066
+ break;
1067
+ }
1068
+ }
1069
+ }
1070
+ }
1071
+ });
1072
+ }, [id]);
1073
+ return richAd;
1074
+ }
1075
+
1017
1076
  const propTypes = {
1018
1077
  slot: PropTypes.string.isRequired,
1019
1078
  path: adPath,
@@ -1030,6 +1089,7 @@ const propTypes = {
1030
1089
  className: PropTypes.string,
1031
1090
  emptyClassName: PropTypes.string,
1032
1091
  adClassName: PropTypes.string,
1092
+ richAdClassName: PropTypes.string,
1033
1093
  onRender: PropTypes.func,
1034
1094
  onDestroy: PropTypes.func,
1035
1095
  slotRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
@@ -1049,6 +1109,7 @@ const defaultProps = {
1049
1109
  className: null,
1050
1110
  emptyClassName: null,
1051
1111
  adClassName: null,
1112
+ richAdClassName: null,
1052
1113
  onRender: null,
1053
1114
  onDestroy: null,
1054
1115
  slotRef: null
@@ -1070,6 +1131,7 @@ function Ad(_ref) {
1070
1131
  className,
1071
1132
  emptyClassName,
1072
1133
  adClassName,
1134
+ richAdClassName,
1073
1135
  onRender,
1074
1136
  onDestroy,
1075
1137
  slotRef
@@ -1169,6 +1231,8 @@ function Ad(_ref) {
1169
1231
  disabled: finalDisabled,
1170
1232
  disableTracking
1171
1233
  });
1234
+ const adContainerRef = useRef(null);
1235
+ const richAd = useRichAd(adContainerRef, id);
1172
1236
  if (slotRef !== null && isFunction(slotRef)) {
1173
1237
  slotRef(slotObject);
1174
1238
  } else if (slotRef !== null && isObject(slotRef)) {
@@ -1217,16 +1281,20 @@ function Ad(_ref) {
1217
1281
  }]),
1218
1282
  style: !withoutStyle ? containerStyle : null,
1219
1283
  ref: refObserver,
1220
- children: /*#__PURE__*/jsx("div", {
1284
+ children: /*#__PURE__*/jsxs("div", {
1221
1285
  className: adClassName,
1222
1286
  style: adStyle,
1223
- children: /*#__PURE__*/jsx("div", {
1287
+ ref: adContainerRef,
1288
+ children: [/*#__PURE__*/jsx("div", {
1224
1289
  id: id
1225
- })
1290
+ }), isRendered && richAd !== null ? /*#__PURE__*/jsx(RichAd, {
1291
+ richAd: richAd,
1292
+ className: richAdClassName
1293
+ }) : null]
1226
1294
  })
1227
1295
  });
1228
1296
  }
1229
1297
  Ad.propTypes = propTypes;
1230
1298
  Ad.defaultProps = defaultProps;
1231
1299
 
1232
- export { Ad, AdSlot, AdsManager, AdsProvider, AdsTargetingProvider, propTypes$3 as PropTypes, buildSizeMappingFromSizes, buildSizeMappingFromViewports, getAdSizes, getMinimumAdSize, getSizeMappingFromSlot, getSortedViewports, sizeFitsInViewport, slots, useAd, useAdsContext, useAdsTargeting, useAdsTracking, viewports };
1300
+ export { Ad, AdSlot, AdsManager, AdsProvider, AdsTargetingProvider, propTypes$4 as PropTypes, RichAd, buildSizeMappingFromSizes, buildSizeMappingFromViewports, getAdSizes, getMinimumAdSize, getSizeMappingFromSlot, getSortedViewports, sizeFitsInViewport, slots, useAd, useAdsContext, useAdsTargeting, useAdsTracking, useRichAd, viewports };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/ads",
3
- "version": "0.0.64",
3
+ "version": "0.0.65",
4
4
  "description": "Ads library",
5
5
  "keywords": [
6
6
  "javascript",
@@ -50,10 +50,11 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "774cbc9e93dc59d16e4b503264a3fbe1cca945d3",
53
+ "gitHead": "c054f8aebebb312df15dbd87158516bbc750855f",
54
54
  "dependencies": {
55
- "@folklore/hooks": "^0.0.58",
55
+ "@folklore/hooks": "^0.0.59",
56
56
  "@folklore/tracking": "^0.0.23",
57
+ "@folklore/utils": "^0.1.1",
57
58
  "classnames": "^2.5.1",
58
59
  "debug": "^4.3.4",
59
60
  "lodash": "^4.17.21",