@folklore/ads 0.0.8 → 0.0.10

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 +53 -54
  2. package/dist/es.js +50 -52
  3. package/package.json +2 -2
package/dist/cjs.js CHANGED
@@ -4,13 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var PropTypes = require('prop-types');
6
6
  var classNames = require('classnames');
7
+ var isFunction = require('lodash/isFunction');
8
+ var isObject = require('lodash/isObject');
7
9
  var React = require('react');
10
+ var debounce = require('lodash/debounce');
8
11
  var isArray = require('lodash/isArray');
9
12
  var uniqBy = require('lodash/uniqBy');
10
13
  var sortBy = require('lodash/sortBy');
11
- var debounce = require('lodash/debounce');
12
14
  var EventEmitter = require('wolfy87-eventemitter');
13
- var isObject = require('lodash/isObject');
14
15
  var createDebug = require('debug');
15
16
  var jsxRuntime = require('react/jsx-runtime');
16
17
  var hooks = require('@folklore/hooks');
@@ -20,13 +21,14 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
20
21
 
21
22
  var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
22
23
  var classNames__default = /*#__PURE__*/_interopDefaultLegacy(classNames);
24
+ var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
25
+ var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
23
26
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
27
+ var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
24
28
  var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
25
29
  var uniqBy__default = /*#__PURE__*/_interopDefaultLegacy(uniqBy);
26
30
  var sortBy__default = /*#__PURE__*/_interopDefaultLegacy(sortBy);
27
- var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
28
31
  var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
29
- var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
30
32
  var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
31
33
 
32
34
  const adPath = PropTypes__default["default"].string;
@@ -704,7 +706,13 @@ function AdsProvider(_ref) {
704
706
  ready,
705
707
  ads,
706
708
  viewports,
707
- slots,
709
+ slots: Object.keys(slots || {}).reduce((map, key) => ({
710
+ ...map,
711
+ [key]: {
712
+ sizeMapping: getSizeMappingFromSlot(slots[key], viewports),
713
+ ...slots[key]
714
+ }
715
+ }), {}),
708
716
  slotsPath: defaultSlotPath !== null ? {
709
717
  default: defaultSlotPath,
710
718
  ...slotsPath
@@ -859,17 +867,21 @@ function useAd(path, size) {
859
867
  return () => {};
860
868
  }
861
869
  const onSlotRender = event => {
862
- setRenderEvent(event);
870
+ const newRenderEvent = {
871
+ ...event,
872
+ ...(slot !== null ? slot.getRenderedSize() : null)
873
+ };
874
+ setRenderEvent(newRenderEvent);
863
875
  if (onRender !== null) {
864
- onRender(event);
876
+ onRender(newRenderEvent, slot);
865
877
  }
866
878
  const {
867
879
  isEmpty = true
868
- } = event || {};
880
+ } = newRenderEvent || {};
869
881
  if (isEmpty) {
870
882
  track('Empty', slot);
871
883
  } else {
872
- track('Render', slot, event);
884
+ track('Render', slot, newRenderEvent);
873
885
  }
874
886
  };
875
887
  slot.on('render', onSlotRender);
@@ -885,6 +897,7 @@ function useAd(path, size) {
885
897
  }, []);
