@folklore/ads 0.0.109 → 0.0.111

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/cjs.js CHANGED
@@ -2,10 +2,9 @@
2
2
 
3
3
  var PropTypes = require('prop-types');
4
4
  var classNames = require('classnames');
5
- var isFunction = require('lodash/isFunction');
6
- var isObject = require('lodash/isObject');
7
5
  var React = require('react');
8
6
  var isArray = require('lodash/isArray');
7
+ var isObject = require('lodash/isObject');
9
8
  var sortBy = require('lodash/sortBy');
10
9
  var uniqBy = require('lodash/uniqBy');
11
10
  var debounce = require('lodash/debounce');
@@ -29,7 +28,7 @@ const adTargeting = PropTypes.shape({
29
28
  domain: PropTypes.string
30
29
  });
31
30
 
32
- var propTypes$4 = /*#__PURE__*/Object.freeze({
31
+ var propTypes = /*#__PURE__*/Object.freeze({
33
32
  __proto__: null,
34
33
  adPath: adPath,
35
34
  adSize: adSize,
@@ -52,42 +51,45 @@ function normalizeAdSizes(size) {
52
51
  function getAdSizes(sizes) {
53
52
  return uniqBy(sizes, size => isArray(size) ? size.join('x') : size);
54
53
  }
55
- const getMinimumAdSize = sizes => getAdSizes(sizes).filter(size => size !== 'fluid').reduce((minimumSize, size) => ({
56
- width: Math.min(minimumSize.width, size[0]),
57
- height: Math.min(minimumSize.height, size[1])
58
- }), {
59
- width: Infinity,
60
- height: Infinity
61
- });
62
- const sizeFitsInViewport = (size, viewport) => size === 'fluid' && viewport[0] > 600 || size !== 'fluid' && (viewport[0] === 0 || size[0] <= viewport[0]) && (viewport[1] === 0 || size[1] <= viewport[1]);
63
- const getSortedViewports = viewports => sortBy(Object.keys(viewports).map(name => ({
64
- name,
65
- size: viewports[name]
66
- })), [viewport => viewport.size[0]]).reverse();
67
- const buildSizeMappingFromViewports = (sizeMapping, viewports) => isObject(sizeMapping) && !isArray(sizeMapping) ? getSortedViewports(viewports).reduce((newSizeMapping, _ref) => {
68
- let {
54
+ function getMinimumAdSize(sizes) {
55
+ return getAdSizes(sizes).filter(size => size !== 'fluid').reduce((minimumSize, size) => ({
56
+ width: Math.min(minimumSize.width, size[0]),
57
+ height: Math.min(minimumSize.height, size[1])
58
+ }), {
59
+ width: Infinity,
60
+ height: Infinity
61
+ });
62
+ }
63
+ function sizeFitsInViewport(size, viewport) {
64
+ return size === 'fluid' && viewport[0] > 600 || size !== 'fluid' && (viewport[0] === 0 || size[0] <= viewport[0]) && (viewport[1] === 0 || size[1] <= viewport[1]);
65
+ }
66
+ function getSortedViewports(viewports) {
67
+ return sortBy(Object.keys(viewports).map(name => ({
68
+ name,
69
+ size: viewports[name]
70
+ })), [viewport => viewport.size[0]]).reverse();
71
+ }
72
+ function buildSizeMappingFromViewports(sizeMapping, viewports) {
73
+ return isObject(sizeMapping) && !isArray(sizeMapping) ? getSortedViewports(viewports).reduce((newSizeMapping, {
69
74
  name,
70
75
  size: viewPortSize
71
- } = _ref;
72
- return typeof sizeMapping[name] !== 'undefined' ? [...newSizeMapping, [viewPortSize, sizeMapping[name]]] : newSizeMapping;
73
- }, []) : sizeMapping;
74
- const buildSizeMappingFromSizes = (sizes, viewports) => getSortedViewports(viewports).map(_ref2 => {
75
- let {
76
+ }) => typeof sizeMapping[name] !== 'undefined' ? [...newSizeMapping, [viewPortSize, sizeMapping[name]]] : newSizeMapping, []) : sizeMapping;
77
+ }
78
+ function buildSizeMappingFromSizes(sizes, viewports) {
79
+ return getSortedViewports(viewports).map(({
76
80
  name,
77
81
  size: viewPortSize
78
- } = _ref2;
79
- return [viewPortSize, sizes.filter(size => sizeFitsInViewport(size, name === 'default' ? [300, 300] : viewPortSize))];
80
- });
81
- const getSizeMappingFromSlot = (_ref3, viewports) => {
82
- let {
83
- size: allSizes = [],
84
- sizeMapping = null
85
- } = _ref3;
82
+ }) => [viewPortSize, sizes.filter(size => sizeFitsInViewport(size, name === 'default' ? [300, 300] : viewPortSize))]);
83
+ }
84
+ function getSizeMappingFromSlot({
85
+ size: allSizes = [],
86
+ sizeMapping = null
87
+ }, viewports) {
86
88
  if (sizeMapping === true) {
87
89
  return buildSizeMappingFromSizes(allSizes, viewports);
88
90
  }
89
91
  return sizeMapping !== null ? buildSizeMappingFromViewports(sizeMapping, viewports) : null;
90
- };
92
+ }
91
93
  function getSizeFromSizeMapping(sizeMapping) {
92
94
  if (sizeMapping === null) {
93
95
  return null;
@@ -107,8 +109,7 @@ function getSizeFromSizeMapping(sizeMapping) {
107
109
  }
108
110
 
109
111
  class AdSlot extends EventEmitter {
110
- constructor(id, path, size) {
111
- let opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
112
+ constructor(id, path, size, opts = {}) {
112
113
  super();
113
114
  this.options = {
114
115
  sizeMapping: null,
@@ -336,8 +337,7 @@ class AdsManager extends EventEmitter {
336
337
  [index.id]: [item.slug]
337
338
  } : null;
338
339
  }
339
- constructor() {
340
- let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
340
+ constructor(opts = {}) {
341
341
  super();
342
342
  this.options = {
343
343
  disabled: false,
@@ -505,8 +505,7 @@ class AdsManager extends EventEmitter {
505
505
  googletag.pubads().setRequestNonPersonalizedAds(disablePersonnalizedAds ? 1 : 0);
506
506
  });
507
507
  }
508
- createSlot(path, size) {
509
- let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
508
+ createSlot(path, size, opts = {}) {
510
509
  const {
511
510
  id: providedId = null
512
511
  } = opts;
@@ -683,66 +682,26 @@ const AdsContext = /*#__PURE__*/React.createContext({
683
682
  ready: false
684
683
  });
685
684
  const useAdsContext = () => React.useContext(AdsContext);
686
- const propTypes$3 = {
687
- children: PropTypes.node.isRequired,
688
- defaultSlotPath: PropTypes.string,
689
- slotsPath: PropTypes.objectOf(PropTypes.string),
690
- disableSingleRequest: PropTypes.bool,
691
- disableVideoAds: PropTypes.bool,
692
- disableLazyLoad: PropTypes.bool,
693
- autoInit: PropTypes.bool,
694
- resizeDebounceDelay: PropTypes.number,
695
- refreshOnResize: PropTypes.bool,
696
- mobileScaling: PropTypes.number,
697
- renderMarginPercent: PropTypes.number,
698
- fetchMarginPercent: PropTypes.number,
699
- viewport: PropTypes.string,
700
- slots: adSlots,
701
- viewports: adViewports,
702
- richAdComponents: PropTypes.objectOf(PropTypes.elementType),
703
- disabled: PropTypes.bool,
704
- disableTracking: PropTypes.bool
705
- };
706
- const defaultProps$2 = {
707
- defaultSlotPath: null,
708
- slotsPath: null,
709
- disableSingleRequest: false,
710
- disableVideoAds: false,
711
- disableLazyLoad: false,
712
- autoInit: true,
713
- resizeDebounceDelay: 500,
714
- refreshOnResize: false,
715
- mobileScaling: 1.0,
716
- renderMarginPercent: 100,
717
- fetchMarginPercent: 300,
718
- viewport: null,
719
- slots: slots,
720
- viewports: viewports,
721
- richAdComponents: null,
722
- disabled: false,
723
- disableTracking: false
724
- };
725
- function AdsProvider(_ref) {
726
- let {
727
- children,
728
- defaultSlotPath,
729
- slotsPath,
730
- disableSingleRequest,
731
- disableVideoAds,
732
- disableLazyLoad,
733
- autoInit,
734
- resizeDebounceDelay,
735
- refreshOnResize,
736
- mobileScaling,
737
- renderMarginPercent,
738
- fetchMarginPercent,
739
- viewport,
740
- viewports,
741
- slots,
742
- richAdComponents,
743
- disabled,
744
- disableTracking
745
- } = _ref;
685
+ function AdsProvider({
686
+ children,
687
+ defaultSlotPath = null,
688
+ slotsPath = null,
689
+ disableSingleRequest = false,
690
+ disableVideoAds = false,
691
+ disableLazyLoad = false,
692
+ autoInit = true,
693
+ resizeDebounceDelay = 500,
694
+ refreshOnResize = false,
695
+ mobileScaling = 1.0,
696
+ renderMarginPercent = 100,
697
+ fetchMarginPercent = 300,
698
+ viewport = null,
699
+ viewports: viewports$1 = viewports,
700
+ slots: slots$1 = slots,
701
+ richAdComponents = null,
702
+ disabled = false,
703
+ disableTracking = false
704
+ }) {
746
705
  const [ready, setReady] = React.useState(false);
747
706
  const adsRef = React.useRef(null);
748
707
  const ads = React.useMemo(() => {
@@ -793,12 +752,12 @@ function AdsProvider(_ref) {
793
752
  onResize.cancel();
794
753
  };
795
754
  }, [ads, resizeDebounceDelay, refreshOnResize]);
796
- const slotsWithSizeMapping = React.useMemo(() => Object.keys(slots || {}).reduce((map, key) => {
797
- const slot = slots[key];
755
+ const slotsWithSizeMapping = React.useMemo(() => Object.keys(slots$1 || {}).reduce((map, key) => {
756
+ const slot = slots$1[key];
798
757
  const {
799
758
  size
800
759
  } = slot;
801
- const sizeMapping = getSizeMappingFromSlot(slot, viewports);
760
+ const sizeMapping = getSizeMappingFromSlot(slot, viewports$1);
802
761
  return {
803
762
  ...map,
804
763
  [key]: {
@@ -807,51 +766,44 @@ function AdsProvider(_ref) {
807
766
  sizeMapping
808
767
  }
809
768
  };
810
- }, {}), []);
811
- const finalSlotsPath = React.useMemo(() => defaultSlotPath !== null ? {
812
- default: defaultSlotPath,
813
- ...slotsPath
814
- } : {
815
- ...slotsPath
769
+ }, {}), [slots$1, viewports$1]);
770
+ const finalSlotsPath = React.useMemo(() => {
771
+ if (defaultSlotPath !== null && slotsPath) {
772
+ return {
773
+ default: defaultSlotPath,
774
+ ...slotsPath
775
+ };
776
+ }
777
+ return slotsPath ? {
778
+ ...slotsPath
779
+ } : {};
816
780
  }, [defaultSlotPath, slotsPath]);
817
781
  const value = React.useMemo(() => ({
818
782
  ready,
819
783
  ads,
820
- viewports,
784
+ viewports: viewports$1,
821
785
  viewport,
822
786
  slots: slotsWithSizeMapping,
823
787
  slotsPath: finalSlotsPath,
824
788
  trackingDisabled: disableTracking,
825
789
  richAdComponents
826
- }), [ready, ads, viewports, viewport, slotsWithSizeMapping, finalSlotsPath, disableTracking, richAdComponents]);
790
+ }), [ready, ads, viewports$1, viewport, slotsWithSizeMapping, finalSlotsPath, disableTracking, richAdComponents]);
827
791
  return /*#__PURE__*/jsxRuntime.jsx(AdsContext.Provider, {
828
792
  value: value,
829
793
  children: children
830
794
  });
831
795
  }
832
- AdsProvider.propTypes = propTypes$3;
833
- AdsProvider.defaultProps = defaultProps$2;
834
796
 
835
- const AdsTargetingContext = /*#__PURE__*/React.createContext(null);
836
- const useAdsTargeting = () => React.useContext(AdsTargetingContext);
837
- const propTypes$2 = {
838
- children: PropTypes.node.isRequired,
839
- // eslint-disable-next-line react/forbid-prop-types
840
- targeting: PropTypes.object,
841
- replace: PropTypes.bool
797
+ const defaultTargeting = {
798
+ domain: typeof window !== 'undefined' ? `${window.location.protocol}//${window.location.host}` : null
842
799
  };
843
- const defaultProps$1 = {
844
- targeting: {
845
- domain: typeof window !== 'undefined' ? `${window.location.protocol}//${window.location.host}` : null
846
- },
847
- replace: false
848
- };
849
- function AdsTargetingProvider(_ref) {
850
- let {
851
- children,
852
- targeting,
853
- replace
854
- } = _ref;
800
+ const AdsTargetingContext = /*#__PURE__*/React.createContext(defaultTargeting);
801
+ const useAdsTargeting = () => React.useContext(AdsTargetingContext);
802
+ function AdsTargetingProvider({
803
+ children,
804
+ targeting = defaultTargeting,
805
+ replace = false
806
+ }) {
855
807
  const previousTargeting = useAdsTargeting();
856
808
  const mergedTargeting = React.useMemo(() => replace ? targeting : {
857
809
  ...previousTargeting,
@@ -862,21 +814,12 @@ function AdsTargetingProvider(_ref) {
862
814
  children: children
863
815
  });
864
816
  }
865
- AdsTargetingProvider.propTypes = propTypes$2;
866
- AdsTargetingProvider.defaultProps = defaultProps$1;
867
817
 
868
818
  /* eslint-disable react/jsx-props-no-spreading */
869
- const propTypes$1 = {
870
- richAd: PropTypes.shape({
871
- type: PropTypes.string.isRequired
872
- }).isRequired
873
- };
874
- const defaultProps = {};
875
- function RichAd(_ref) {
876
- let {
877
- richAd,
878
- ...props
879
- } = _ref;
819
+ function RichAd({
820
+ richAd,
821
+ ...props
822
+ }) {
880
823
  const {
881
824
  type = null,
882
825
  ...richAdProps
@@ -890,12 +833,10 @@ function RichAd(_ref) {
890
833
  ...richAdProps
891
834
  }) : null;
892
835
  }
893
- RichAd.propTypes = propTypes$1;
894
- RichAd.defaultProps = defaultProps;
895
836
 
896
837
  function useAdsTracking() {
897
838
  const tracking$1 = tracking.useTracking() || null;
898
- const trackEvent = React.useCallback((action, slot, renderEvent) => {
839
+ const trackEvent = React.useCallback((action, slot = null, renderEvent = null) => {
899
840
  if (tracking$1 !== null && typeof tracking$1.trackAd !== 'undefined') {
900
841
  tracking$1.trackAd(action, slot, renderEvent);
901
842
  }
@@ -903,21 +844,20 @@ function useAdsTracking() {
903
844
  return trackEvent;
904
845
  }
905
846
 
906
- function useAd(path, size) {
907
- let {
908
- id,
909
- sizeMapping = null,
910
- viewport = null,
911
- targeting = null,
912
- categoryExclusions = null,
913
- refreshInterval = null,
914
- alwaysRender = false,
915
- onRender = null,
916
- onDestroy = null,
917
- disabled = false,
918
- disableTracking = false,
919
- rootMargin = '300px'
920
- } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
847
+ function useAd(path, size, {
848
+ id,
849
+ sizeMapping = null,
850
+ viewport = null,
851
+ targeting = null,
852
+ categoryExclusions = null,
853
+ refreshInterval = null,
854
+ alwaysRender = false,
855
+ onRender = null,
856
+ onDestroy = null,
857
+ disabled = false,
858
+ disableTracking = false,
859
+ rootMargin = '300px'
860
+ } = {}) {
921
861
  const {
922
862
  ads: adsManager,
923
863
  viewports,
@@ -925,9 +865,9 @@ function useAd(path, size) {
925
865
  trackingDisabled: globalTrackingDisabled = false
926
866
  } = useAdsContext();
927
867
  const trackAd = useAdsTracking();
928
- const track = React.useCallback(function () {
868
+ const track = React.useCallback((action, slot = null, renderEvent = null) => {
929
869
  if (!disableTracking && !globalTrackingDisabled) {
930
- trackAd(...arguments);
870
+ trackAd(action, slot, renderEvent);
931
871
  }
932
872
  }, [disableTracking, globalTrackingDisabled, trackAd]);
933
873
 
@@ -935,7 +875,7 @@ function useAd(path, size) {
935
875
  const {
936
876
  ref: refObserver,
937
877
  entry: {
938
- isIntersecting
878
+ isIntersecting = false
939
879
  }
940
880
  } = hooks.useIntersectionObserver({
941
881
  rootMargin,
@@ -965,10 +905,7 @@ function useAd(path, size) {
965
905
  adsManager.destroySlot(currentSlot);
966
906
  }
967
907
  const viewportSize = viewport !== null ? viewports[viewport] || null : null;
968
- const [, viewportFixedSize = null] = sizeMapping !== null && viewportSize !== null ? sizeMapping.find(_ref => {
969
- let [itViewport] = _ref;
970
- return itViewport.join('x') === viewportSize.join('x');
971
- }) || [] : [];
908
+ const [, viewportFixedSize = null] = sizeMapping !== null && viewportSize !== null ? sizeMapping.find(([itViewport]) => itViewport.join('x') === viewportSize.join('x')) || [] : [];
972
909
  const newSlot = path !== null && !disabled ? adsManager.createSlot(path, viewportFixedSize || size, {
973
910
  id,
974
911
  visible: isVisible,
@@ -1115,7 +1052,7 @@ function parseRichAd(data) {
1115
1052
  } catch (e) {}
1116
1053
  return richAd;
1117
1054
  }
1118
- function useRichAd(containerRef, id, opts) {
1055
+ function useRichAd(containerRef, id, opts = {}) {
1119
1056
  const [richAd, setRichAd] = React.useState(null);
1120
1057
  const {
1121
1058
  onRichAd = null
@@ -1153,57 +1090,31 @@ function useRichAd(containerRef, id, opts) {
1153
1090
  }
1154
1091
 
1155
1092
  /* eslint-disable react/require-default-props */
1156
- const propTypes = {
1157
- slot: PropTypes.string.isRequired,
1158
- path: adPath,
1159
- size: adSize,
1160
- sizeMapping: adSizeMapping,
1161
- viewport: PropTypes.string,
1162
- targeting: adTargeting,
1163
- refreshInterval: PropTypes.number,
1164
- alwaysRender: PropTypes.bool,
1165
- disabled: PropTypes.bool,
1166
- disableTracking: PropTypes.bool,
1167
- shouldKeepSize: PropTypes.bool,
1168
- withoutStyle: PropTypes.bool,
1169
- withoutMinimumSize: PropTypes.bool,
1170
- withReactId: PropTypes.bool,
1171
- className: PropTypes.string,
1172
- emptyClassName: PropTypes.string,
1173
- adClassName: PropTypes.string,
1174
- richAdClassName: PropTypes.string,
1175
- richAdIframeClassName: PropTypes.string,
1176
- onRender: PropTypes.func,
1177
- onDestroy: PropTypes.func,
1178
- onRichAd: PropTypes.func,
1179
- slotRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
1180
- };
1181
- function Ad(_ref) {
1182
- let {
1183
- slot: slotName = null,
1184
- path: providedPath = null,
1185
- size: providedSize = null,
1186
- sizeMapping: providedSizeMapping = null,
1187
- viewport: providedViewport = null,
1188
- targeting: providedTargeting = null,
1189
- refreshInterval: providedRefreshInterval = null,
1190
- alwaysRender = true,
1191
- disabled: providedDisabled = false,
1192
- disableTracking = false,
1193
- shouldKeepSize = false,
1194
- withoutStyle = false,
1195
- withoutMinimumSize = false,
1196
- withReactId = false,
1197
- className = null,
1198
- emptyClassName = null,
1199
- adClassName = null,
1200
- richAdClassName = null,
1201
- richAdIframeClassName = null,
1202
- onRender = null,
1203
- onDestroy = null,
1204
- onRichAd = null,
1205
- slotRef = null
1206
- } = _ref;
1093
+ function Ad({
1094
+ slot: slotName,
1095
+ path: providedPath = null,
1096
+ size: providedSize = null,
1097
+ sizeMapping: providedSizeMapping = null,
1098
+ viewport: providedViewport = null,
1099
+ targeting: providedTargeting = null,
1100
+ refreshInterval: providedRefreshInterval = null,
1101
+ alwaysRender = true,
1102
+ disabled: providedDisabled = false,
1103
+ disableTracking = false,
1104
+ shouldKeepSize = false,
1105
+ withoutStyle = false,
1106
+ withoutMinimumSize = false,
1107
+ withReactId = false,
1108
+ className = null,
1109
+ emptyClassName = null,
1110
+ adClassName = null,
1111
+ richAdClassName = null,
1112
+ richAdIframeClassName = null,
1113
+ onRender = null,
1114
+ onDestroy = null,
1115
+ onRichAd = null,
1116
+ slotRef = null
1117
+ }) {
1207
1118
  const {
1208
1119
  slots = null,
1209
1120
  slotsPath = null,
@@ -1213,7 +1124,7 @@ function Ad(_ref) {
1213
1124
  const {
1214
1125
  default: defaultSlotPath = null
1215
1126
  } = slotsPath || {};
1216
- const slot = slotName !== null && slots !== null ? slots[slotName] || null : null;
1127
+ const slot = slotName && slots !== null ? slots[slotName] || null : null;
1217
1128
  const {
1218
1129
  sizeMapping: slotSizeMapping = null,
1219
1130
  size: slotSize = null,
@@ -1312,9 +1223,9 @@ function Ad(_ref) {
1312
1223
  const richAd = useRichAd(adContainerRef, id, {
1313
1224
  onRichAd
1314
1225
  });
1315
- if (slotRef !== null && isFunction(slotRef)) {
1226
+ if (slotRef !== null && typeof slotRef === 'function') {
1316
1227
  slotRef(slotObject);
1317
- } else if (slotRef !== null && isObject(slotRef)) {
1228
+ } else if (slotRef !== null && typeof slotRef === 'object') {
1318
1229
  // eslint-disable-next-line no-param-reassign
1319
1230
  slotRef.current = slotObject;
1320
1231
  }
@@ -1376,14 +1287,13 @@ function Ad(_ref) {
1376
1287
  })
1377
1288
  });
1378
1289
  }
1379
- Ad.propTypes = propTypes;
1380
1290
 
1381
1291
  exports.Ad = Ad;
1382
1292
  exports.AdSlot = AdSlot;
1383
1293
  exports.AdsManager = AdsManager;
1384
1294
  exports.AdsProvider = AdsProvider;
1385
1295
  exports.AdsTargetingProvider = AdsTargetingProvider;
1386
- exports.PropTypes = propTypes$4;
1296
+ exports.PropTypes = propTypes;
1387
1297
  exports.RichAd = RichAd;
1388
1298
  exports.buildSizeMappingFromSizes = buildSizeMappingFromSizes;
1389
1299
  exports.buildSizeMappingFromViewports = buildSizeMappingFromViewports;