@folklore/ads 0.0.13 → 0.0.15

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 +93 -16
  2. package/dist/es.js +92 -15
  3. package/package.json +2 -2
package/dist/cjs.js CHANGED
@@ -7,10 +7,10 @@ var classNames = require('classnames');
7
7
  var isFunction = require('lodash/isFunction');
8
8
  var isObject = require('lodash/isObject');
9
9
  var React = require('react');
10
- var debounce = require('lodash/debounce');
11
10
  var isArray = require('lodash/isArray');
12
11
  var sortBy = require('lodash/sortBy');
13
12
  var uniqBy = require('lodash/uniqBy');
13
+ var debounce = require('lodash/debounce');
14
14
  var EventEmitter = require('wolfy87-eventemitter');
15
15
  var createDebug = require('debug');
16
16
  var jsxRuntime = require('react/jsx-runtime');
@@ -24,10 +24,10 @@ var classNames__default = /*#__PURE__*/_interopDefaultLegacy(classNames);
24
24
  var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
25
25
  var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
26
26
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
27
- var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
28
27
  var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
29
28
  var sortBy__default = /*#__PURE__*/_interopDefaultLegacy(sortBy);
30
29
  var uniqBy__default = /*#__PURE__*/_interopDefaultLegacy(uniqBy);
30
+ var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
31
31
  var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
32
32
  var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
33
33
 
