@folklore/ads 0.0.8 → 0.0.9

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 +50 -54
  2. package/dist/es.js +47 -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,10 @@ function AdsProvider(_ref) {
704
706
  ready,
705
707
  ads,
706
708
  viewports,
707
- slots,
709
+ slots: (slots || []).map(slot => ({
710
+ sizeMapping: getSizeMappingFromSlot(slot, viewports),
711
+ ...slot
712
+ })),
708
713
  slotsPath: defaultSlotPath !== null ? {
709
714
  default: defaultSlotPath,
710
715
  ...slotsPath
@@ -859,17 +864,21 @@ function useAd(path, size) {
859
864
  return () => {};
860
865
  }
861
866
  const onSlotRender = event => {
862
- setRenderEvent(event);
867
+ const newRenderEvent = {
868
+ ...event,
869
+ ...(slot !== null ? slot.getRenderedSize() : null)
870
+ };
871
+ setRenderEvent(newRenderEvent);
863
872
  if (onRender !== null) {
864
- onRender(event);
873
+ onRender(newRenderEvent, slot);
865
874
  }
866
875
  const {
867
876
  isEmpty = true
868
- } = event || {};
877
+ } = newRenderEvent || {};
869
878
  if (isEmpty) {
870
879
  track('Empty', slot);
871
880
  } else {
872
- track('Render', slot, event);
881
+ track('Render', slot, newRenderEvent);
873
882
  }
874
883
  };
875
884
  slot.on('render', onSlotRender);
@@ -885,6 +894,7 @@ function useAd(path, size) {
885
894
  }, []);