886
898
  return {
887
899
  refObserver,
900
+ slot,
888
901
  disabled: adsManager.isDisabled(),
889
902
  id: slot !== null ? slot.getElementId() : null,
890
903
  isRendered: slot !== null && slot.isRendered(),
@@ -909,7 +922,8 @@ const propTypes = {
909
922
  className: PropTypes__default["default"].string,
910
923
  emptyClassName: PropTypes__default["default"].string,
911
924
  adClassName: PropTypes__default["default"].string,
912
- onRender: PropTypes__default["default"].func
925
+ onRender: PropTypes__default["default"].func,
926
+ slotRef: PropTypes__default["default"].oneOfType([PropTypes__default["default"].func, PropTypes__default["default"].object])
913
927
  };
914
928
  const defaultProps = {
915
929
  path: null,
@@ -922,7 +936,8 @@ const defaultProps = {
922
936
  className: null,
923
937
  emptyClassName: null,
924
938
  adClassName: null,
925
- onRender: null
939
+ onRender: null,
940
+ slotRef: null
926
941
  };
927
942
  function Ad(_ref) {
928
943
  let {
@@ -937,20 +952,20 @@ function Ad(_ref) {
937
952
  className,
938
953
  emptyClassName,
939
954
  adClassName,
940
- onRender
955
+ onRender,
956
+ slotRef
941
957
  } = _ref;
942
958
  const {
943
- viewports,
944
- slots,
945
- slotsPath
959
+ slots = null,
960
+ slotsPath = {}
946
961
  } = useAdsContext();
947
- const slot = slotName !== null ? slots[slotName] || null : null;
948
- const finalPath = path || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default;
949
- const finalSize = size !== null ? size : slot.size;
962
+ const slot = slotName !== null && slots !== null ? slots[slotName] || null : null;
963
+ const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
964
+ const finalSize = size || (slot !== null ? slot.size || null : null);
965
+ const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
950
966
 
951
967
  // Targeting
952
968
  const contextTargeting = useAdsTargeting();
953
- const finalSizeMapping = React.useMemo(() => sizeMapping !== null ? sizeMapping : getSizeMappingFromSlot(slot, viewports), [sizeMapping, slot, viewports]);
954
969
  const allTargeting = React.useMemo(() => contextTargeting !== null || targeting !== null || slotName !== null ? {
955
970
  slot: slotName,
956
971
  ...contextTargeting,
@@ -974,8 +989,8 @@ function Ad(_ref) {
974
989
  width,
975
990
  height,
976
991
  isEmpty,
977
- isRendered,
978
- refObserver
992
+ refObserver,
993
+ slot: slotObject = null
979
994
  } = useAd(finalPath, finalSize, {
980
995
  sizeMapping: finalSizeMapping,
981
996
  targeting: finalAdTargeting.targeting,
@@ -984,50 +999,34 @@ function Ad(_ref) {
984
999
  onRender,
985
1000
  disabled
986
1001
  });
1002
+ if (slotRef !== null && isFunction__default["default"](slotRef)) {
1003
+ slotRef(slotObject);
1004
+ } else if (slotRef !== null && isObject__default["default"](slotRef)) {
1005
+ // eslint-disable-next-line no-param-reassign
1006
+ slotRef.current = slotObject;
1007
+ }
987
1008
 
988
1009
  // Get size
989
- const lastRenderedSize = React.useRef(null);
990
- const wasDisabled = React.useRef(disabled);
991
- const waitingNextRender = React.useMemo(() => {
992
- if (disabled) {
993
- wasDisabled.current = true;
994
- } else if (!disabled && isRendered) {
995
- wasDisabled.current = false;
996
- }
997
- return wasDisabled.current && !isRendered;
998
- }, [isRendered]);
999
- const sizeStyle = React.useMemo(() => {
1000
- if (disabled || waitingNextRender) {
1001
- return lastRenderedSize.current;
1002
- }
1003
- const {
1004
- width: minimumWidth,
1005
- height: minimumHeight
1006
- } = getMinimumAdSize(finalSizeMapping !== null ? finalSizeMapping.reduce((allSizes, sizeMap) => [...allSizes, sizeMap[1]], [finalSize]) : finalSize);
1007
- if (isRendered) {
1008
- lastRenderedSize.current = !isEmpty ? {
1009
- width,
1010
- height
1011
- } : null;
1012
- }
1013
- return {
1014
- width: isRendered ? width : minimumWidth,
1015
- height: isRendered ? height : minimumHeight
1016
- };
1017
- }, [id, disabled, finalSize, finalSizeMapping, width, height, isRendered, isEmpty]);
1018
- const keepSize = (disabled || waitingNextRender) && lastRenderedSize.current !== null;
1019
- if (id === null && !keepSize) {
1010
+
1011
+ if (id === null) {
1020
1012
  return null;
1021
1013
  }
1022
1014
  return /*#__PURE__*/jsxRuntime.jsx("div", {
1023
1015
  id: id !== null ? `${id}-container` : null,
1024
1016
  className: classNames__default["default"]([className, {
1025
- [emptyClassName]: emptyClassName !== null && isEmpty && !keepSize
1017
+ [emptyClassName]: emptyClassName !== null && isEmpty
1026
1018
  }]),
1019
+ style: adsDisabled ? {
1020
+ display: 'none',
1021
+ visibility: 'hidden'
1022
+ } : null,
1027
1023
  ref: refObserver,
1028
1024
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1029
1025
  className: adClassName,
1030
- style: sizeStyle,
1026
+ style: {
1027
+ width,
1028
+ height
1029
+ },
1031
1030
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1032
1031
  id: id
1033
1032
  })
package/dist/es.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import PropTypes from 'prop-types';
2
2
  import classNames from 'classnames';
3
+ import isFunction from 'lodash/isFunction';
4
+ import isObject from 'lodash/isObject';
3
5
  import React, { useContext, useState, useRef, useMemo, useEffect, useCallback } from 'react';
6
+ import debounce from 'lodash/debounce';
4
7
  import isArray from 'lodash/isArray';
5
8
  import uniqBy from 'lodash/uniqBy';
6
9
  import sortBy from 'lodash/sortBy';
7
- import debounce from 'lodash/debounce';
8
10
  import EventEmitter from 'wolfy87-eventemitter';
9
- import isObject from 'lodash/isObject';
10
11
  import createDebug from 'debug';
11
12
  import { jsx } from 'react/jsx-runtime';
12
13
  import { useIntersectionObserver, useWindowEvent } from '@folklore/hooks';
@@ -687,7 +688,13 @@ function AdsProvider(_ref) {
687
688
  ready,
688
689
  ads,
689
690
  viewports,
690
- slots,
691
+ slots: Object.keys(slots || {}).reduce((map, key) => ({
692
+ ...map,
693
+ [key]: {
694
+ sizeMapping: getSizeMappingFromSlot(slots[key], viewports),
695
+ ...slots[key]
696
+ }
697
+ }), {}),
691
698
  slotsPath: defaultSlotPath !== null ? {
692
699
  default: defaultSlotPath,
693
700
  ...slotsPath
@@ -842,17 +849,21 @@ function useAd(path, size) {
842
849
  return () => {};
843
850
  }
844
851
  const onSlotRender = event => {
845
- setRenderEvent(event);
852
+ const newRenderEvent = {
853
+ ...event,
854
+ ...(slot !== null ? slot.getRenderedSize() : null)
855
+ };
856
+ setRenderEvent(newRenderEvent);
846
857
  if (onRender !== null) {
847
- onRender(event);
858
+ onRender(newRenderEvent, slot);
848
859
  }
849
860
  const {
850
861
  isEmpty = true
851
- } = event || {};
862
+ } = newRenderEvent || {};
852
863
  if (isEmpty) {
853
864
  track('Empty', slot);
854
865
  } else {
855
- track('Render', slot, event);
866
+ track('Render', slot, newRenderEvent);
856
867
  }
857
868
  };
858
869
  slot.on('render', onSlotRender);
@@ -868,6 +879,7 @@ function useAd(path, size) {
868
879
  }, []);
869
880
  return {
870
881
  refObserver,
882
+ slot,
871
883
  disabled: adsManager.isDisabled(),
872
884
  id: slot !== null ? slot.getElementId() : null,
873
885
  isRendered: slot !== null && slot.isRendered(),
@@ -892,7 +904,8 @@ const propTypes = {
892
904
  className: PropTypes.string,
893
905
  emptyClassName: PropTypes.string,
894
906
  adClassName: PropTypes.string,
895
- onRender: PropTypes.func
907
+ onRender: PropTypes.func,
908
+ slotRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
896
909
  };
897
910
  const defaultProps = {
898
911
  path: null,
@@ -905,7 +918,8 @@ const defaultProps = {
905
918
  className: null,
906
919
  emptyClassName: null,
907
920
  adClassName: null,
908
- onRender: null
921
+ onRender: null,
922
+ slotRef: null
909
923
  };
910
924
  function Ad(_ref) {
911
925
  let {
@@ -920,20 +934,20 @@ function Ad(_ref) {
920
934
  className,
921
935
  emptyClassName,
922
936
  adClassName,
923
- onRender
937
+ onRender,
938
+ slotRef
924
939
  } = _ref;
925
940
  const {
926
- viewports,
927
- slots,
928
- slotsPath
941
+ slots = null,
942
+ slotsPath = {}
929
943
  } = useAdsContext();
930
- const slot = slotName !== null ? slots[slotName] || null : null;
931
- const finalPath = path || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default;
932
- const finalSize = size !== null ? size : slot.size;
944
+ const slot = slotName !== null && slots !== null ? slots[slotName] || null : null;
945
+ const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
946
+ const finalSize = size || (slot !== null ? slot.size || null : null);
947
+ const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
933
948
 
934
949
  // Targeting
935
950
  const contextTargeting = useAdsTargeting();
936
- const finalSizeMapping = useMemo(() => sizeMapping !== null ? sizeMapping : getSizeMappingFromSlot(slot, viewports), [sizeMapping, slot, viewports]);
937
951
  const allTargeting = useMemo(() => contextTargeting !== null || targeting !== null || slotName !== null ? {
938
952
  slot: slotName,
939
953
  ...contextTargeting,
@@ -957,8 +971,8 @@ function Ad(_ref) {
957
971
  width,
958
972
  height,
959
973
  isEmpty,
960
- isRendered,
961
- refObserver
974
+ refObserver,
975
+ slot: slotObject = null
962
976
  } = useAd(finalPath, finalSize, {
963
977
  sizeMapping: finalSizeMapping,
964
978
  targeting: finalAdTargeting.targeting,
@@ -967,50 +981,34 @@ function Ad(_ref) {
967
981
  onRender,
968
982
  disabled
969
983
  });
984
+ if (slotRef !== null && isFunction(slotRef)) {
985
+ slotRef(slotObject);
986
+ } else if (slotRef !== null && isObject(slotRef)) {
987
+ // eslint-disable-next-line no-param-reassign
988
+ slotRef.current = slotObject;
989
+ }
970
990
 
971
991
  // Get size
972
- const lastRenderedSize = useRef(null);
973
- const wasDisabled = useRef(disabled);
974
- const waitingNextRender = useMemo(() => {
975
- if (disabled) {
976
- wasDisabled.current = true;
977
- } else if (!disabled && isRendered) {
978
- wasDisabled.current = false;
979
- }
980
- return wasDisabled.current && !isRendered;
981
- }, [isRendered]);
982
- const sizeStyle = useMemo(() => {
983
- if (disabled || waitingNextRender) {
984
- return lastRenderedSize.current;
985
- }
986
- const {
987
- width: minimumWidth,
988
- height: minimumHeight
989
- } = getMinimumAdSize(finalSizeMapping !== null ? finalSizeMapping.reduce((allSizes, sizeMap) => [...allSizes, sizeMap[1]], [finalSize]) : finalSize);
990
- if (isRendered) {
991
- lastRenderedSize.current = !isEmpty ? {
992
- width,
993
- height
994
- } : null;
995
- }
996
- return {
997
- width: isRendered ? width : minimumWidth,
998
- height: isRendered ? height : minimumHeight
999
- };
1000
- }, [id, disabled, finalSize, finalSizeMapping, width, height, isRendered, isEmpty]);
1001
- const keepSize = (disabled || waitingNextRender) && lastRenderedSize.current !== null;
1002
- if (id === null && !keepSize) {
992
+
993
+ if (id === null) {
1003
994
  return null;
1004
995
  }
1005
996
  return /*#__PURE__*/jsx("div", {
1006
997
  id: id !== null ? `${id}-container` : null,
1007
998
  className: classNames([className, {
1008
- [emptyClassName]: emptyClassName !== null && isEmpty && !keepSize
999
+ [emptyClassName]: emptyClassName !== null && isEmpty
1009
1000
  }]),
1001
+ style: adsDisabled ? {
1002
+ display: 'none',
1003
+ visibility: 'hidden'
1004
+ } : null,
1010
1005
  ref: refObserver,
1011
1006
  children: /*#__PURE__*/jsx("div", {
1012
1007
  className: adClassName,
1013
- style: sizeStyle,
1008
+ style: {
1009
+ width,
1010
+ height
1011
+ },
1014
1012
  children: /*#__PURE__*/jsx("div", {
1015
1013
  id: id
1016
1014
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/ads",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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": "72ba6039bc64d4b23921164affb9115727b8b651",
53
+ "gitHead": "b1d0b44123aa9abfd77f520bd356435d8616ff2d",
54
54
  "dependencies": {
55
55
  "@folklore/hooks": "^0.0.41",
56
56
  "@folklore/tracking": "^0.0.16",