@@ -864,16 +864,20 @@ function useAd(path, size) {
864
864
  // Listen to render event
865
865
  React.useEffect(() => {
866
866
  if (slot === null) {
867
+ if (renderEvent !== null) {
868
+ setRenderEvent(null);
869
+ }
867
870
  return () => {};
868
871
  }
869
872
  const onSlotRender = event => {
870
873
  const newRenderEvent = {
871
874
  ...event,
872
- ...(slot !== null ? slot.getRenderedSize() : null)
875
+ ...(slot !== null ? slot.getRenderedSize() : null),
876
+ slot
873
877
  };
874
878
  setRenderEvent(newRenderEvent);
875
879
  if (onRender !== null) {
876
- onRender(newRenderEvent, slot);
880
+ onRender(newRenderEvent);
877
881
  }
878
882
  const {
879
883
  isEmpty = true
@@ -919,6 +923,9 @@ const propTypes = {
919
923
  refreshInterval: PropTypes__default["default"].number,
920
924
  alwaysRender: PropTypes__default["default"].bool,
921
925
  disabled: PropTypes__default["default"].bool,
926
+ shouldKeepSize: PropTypes__default["default"].bool,
927
+ withoutStyle: PropTypes__default["default"].bool,
928
+ withoutMinimumSize: PropTypes__default["default"].bool,
922
929
  className: PropTypes__default["default"].string,
923
930
  emptyClassName: PropTypes__default["default"].string,
924
931
  adClassName: PropTypes__default["default"].string,
@@ -933,6 +940,9 @@ const defaultProps = {
933
940
  refreshInterval: null,
934
941
  alwaysRender: true,
935
942
  disabled: false,
943
+ shouldKeepSize: false,
944
+ withoutStyle: false,
945
+ withoutMinimumSize: false,
936
946
  className: null,
937
947
  emptyClassName: null,
938
948
  adClassName: null,
@@ -949,6 +959,9 @@ function Ad(_ref) {
949
959
  refreshInterval,
950
960
  alwaysRender,
951
961
  disabled,
962
+ shouldKeepSize,
963
+ withoutStyle,
964
+ withoutMinimumSize,
952
965
  className,
953
966
  emptyClassName,
954
967
  adClassName,
@@ -963,6 +976,7 @@ function Ad(_ref) {
963
976
  const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
964
977
  const finalSize = size || (slot !== null ? slot.size || null : null);
965
978
  const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
979
+ const minimumSize = React.useMemo(() => getMinimumAdSize(finalSizeMapping !== null ? finalSizeMapping.reduce((allSizes, sizeMap) => [...allSizes, sizeMap[1]], [finalSize]) : finalSize), [finalSizeMapping, finalSize]);
966
980
 
967
981
  // Targeting
968
982
  const contextTargeting = useAdsTargeting();
@@ -981,6 +995,39 @@ function Ad(_ref) {
981
995
  targeting: otherProps || {}
982
996
  };
983
997
  }, [allTargeting, refreshInterval]);
998
+ const lastRenderedSize = React.useRef(null);
999
+ const wasDisabled = React.useRef(disabled);
1000
+ const onAdRender = React.useCallback(event => {
1001
+ const {
1002
+ isEmpty: newIsEmpty = true
1003
+ } = event || {};
1004
+ const newIsRendered = !newIsEmpty;
1005
+ if (disabled) {
1006
+ wasDisabled.current = true;
1007
+ } else if (!disabled && newIsRendered) {
1008
+ wasDisabled.current = false;
1009
+ }
1010
+ const waitingNextRender = wasDisabled.current && !newIsRendered;
1011
+ const keepSize = shouldKeepSize && (disabled || waitingNextRender) && lastRenderedSize.current !== null;
1012
+ if (onRender !== null) {
1013
+ onRender({
1014
+ ...event,
1015
+ keepSize
1016
+ });
1017
+ }
1018
+ }, [onRender, shouldKeepSize, disabled]);
1019
+ React.useEffect(() => {
1020
+ if (!disabled) {
1021
+ return;
1022
+ }
1023
+ const keepSize = shouldKeepSize && lastRenderedSize.current !== null;
1024
+ if (onRender !== null) {
1025
+ onRender({
1026
+ isEmpty: true,
1027
+ keepSize
1028
+ });
1029
+ }
1030
+ }, [disabled]);
984
1031
 
985
1032
  // Create ad
986
1033
  const {
@@ -989,6 +1036,7 @@ function Ad(_ref) {
989
1036
  width,
990
1037
  height,
991
1038
  isEmpty,
1039
+ isRendered,
992
1040
  refObserver,
993
1041
  slot: slotObject = null
994
1042
  } = useAd(finalPath, finalSize, {
@@ -996,7 +1044,7 @@ function Ad(_ref) {
996
1044
  targeting: finalAdTargeting.targeting,
997
1045
  refreshInterval: finalAdTargeting.refreshInterval,
998
1046
  alwaysRender,
999
- onRender,
1047
+ onRender: onAdRender,
1000
1048
  disabled
1001
1049
  });
1002
1050
  if (slotRef !== null && isFunction__default["default"](slotRef)) {
@@ -1005,28 +1053,57 @@ function Ad(_ref) {
1005
1053
  // eslint-disable-next-line no-param-reassign
1006
1054
  slotRef.current = slotObject;
1007
1055
  }
1008
-
1009
- // Get size
1010
-
1056
+ if (isRendered) {
1057
+ lastRenderedSize.current = {
1058
+ width,
1059
+ height
1060
+ };
1061
+ }
1062
+ if (disabled) {
1063
+ wasDisabled.current = true;
1064
+ } else if (!disabled && isRendered) {
1065
+ wasDisabled.current = false;
1066
+ }
1067
+ const waitingNextRender = wasDisabled.current && !isRendered;
1068
+ const keepSize = shouldKeepSize && (disabled || waitingNextRender) && lastRenderedSize.current !== null;
1011
1069
  if (id === null) {
1012
1070
  return null;
1013
1071
  }
1072
+ let adStyle = null;
1073
+ if (isRendered) {
1074
+ adStyle = {
1075
+ width,
1076
+ height
1077
+ };
1078
+ } else if (shouldKeepSize && (disabled || waitingNextRender)) {
1079
+ adStyle = lastRenderedSize.current;
1080
+ } else if (!withoutMinimumSize) {
1081
+ adStyle = minimumSize;
1082
+ }
1083
+ let containerStyle = null;
1084
+ if (adsDisabled) {
1085
+ containerStyle = {
1086
+ display: 'none',
1087
+ visibility: 'hidden'
1088
+ };
1089
+ } else if (isEmpty && !keepSize) {
1090
+ containerStyle = {
1091
+ height: 0,
1092
+ paddingBottom: 0,
1093
+ overflow: 'hidden',
1094
+ opacity: 0
1095
+ };
1096
+ }
1014
1097
  return /*#__PURE__*/jsxRuntime.jsx("div", {
1015
1098
  id: id !== null ? `${id}-container` : null,
1016
1099
  className: classNames__default["default"]([className, {
1017
1100
  [emptyClassName]: emptyClassName !== null && isEmpty
1018
1101
  }]),
1019
- style: adsDisabled ? {
1020
- display: 'none',
1021
- visibility: 'hidden'
1022
- } : null,
1102
+ style: !withoutStyle ? containerStyle : null,
1023
1103
  ref: refObserver,
1024
1104
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1025
1105
  className: adClassName,
1026
- style: {
1027
- width,
1028
- height
1029
- },
1106
+ style: adStyle,
1030
1107
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1031
1108
  id: id
1032
1109
  })
package/dist/es.js CHANGED
@@ -3,10 +3,10 @@ import classNames from 'classnames';
3
3
  import isFunction from 'lodash/isFunction';
4
4
  import isObject from 'lodash/isObject';
5
5
  import React, { useContext, useState, useRef, useMemo, useEffect, useCallback } from 'react';
6
- import debounce from 'lodash/debounce';
7
6
  import isArray from 'lodash/isArray';
8
7
  import sortBy from 'lodash/sortBy';
9
8
  import uniqBy from 'lodash/uniqBy';
9
+ import debounce from 'lodash/debounce';
10
10
  import EventEmitter from 'wolfy87-eventemitter';
11
11
  import createDebug from 'debug';
12
12
  import { jsx } from 'react/jsx-runtime';
@@ -846,16 +846,20 @@ function useAd(path, size) {
846
846
  // Listen to render event
847
847
  useEffect(() => {
848
848
  if (slot === null) {
849
+ if (renderEvent !== null) {
850
+ setRenderEvent(null);
851
+ }
849
852
  return () => {};
850
853
  }
851
854
  const onSlotRender = event => {
852
855
  const newRenderEvent = {
853
856
  ...event,
854
- ...(slot !== null ? slot.getRenderedSize() : null)
857
+ ...(slot !== null ? slot.getRenderedSize() : null),
858
+ slot
855
859
  };
856
860
  setRenderEvent(newRenderEvent);
857
861
  if (onRender !== null) {
858
- onRender(newRenderEvent, slot);
862
+ onRender(newRenderEvent);
859
863
  }
860
864
  const {
861
865
  isEmpty = true
@@ -901,6 +905,9 @@ const propTypes = {
901
905
  refreshInterval: PropTypes.number,
902
906
  alwaysRender: PropTypes.bool,
903
907
  disabled: PropTypes.bool,
908
+ shouldKeepSize: PropTypes.bool,
909
+ withoutStyle: PropTypes.bool,
910
+ withoutMinimumSize: PropTypes.bool,
904
911
  className: PropTypes.string,
905
912
  emptyClassName: PropTypes.string,
906
913
  adClassName: PropTypes.string,
@@ -915,6 +922,9 @@ const defaultProps = {
915
922
  refreshInterval: null,
916
923
  alwaysRender: true,
917
924
  disabled: false,
925
+ shouldKeepSize: false,
926
+ withoutStyle: false,
927
+ withoutMinimumSize: false,
918
928
  className: null,
919
929
  emptyClassName: null,
920
930
  adClassName: null,
@@ -931,6 +941,9 @@ function Ad(_ref) {
931
941
  refreshInterval,
932
942
  alwaysRender,
933
943
  disabled,
944
+ shouldKeepSize,
945
+ withoutStyle,
946
+ withoutMinimumSize,
934
947
  className,
935
948
  emptyClassName,
936
949
  adClassName,
@@ -945,6 +958,7 @@ function Ad(_ref) {
945
958
  const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
946
959
  const finalSize = size || (slot !== null ? slot.size || null : null);
947
960
  const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
961
+ const minimumSize = useMemo(() => getMinimumAdSize(finalSizeMapping !== null ? finalSizeMapping.reduce((allSizes, sizeMap) => [...allSizes, sizeMap[1]], [finalSize]) : finalSize), [finalSizeMapping, finalSize]);
948
962
 
949
963
  // Targeting
950
964
  const contextTargeting = useAdsTargeting();
@@ -963,6 +977,39 @@ function Ad(_ref) {
963
977
  targeting: otherProps || {}
964
978
  };
965
979
  }, [allTargeting, refreshInterval]);
980
+ const lastRenderedSize = useRef(null);
981
+ const wasDisabled = useRef(disabled);
982
+ const onAdRender = useCallback(event => {
983
+ const {
984
+ isEmpty: newIsEmpty = true
985
+ } = event || {};
986
+ const newIsRendered = !newIsEmpty;
987
+ if (disabled) {
988
+ wasDisabled.current = true;
989
+ } else if (!disabled && newIsRendered) {
990
+ wasDisabled.current = false;
991
+ }
992
+ const waitingNextRender = wasDisabled.current && !newIsRendered;
993
+ const keepSize = shouldKeepSize && (disabled || waitingNextRender) && lastRenderedSize.current !== null;
994
+ if (onRender !== null) {
995
+ onRender({
996
+ ...event,
997
+ keepSize
998
+ });
999
+ }
1000
+ }, [onRender, shouldKeepSize, disabled]);
1001
+ useEffect(() => {
1002
+ if (!disabled) {
1003
+ return;
1004
+ }
1005
+ const keepSize = shouldKeepSize && lastRenderedSize.current !== null;
1006
+ if (onRender !== null) {
1007
+ onRender({
1008
+ isEmpty: true,
1009
+ keepSize
1010
+ });
1011
+ }
1012
+ }, [disabled]);
966
1013
 
967
1014
  // Create ad
968
1015
  const {
@@ -971,6 +1018,7 @@ function Ad(_ref) {
971
1018
  width,
972
1019
  height,
973
1020
  isEmpty,
1021
+ isRendered,
974
1022
  refObserver,
975
1023
  slot: slotObject = null
976
1024
  } = useAd(finalPath, finalSize, {
@@ -978,7 +1026,7 @@ function Ad(_ref) {
978
1026
  targeting: finalAdTargeting.targeting,
979
1027
  refreshInterval: finalAdTargeting.refreshInterval,
980
1028
  alwaysRender,
981
- onRender,
1029
+ onRender: onAdRender,
982
1030
  disabled
983
1031
  });
984
1032
  if (slotRef !== null && isFunction(slotRef)) {
@@ -987,28 +1035,57 @@ function Ad(_ref) {
987
1035
  // eslint-disable-next-line no-param-reassign
988
1036
  slotRef.current = slotObject;
989
1037
  }
990
-
991
- // Get size
992
-
1038
+ if (isRendered) {
1039
+ lastRenderedSize.current = {
1040
+ width,
1041
+ height
1042
+ };
1043
+ }
1044
+ if (disabled) {
1045
+ wasDisabled.current = true;
1046
+ } else if (!disabled && isRendered) {
1047
+ wasDisabled.current = false;
1048
+ }
1049
+ const waitingNextRender = wasDisabled.current && !isRendered;
1050
+ const keepSize = shouldKeepSize && (disabled || waitingNextRender) && lastRenderedSize.current !== null;
993
1051
  if (id === null) {
994
1052
  return null;
995
1053
  }
1054
+ let adStyle = null;
1055
+ if (isRendered) {
1056
+ adStyle = {
1057
+ width,
1058
+ height
1059
+ };
1060
+ } else if (shouldKeepSize && (disabled || waitingNextRender)) {
1061
+ adStyle = lastRenderedSize.current;
1062
+ } else if (!withoutMinimumSize) {
1063
+ adStyle = minimumSize;
1064
+ }
1065
+ let containerStyle = null;
1066
+ if (adsDisabled) {
1067
+ containerStyle = {
1068
+ display: 'none',
1069
+ visibility: 'hidden'
1070
+ };
1071
+ } else if (isEmpty && !keepSize) {
1072
+ containerStyle = {
1073
+ height: 0,
1074
+ paddingBottom: 0,
1075
+ overflow: 'hidden',
1076
+ opacity: 0
1077
+ };
1078
+ }
996
1079
  return /*#__PURE__*/jsx("div", {
997
1080
  id: id !== null ? `${id}-container` : null,
998
1081
  className: classNames([className, {
999
1082
  [emptyClassName]: emptyClassName !== null && isEmpty
1000
1083
  }]),
1001
- style: adsDisabled ? {
1002
- display: 'none',
1003
- visibility: 'hidden'
1004
- } : null,
1084
+ style: !withoutStyle ? containerStyle : null,
1005
1085
  ref: refObserver,
1006
1086
  children: /*#__PURE__*/jsx("div", {
1007
1087
  className: adClassName,
1008
- style: {
1009
- width,
1010
- height
1011
- },
1088
+ style: adStyle,
1012
1089
  children: /*#__PURE__*/jsx("div", {
1013
1090
  id: id
1014
1091
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/ads",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Ads library",
5
5
  "keywords": [
6
6
  "javascript",
@@ -50,7 +50,7 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "9888055874220611cb49266e2d6943747a783946",
53
+ "gitHead": "02f9a585bd24267dc0032cab26ce37b0a0a7d316",
54
54
  "dependencies": {
55
55
  "@folklore/hooks": "^0.0.41",
56
56
  "@folklore/tracking": "^0.0.16",