886
895
  return {
887
896
  refObserver,
897
+ slot,
888
898
  disabled: adsManager.isDisabled(),
889
899
  id: slot !== null ? slot.getElementId() : null,
890
900
  isRendered: slot !== null && slot.isRendered(),
@@ -909,7 +919,8 @@ const propTypes = {
909
919
  className: PropTypes__default["default"].string,
910
920
  emptyClassName: PropTypes__default["default"].string,
911
921
  adClassName: PropTypes__default["default"].string,
912
- onRender: PropTypes__default["default"].func
922
+ onRender: PropTypes__default["default"].func,
923
+ slotRef: PropTypes__default["default"].oneOfType([PropTypes__default["default"].func, PropTypes__default["default"].object])
913
924
  };
914
925
  const defaultProps = {
915
926
  path: null,
@@ -922,7 +933,8 @@ const defaultProps = {
922
933
  className: null,
923
934
  emptyClassName: null,
924
935
  adClassName: null,
925
- onRender: null
936
+ onRender: null,
937
+ slotRef: null
926
938
  };
927
939
  function Ad(_ref) {
928
940
  let {
@@ -937,20 +949,20 @@ function Ad(_ref) {
937
949
  className,
938
950
  emptyClassName,
939
951
  adClassName,
940
- onRender
952
+ onRender,
953
+ slotRef
941
954
  } = _ref;
942
955
  const {
943
- viewports,
944
- slots,
945
- slotsPath
956
+ slots = null,
957
+ slotsPath = {}
946
958
  } = 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;
959
+ const slot = slotName !== null && slots !== null ? slots[slotName] || null : null;
960
+ const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
961
+ const finalSize = size || (slot !== null ? slot.size || null : null);
962
+ const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
950
963
 
951
964
  // Targeting
952
965
  const contextTargeting = useAdsTargeting();
953
- const finalSizeMapping = React.useMemo(() => sizeMapping !== null ? sizeMapping : getSizeMappingFromSlot(slot, viewports), [sizeMapping, slot, viewports]);
954
966
  const allTargeting = React.useMemo(() => contextTargeting !== null || targeting !== null || slotName !== null ? {
955
967
  slot: slotName,
956
968
  ...contextTargeting,
@@ -974,8 +986,8 @@ function Ad(_ref) {
974
986
  width,
975
987
  height,
976
988
  isEmpty,
977
- isRendered,
978
- refObserver
989
+ refObserver,
990
+ slot: slotObject = null
979
991
  } = useAd(finalPath, finalSize, {
980
992
  sizeMapping: finalSizeMapping,
981
993
  targeting: finalAdTargeting.targeting,
@@ -984,50 +996,34 @@ function Ad(_ref) {
984
996
  onRender,
985
997
  disabled
986
998
  });
999
+ if (slotRef !== null && isFunction__default["default"](slotRef)) {
1000
+ slotRef(slotObject);
1001
+ } else if (slotRef !== null && isObject__default["default"](slotRef)) {
1002
+ // eslint-disable-next-line no-param-reassign
1003
+ slotRef.current = slotObject;
1004
+ }
987
1005
 
988
1006
  // 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) {
1007
+
1008
+ if (id === null) {
1020
1009
  return null;
1021
1010
  }
1022
1011
  return /*#__PURE__*/jsxRuntime.jsx("div", {
1023
1012
  id: id !== null ? `${id}-container` : null,
1024
1013
  className: classNames__default["default"]([className, {
1025
- [emptyClassName]: emptyClassName !== null && isEmpty && !keepSize
1014
+ [emptyClassName]: emptyClassName !== null && isEmpty
1026
1015
  }]),
1016
+ style: adsDisabled ? {
1017
+ display: 'none',
1018
+ visibility: 'hidden'
1019
+ } : null,
1027
1020
  ref: refObserver,
1028
1021
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1029
1022
  className: adClassName,
1030
- style: sizeStyle,
1023
+ style: {
1024
+ width,
1025
+ height
1026
+ },
1031
1027
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
1032
1028
  id: id
1033
1029
  })
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,10 @@ function AdsProvider(_ref) {
687
688
  ready,
688
689
  ads,
689
690
  viewports,
690
- slots,
691
+ slots: (slots || []).map(slot => ({
692
+ sizeMapping: getSizeMappingFromSlot(slot, viewports),
693
+ ...slot
694
+ })),
691
695
  slotsPath: defaultSlotPath !== null ? {
692
696
  default: defaultSlotPath,
693
697
  ...slotsPath
@@ -842,17 +846,21 @@ function useAd(path, size) {
842
846
  return () => {};
843
847
  }
844
848
  const onSlotRender = event => {
845
- setRenderEvent(event);
849
+ const newRenderEvent = {
850
+ ...event,
851
+ ...(slot !== null ? slot.getRenderedSize() : null)
852
+ };
853
+ setRenderEvent(newRenderEvent);
846
854
  if (onRender !== null) {
847
- onRender(event);
855
+ onRender(newRenderEvent, slot);
848
856
  }
849
857
  const {
850
858
  isEmpty = true
851
- } = event || {};
859
+ } = newRenderEvent || {};
852
860
  if (isEmpty) {
853
861
  track('Empty', slot);
854
862
  } else {
855
- track('Render', slot, event);
863
+ track('Render', slot, newRenderEvent);
856
864
  }
857
865
  };
858
866
  slot.on('render', onSlotRender);
@@ -868,6 +876,7 @@ function useAd(path, size) {
868
876
  }, []);
869
877
  return {
870
878
  refObserver,
879
+ slot,
871
880
  disabled: adsManager.isDisabled(),
872
881
  id: slot !== null ? slot.getElementId() : null,
873
882
  isRendered: slot !== null && slot.isRendered(),
@@ -892,7 +901,8 @@ const propTypes = {
892
901
  className: PropTypes.string,
893
902
  emptyClassName: PropTypes.string,
894
903
  adClassName: PropTypes.string,
895
- onRender: PropTypes.func
904
+ onRender: PropTypes.func,
905
+ slotRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
896
906
  };
897
907
  const defaultProps = {
898
908
  path: null,
@@ -905,7 +915,8 @@ const defaultProps = {
905
915
  className: null,
906
916
  emptyClassName: null,
907
917
  adClassName: null,
908
- onRender: null
918
+ onRender: null,
919
+ slotRef: null
909
920
  };
910
921
  function Ad(_ref) {
911
922
  let {
@@ -920,20 +931,20 @@ function Ad(_ref) {
920
931
  className,
921
932
  emptyClassName,
922
933
  adClassName,
923
- onRender
934
+ onRender,
935
+ slotRef
924
936
  } = _ref;
925
937
  const {
926
- viewports,
927
- slots,
928
- slotsPath
938
+ slots = null,
939
+ slotsPath = {}
929
940
  } = 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;
941
+ const slot = slotName !== null && slots !== null ? slots[slotName] || null : null;
942
+ const finalPath = path || (slot !== null ? slot.path || null : null) || (slotName !== null ? slotsPath[slotName] : null) || slotsPath.default || null;
943
+ const finalSize = size || (slot !== null ? slot.size || null : null);
944
+ const finalSizeMapping = sizeMapping || (slot !== null ? slot.sizeMapping || null : null);
933
945
 
934
946
  // Targeting
935
947
  const contextTargeting = useAdsTargeting();
936
- const finalSizeMapping = useMemo(() => sizeMapping !== null ? sizeMapping : getSizeMappingFromSlot(slot, viewports), [sizeMapping, slot, viewports]);
937
948
  const allTargeting = useMemo(() => contextTargeting !== null || targeting !== null || slotName !== null ? {
938
949
  slot: slotName,
939
950
  ...contextTargeting,
@@ -957,8 +968,8 @@ function Ad(_ref) {
957
968
  width,
958
969
  height,
959
970
  isEmpty,
960
- isRendered,
961
- refObserver
971
+ refObserver,
972
+ slot: slotObject = null
962
973
  } = useAd(finalPath, finalSize, {
963
974
  sizeMapping: finalSizeMapping,
964
975
  targeting: finalAdTargeting.targeting,
@@ -967,50 +978,34 @@ function Ad(_ref) {
967
978
  onRender,
968
979
  disabled
969
980
  });
981
+ if (slotRef !== null && isFunction(slotRef)) {
982
+ slotRef(slotObject);
983
+ } else if (slotRef !== null && isObject(slotRef)) {
984
+ // eslint-disable-next-line no-param-reassign
985
+ slotRef.current = slotObject;
986
+ }
970
987
 
971
988
  // 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) {
989
+
990
+ if (id === null) {
1003
991
  return null;
1004
992
  }
1005
993
  return /*#__PURE__*/jsx("div", {
1006
994
  id: id !== null ? `${id}-container` : null,
1007
995
  className: classNames([className, {
1008
- [emptyClassName]: emptyClassName !== null && isEmpty && !keepSize
996
+ [emptyClassName]: emptyClassName !== null && isEmpty
1009
997
  }]),
998
+ style: adsDisabled ? {
999
+ display: 'none',
1000
+ visibility: 'hidden'
1001
+ } : null,
1010
1002
  ref: refObserver,
1011
1003
  children: /*#__PURE__*/jsx("div", {
1012
1004
  className: adClassName,
1013
- style: sizeStyle,
1005
+ style: {
1006
+ width,
1007
+ height
1008
+ },
1014
1009
  children: /*#__PURE__*/jsx("div", {
1015
1010
  id: id
1016
1011
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@folklore/ads",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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": "f00b6b5420946e2ab65ba278dd860090d106704b",
54
54
  "dependencies": {
55
55
  "@folklore/hooks": "^0.0.41",
56
56
  "@folklore/tracking": "^0.0.16",