@ringcentral/juno 2.5.0 → 2.6.0

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 (30) hide show
  1. package/components/Buttons/IconButton/IconButton.d.ts +5 -1
  2. package/components/Buttons/IconButton/IconButton.js +2 -2
  3. package/components/Downshift/styles/DownshiftStyle.d.ts +1 -1
  4. package/components/Forms/Picker/DatePicker/styles/StyledDatePickerHeader.d.ts +1 -1
  5. package/components/Forms/Picker/DatePicker/styles/StyledYear.d.ts +1 -1
  6. package/components/Forms/Picker/TimePicker/styles/StyledSelectionItem.d.ts +1 -1
  7. package/components/Forms/Picker/TimePicker/styles/StyledTimeIconButton.d.ts +1 -1
  8. package/components/Forms/Picker/styles/PickerBaseIconButton.d.ts +1 -1
  9. package/components/Forms/TextField/styles/ClearIconButton.d.ts +2 -2
  10. package/es6/components/Buttons/IconButton/IconButton.js +2 -2
  11. package/es6/foundation/hooks/useEventListener/useEventListener.js +27 -8
  12. package/es6/foundation/hooks/useGlobalListener/createGlobalListener.js +81 -0
  13. package/es6/foundation/hooks/useGlobalListener/index.js +1 -0
  14. package/es6/foundation/hooks/useGlobalListener/useGlobalListener.js +67 -97
  15. package/es6/foundation/hooks/useLongPress/useLongPress.js +50 -31
  16. package/es6/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.js +27 -28
  17. package/foundation/hooks/useEventListener/useEventListener.d.ts +12 -16
  18. package/foundation/hooks/useEventListener/useEventListener.js +26 -7
  19. package/foundation/hooks/useGlobalListener/createGlobalListener.d.ts +55 -0
  20. package/foundation/hooks/useGlobalListener/createGlobalListener.js +83 -0
  21. package/foundation/hooks/useGlobalListener/index.d.ts +1 -0
  22. package/foundation/hooks/useGlobalListener/index.js +1 -0
  23. package/foundation/hooks/useGlobalListener/useGlobalListener.d.ts +11 -105
  24. package/foundation/hooks/useGlobalListener/useGlobalListener.js +67 -97
  25. package/foundation/hooks/useLongPress/useLongPress.d.ts +8 -8
  26. package/foundation/hooks/useLongPress/useLongPress.js +48 -29
  27. package/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.d.ts +27 -14
  28. package/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.js +27 -28
  29. package/foundation/typings/deepPartial.d.ts +1 -1
  30. package/package.json +1 -1
@@ -1,89 +1,8 @@
1
+ import { __assign, __rest } from "tslib";
1
2
  import { useEffect, useRef } from 'react';
2
3
  import { getRefElement, logInDev } from '../../utils';
3
4
  import { useEventCallback } from '../useEventCallback';
4
- export var globalListenerEventMap = new Map();
5
- /**
6
- * create globalListener handler for share one listener event
7
- *
8
- * @example
9
- * ```ts
10
- * const globalListener = createGlobalListener('focus', () => { console.log('focus') });
11
- * const globalListener2 = createGlobalListener('focus', () => { console.log('focus') });
12
- * globalListener.listen();
13
- * globalListener2.listen();
14
- *
15
- * => // that will only create an event listener on window, but every callback will get emit value when event triggered
16
- * ```
17
- */
18
- export var createGlobalListener = function (key, listener, _a) {
19
- var _b = _a === void 0 ? {} : _a, _c = _b.customKey, customKey = _c === void 0 ? key : _c, _d = _b.target, targetProp = _d === void 0 ? window : _d;
20
- var listening = false;
21
- var getMapValue = function () { return globalListenerEventMap.get(customKey); };
22
- var addListener = function () {
23
- if (listening)
24
- return;
25
- var savedEvent = getMapValue();
26
- if (!(savedEvent === null || savedEvent === void 0 ? void 0 : savedEvent.exec)) {
27
- var exec = function (e) {
28
- var event = getMapValue();
29
- event === null || event === void 0 ? void 0 : event.listeners.forEach(function (c) { return c(e); });
30
- };
31
- globalListenerEventMap.set(customKey, {
32
- exec: exec,
33
- listeners: new Set([listener]),
34
- });
35
- var target = getRefElement(targetProp);
36
- target === null || target === void 0 ? void 0 : target.addEventListener(key, exec);
37
- }
38
- else {
39
- savedEvent.listeners.add(listener);
40
- }
41
- listening = true;
42
- };
43
- var removeListener = function () {
44
- if (!listening)
45
- return;
46
- listening = false;
47
- var _savedEvent = getMapValue();
48
- if (!_savedEvent)
49
- return;
50
- var listeners = _savedEvent.listeners;
51
- listeners.delete(listener);
52
- if (listeners.size === 0) {
53
- var target = getRefElement(targetProp);
54
- target === null || target === void 0 ? void 0 : target.removeEventListener(key, _savedEvent.exec);
55
- globalListenerEventMap.delete(customKey);
56
- }
57
- };
58
- return {
59
- /**
60
- * bind listener again
61
- */
62
- listen: function () { return addListener(); },
63
- /**
64
- * remove listener
65
- */
66
- remove: function () { return removeListener(); },
67
- /**
68
- * current listener state
69
- */
70
- get state() {
71
- return {
72
- /**
73
- * current listener state count
74
- */
75
- get count() {
76
- var _a;
77
- return ((_a = getMapValue()) === null || _a === void 0 ? void 0 : _a.listeners.size) || 0;
78
- },
79
- /**
80
- * is that be listening
81
- */
82
- listening: listening,
83
- };
84
- },
85
- };
86
- };
5
+ import { createGlobalListener, } from './createGlobalListener';
87
6
  /**
88
7
  * bind global event, when you bind same key event,
89
8
  * that will reuse one event listener and trigger both callback once that listener be triggered.
@@ -91,40 +10,91 @@ export var createGlobalListener = function (key, listener, _a) {
91
10
  * also you can control listener with method `listen` and `remove`
92
11
  * and get listener `state` for check listener count number and current listing state.
93
12
  *
94
- * - `key` `options` only work when set first time, never change after first render
13
+ * config:
14
+ * - `target`: custom binding event target, default is `window`
15
+ * - `customKey`: custom key for determining different event group, default is same as `key`
16
+ * - `startImmediately`: start listener immediately, default is `true`
17
+ *
18
+ * * `key`, `options` only work when set first time, never change after first render
95
19
  *
96
20
  * @example
97
21
  * ```ts
98
22
  * useGlobalListener('keyup', () => console.log('window key up1'))
23
+ *
99
24
  * useGlobalListener('keyup', () => console.log('window key up2'))
100
- * useGlobalListener('keyup', () => console.log('window key up3'))
25
+ *
26
+ * useGlobalListener('touchend', () => console.log('window key up3'), {
27
+ * target: someWantHostElement,
28
+ * customKey: 'hostTouchend',
29
+ * })
30
+ *
31
+ * useGlobalListener('touchend', () => console.log('window key up3'), { passive: false }, {
32
+ * target: someWantHostElement,
33
+ * customKey: 'hostTouchend',
34
+ * })
101
35
  *
102
36
  * => // that will only create an event listener on `window`, but every callback will get emit value when event triggered
103
37
  * ```
104
38
  */
105
- export function useGlobalListener(key, listener, options) {
106
- if (options === void 0) { options = {}; }
39
+ export function useGlobalListener(key, listener) {
40
+ var args = [];
41
+ for (var _i = 2; _i < arguments.length; _i++) {
42
+ args[_i - 2] = arguments[_i];
43
+ }
44
+ var _a = getListenerOverloadOption(args), options = _a.options, config = _a.config;
45
+ var _b = config || {}, _c = _b.customKey, customKey = _c === void 0 ? key : _c, _d = _b.target, targetProp = _d === void 0 ? window : _d, _e = _b.startImmediately, startImmediately = _e === void 0 ? true : _e;
107
46
  var _listener = useEventCallback(listener);
108
- var globalListener = useRef(createGlobalListener(key, _listener, options)).current;
47
+ var globalListener = useRef(createGlobalListener(key, _listener, options, {
48
+ customKey: customKey,
49
+ target: targetProp,
50
+ })).current;
109
51
  if (process.env.NODE_ENV !== 'production') {
110
- var _a = options.customKey, customKey_1 = _a === void 0 ? key : _a, _b = options.target, targetProp_1 = _b === void 0 ? window : _b;
111
52
  // eslint-disable-next-line react-hooks/rules-of-hooks
112
53
  useEffect(function () {
113
- var target = getRefElement(targetProp_1);
114
- if (target !== window && customKey_1 === key) {
115
- logInDev({
116
- component: 'useGlobalListener',
117
- message: 'When you custom binding event target, you must custom key for determining different event group',
118
- level: 'error',
119
- });
54
+ if (customKey === key) {
55
+ if (Object.keys(options).length > 0) {
56
+ logInDev({
57
+ component: 'useGlobalListener',
58
+ message: 'when have set options, suggest you also bind customKey, otherwise, it may use previous listener without same options',
59
+ level: 'warn',
60
+ });
61
+ }
62
+ var target = getRefElement(targetProp);
63
+ if (target !== window) {
64
+ logInDev({
65
+ component: 'useGlobalListener',
66
+ message: 'When you custom binding event target, you must custom key for determining different event group',
67
+ level: 'error',
68
+ });
69
+ }
120
70
  }
121
71
  // eslint-disable-next-line react-hooks/exhaustive-deps
122
72
  }, []);
123
73
  }
124
74
  useEffect(function () {
125
- globalListener.listen();
75
+ if (startImmediately) {
76
+ globalListener.listen();
77
+ }
126
78
  return globalListener.remove;
127
79
  // eslint-disable-next-line react-hooks/exhaustive-deps
128
80
  }, []);
129
81
  return globalListener;
130
82
  }
83
+ function getListenerOverloadOption(args) {
84
+ var options;
85
+ var config;
86
+ if (typeof args[0] === 'boolean') {
87
+ options = args[0];
88
+ config = args[1];
89
+ }
90
+ else {
91
+ var _a = __assign(__assign({}, (args[0] || {})), (args[1] || {})), startImmediately = _a.startImmediately, customKey = _a.customKey, target = _a.target, restOptions = __rest(_a, ["startImmediately", "customKey", "target"]);
92
+ options = restOptions;
93
+ config = {
94
+ startImmediately: startImmediately,
95
+ customKey: customKey,
96
+ target: target,
97
+ };
98
+ }
99
+ return { options: options, config: config };
100
+ }
@@ -1,24 +1,29 @@
1
1
  import { __assign, __rest } from "tslib";
2
- import { useEffect, useRef } from 'react';
2
+ import { useRef } from 'react';
3
3
  import { useA11yKeyEvent } from '../useA11yKeyEvent';
4
4
  import { useDebounce } from '../useDebounce';
5
5
  import { useEventCallback } from '../useEventCallback';
6
- import { isElmEqualOrContain, useTouchMouseEvent, } from '../useTouchMouseEvent';
6
+ import { useEventListener } from '../useEventListener';
7
+ import { useGlobalListener } from '../useGlobalListener';
8
+ import { useTouchMouseEvent } from '../useTouchMouseEvent';
9
+ export var isTouchEvent = function (ev) {
10
+ return 'touches' in ev;
11
+ };
7
12
  /**
8
13
  * Provide longPress helper, both `click`/`tab`/`keydown` will trigger event
9
14
  *
10
15
  * - Trigger `onTap` when user action leave small than delay time.
11
16
  * - Trigger `onPress` when action time is long than delay time.
12
17
  *
13
- * @default UseLongPressOptions { isPreventDefault = true, delay = 300 }
18
+ * @default UseLongPressOptions { delay = 300 }
14
19
  */
15
20
  export var useLongPress = function (_a, _b, _c) {
16
21
  var onTap = _a.onTap, onPress = _a.onPress;
17
- var _d = _b === void 0 ? {} : _b, onTouchStart = _d.onTouchStart, onTouchEnd = _d.onTouchEnd, onMouseDown = _d.onMouseDown, onKeyDown = _d.onKeyDown, onKeyUp = _d.onKeyUp, onContextMenu = _d.onContextMenu, onMouseUpArg = _d.onMouseUp;
22
+ var _d = _b === void 0 ? {} : _b, onTouchStartArg = _d.onTouchStart, onTouchEndArg = _d.onTouchEnd, onMouseDown = _d.onMouseDown, onKeyDown = _d.onKeyDown, onKeyUp = _d.onKeyUp, onMouseUpArg = _d.onMouseUp;
18
23
  var _e = _c === void 0 ? {} : _c, _f = _e.isPreventDefault, isPreventDefault = _f === void 0 ? true : _f, _g = _e.delay, delay = _g === void 0 ? 300 : _g, _h = _e.externalWindow, externalWindow = _h === void 0 ? window : _h;
19
24
  var isEmittedRef = useRef(false);
20
25
  var isA11yDownRef = useRef(false);
21
- var elmRef = useRef(null);
26
+ var actionRef = useRef(null);
22
27
  var reasonRef = useRef('click');
23
28
  var emitLongPress = useDebounce(function (e) {
24
29
  // * only when have onPress set have emit
@@ -27,10 +32,10 @@ export var useLongPress = function (_a, _b, _c) {
27
32
  isEmittedRef.current = true;
28
33
  }
29
34
  }, delay);
30
- var end = function (e) {
35
+ var end = function (e, isInside) {
31
36
  // ! mouse up only trigger when up on element, so we host that in document
32
- externalWindow.document.removeEventListener('mouseup', onMouseUp);
33
- if (!isEmittedRef.current && isElmEqualOrContain(e.target, elmRef)) {
37
+ globalMouseUpListener.remove();
38
+ if (!isEmittedRef.current && isInside) {
34
39
  onTap === null || onTap === void 0 ? void 0 : onTap(e, reasonRef.current);
35
40
  }
36
41
  reasonRef.current = 'click';
@@ -39,11 +44,8 @@ export var useLongPress = function (_a, _b, _c) {
39
44
  };
40
45
  var start = function (e) {
41
46
  // ! mouse up only trigger when up on element, so we host that in document
42
- externalWindow.document.addEventListener('mouseup', onMouseUp);
47
+ globalMouseUpListener.listen();
43
48
  var isTouch = e.type === 'touchstart';
44
- if (!isTouch)
45
- e.preventDefault();
46
- e.stopPropagation();
47
49
  if (isTouch) {
48
50
  reasonRef.current = 'tap';
49
51
  }
@@ -51,22 +53,49 @@ export var useLongPress = function (_a, _b, _c) {
51
53
  };
52
54
  var _j = useTouchMouseEvent({
53
55
  onTouchStart: function (e) {
54
- onTouchStart === null || onTouchStart === void 0 ? void 0 : onTouchStart(e);
56
+ // prevent long touch leave element focus
57
+ if (isPreventDefault) {
58
+ e.preventDefault();
59
+ }
60
+ onTouchStartArg === null || onTouchStartArg === void 0 ? void 0 : onTouchStartArg(e);
55
61
  start(e);
56
62
  },
57
- onTouchEnd: function (e) {
58
- onTouchEnd === null || onTouchEnd === void 0 ? void 0 : onTouchEnd(e);
59
- end(e);
63
+ onTouchEnd: function (e, isInside) {
64
+ // prevent leave element focus
65
+ if (isPreventDefault) {
66
+ e.preventDefault();
67
+ }
68
+ onTouchEndArg === null || onTouchEndArg === void 0 ? void 0 : onTouchEndArg(e, isInside);
69
+ end(e, isInside);
60
70
  },
61
71
  onMouseDown: function (e) {
72
+ if (isPreventDefault) {
73
+ e.preventDefault();
74
+ }
62
75
  onMouseDown === null || onMouseDown === void 0 ? void 0 : onMouseDown(e);
63
76
  start(e);
64
77
  },
65
- onMouseUp: function (e) {
78
+ onMouseUp: function (e, isInside) {
79
+ // prevent leave element focus
80
+ if (isPreventDefault) {
81
+ e.preventDefault();
82
+ }
66
83
  onMouseUpArg === null || onMouseUpArg === void 0 ? void 0 : onMouseUpArg(e);
67
- end(e);
84
+ end(e, isInside);
68
85
  },
69
- }, { isPreventDefault: isPreventDefault }), onMouseUp = _j.onMouseUp, events = __rest(_j, ["onMouseUp"]);
86
+ }, { actionRef: actionRef }), onMouseUp = _j.onMouseUp, onTouchEnd = _j.onTouchEnd, onTouchStart = _j.onTouchStart, events = __rest(_j, ["onMouseUp", "onTouchEnd", "onTouchStart"]);
87
+ // use event listener for make event `passive` work in safari
88
+ useEventListener(actionRef, 'touchstart', onTouchStart, {
89
+ passive: false,
90
+ });
91
+ // use event listener for make event `passive` work in safari
92
+ useEventListener(actionRef, 'touchend', onTouchEnd, {
93
+ passive: false,
94
+ });
95
+ var globalMouseUpListener = useGlobalListener('mouseup', onMouseUp, {
96
+ target: externalWindow,
97
+ startImmediately: false,
98
+ });
70
99
  var handleDeleteKeyDown = useA11yKeyEvent(function (e) {
71
100
  reasonRef.current = 'keyboard';
72
101
  // * keydown when hold will trigger many time, show when current is a11y down, not trigger again
@@ -82,18 +111,8 @@ export var useLongPress = function (_a, _b, _c) {
82
111
  var handleKeyUp = useEventCallback(function (e) {
83
112
  onKeyUp === null || onKeyUp === void 0 ? void 0 : onKeyUp(e);
84
113
  if (isA11yDownRef.current)
85
- end(e);
114
+ end(e, true);
86
115
  isA11yDownRef.current = false;
87
116
  });
88
- var handleContextMenu = useEventCallback(function (e) {
89
- onContextMenu === null || onContextMenu === void 0 ? void 0 : onContextMenu(e);
90
- e.preventDefault();
91
- });
92
- useEffect(function () {
93
- return function () {
94
- // * clean when in start remove
95
- externalWindow.document.removeEventListener('mouseup', onMouseUp);
96
- };
97
- }, [onMouseUp, externalWindow]);
98
- return __assign({ ref: elmRef, onContextMenu: handleContextMenu, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp }, events);
117
+ return __assign({ ref: actionRef, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp }, events);
99
118
  };
@@ -1,4 +1,5 @@
1
1
  import { useRef } from 'react';
2
+ import { useRcPortalWindowContext } from '../../contexts';
2
3
  import { getRefElement } from '../../utils';
3
4
  import { useEventCallback } from '../useEventCallback';
4
5
  /**
@@ -11,6 +12,7 @@ import { useEventCallback } from '../useEventCallback';
11
12
  * - onMouseDown
12
13
  * - onMouseUp
13
14
  *
15
+ * ### Once that is touch support device, that mouse event will never can be trigger
14
16
  * https://developer.mozilla.org/en-US/docs/Web/API/Touch_events/Supporting_both_TouchEvent_and_MouseEvent
15
17
  *
16
18
  * Event order:
@@ -24,47 +26,44 @@ import { useEventCallback } from '../useEventCallback';
24
26
  */
25
27
  export var useTouchMouseEvent = function (_a, _b) {
26
28
  var onMouseDownArg = _a.onMouseDown, onMouseUpArg = _a.onMouseUp, onTouchStartArg = _a.onTouchStart, onTouchEndArg = _a.onTouchEnd;
27
- var _c = (_b === void 0 ? {} : _b).isPreventDefault, isPreventDefault = _c === void 0 ? true : _c;
28
- var isLiveEndRef = useRef(false);
29
- var isStartRef = useRef(false);
30
- var startAction = function () { return (isStartRef.current = true); };
31
- var endAction = function () { return (isStartRef.current = false); };
29
+ var actionRef = (_b === void 0 ? {} : _b).actionRef;
30
+ var stopMouseEventRef = useRef(false);
31
+ var document = useRcPortalWindowContext().document;
32
32
  var onTouchStart = useEventCallback(function (e) {
33
- startAction();
33
+ stopMouseEventRef.current = true;
34
34
  onTouchStartArg === null || onTouchStartArg === void 0 ? void 0 : onTouchStartArg(e);
35
35
  });
36
+ function checkInside(elm) {
37
+ var hostElement = getRefElement(actionRef);
38
+ if (hostElement) {
39
+ return !!(elm && isElmEqualOrContain(elm, hostElement));
40
+ }
41
+ return false;
42
+ }
36
43
  var onTouchEnd = useEventCallback(function (e) {
37
- endAction();
38
- isLiveEndRef.current = true;
39
- onTouchEndArg === null || onTouchEndArg === void 0 ? void 0 : onTouchEndArg(e);
40
- });
41
- var preventDefaultEvent = function (e) {
42
- if (isPreventDefault) {
43
- e.preventDefault();
44
- e.stopPropagation();
44
+ var isInside;
45
+ if (actionRef && e.touches.length < 2 && e.changedTouches.length < 2) {
46
+ var touch = e.touches[0] || e.changedTouches[0];
47
+ var elm = document.elementFromPoint(touch.pageX, touch.pageY);
48
+ isInside = !!elm && checkInside(elm);
45
49
  }
46
- };
50
+ onTouchEndArg === null || onTouchEndArg === void 0 ? void 0 : onTouchEndArg(e, isInside);
51
+ });
47
52
  var onMouseDown = useEventCallback(function (e) {
48
- startAction();
49
- if (isLiveEndRef.current) {
50
- e.preventDefault();
51
- e.stopPropagation();
53
+ if (stopMouseEventRef.current) {
52
54
  return;
53
55
  }
54
56
  onMouseDownArg === null || onMouseDownArg === void 0 ? void 0 : onMouseDownArg(e);
55
57
  });
56
58
  var onMouseUp = useEventCallback(function (e) {
57
- if (!isStartRef.current) {
58
- preventDefaultEvent(e);
59
- return;
60
- }
61
- endAction();
62
- if (isLiveEndRef.current) {
63
- isLiveEndRef.current = false;
64
- preventDefaultEvent(e);
59
+ var isInside;
60
+ if (stopMouseEventRef.current)
65
61
  return;
62
+ if (actionRef) {
63
+ var elm = e.target;
64
+ isInside = !!elm && checkInside(elm);
66
65
  }
67
- onMouseUpArg === null || onMouseUpArg === void 0 ? void 0 : onMouseUpArg(e);
66
+ onMouseUpArg === null || onMouseUpArg === void 0 ? void 0 : onMouseUpArg(e, isInside);
68
67
  });
69
68
  return {
70
69
  onTouchStart: onTouchStart,
@@ -1,20 +1,16 @@
1
+ import { SyntheticEvent } from 'react';
1
2
  import { RefOrElementOrCallback } from '../../utils';
2
- /**
3
- * bind event when component mount and auto remove event when destroy.
4
- * can remove listener by call returned function or pass empty 'target'.
5
- *
6
- * @param target
7
- * listener will be removed when target is changed to null,
8
- * or rebinding to new element when `ref.current` change
9
- * @returns a method object
10
- * - listen() listen listener again, if that have listener, that will not listen again
11
- * - remove() remove listener
12
- * @example
13
- * ```ts
14
- * useEventListener(window, 'keyup', () => console.log('window key up'))
15
- * ```
16
- */
17
- export declare function useEventListener(target?: RefOrElementOrCallback | EventTarget, ...o: Parameters<EventTarget['addEventListener']>): {
3
+ export declare type UseEventListenerAction = {
18
4
  listen: () => void;
19
5
  remove: () => void;
20
6
  };
7
+ export declare type UseEventListenerConfig = {
8
+ /**
9
+ * start listening when component mounted
10
+ *
11
+ * @default true
12
+ */
13
+ startImmediately?: boolean;
14
+ };
15
+ export declare function useEventListener<T = SyntheticEvent>(target: RefOrElementOrCallback | EventTarget, key: string, callback: (event?: T) => void, config?: UseEventListenerConfig): UseEventListenerAction;
16
+ export declare function useEventListener<T = SyntheticEvent>(target: RefOrElementOrCallback | EventTarget, key: string, callback: (event?: T) => void, options?: AddEventListenerOptions | boolean, config?: UseEventListenerConfig): UseEventListenerAction;
@@ -19,13 +19,14 @@ var useEventCallback_1 = require("../useEventCallback");
19
19
  * useEventListener(window, 'keyup', () => console.log('window key up'))
20
20
  * ```
21
21
  */
22
- function useEventListener(target) {
23
- var o = [];
24
- for (var _i = 1; _i < arguments.length; _i++) {
25
- o[_i - 1] = arguments[_i];
22
+ function useEventListener(target, key, callback) {
23
+ var args = [];
24
+ for (var _i = 3; _i < arguments.length; _i++) {
25
+ args[_i - 3] = arguments[_i];
26
26
  }
27
- var _a = tslib_1.__read(o, 3), key = _a[0], cb = _a[1], options = _a[2];
28
- var listener = useEventCallback_1.useEventCallback(cb);
27
+ var _a = getListenerOverloadOption(args), options = _a.options, config = _a.config;
28
+ var _b = (config || {}).startImmediately, startImmediately = _b === void 0 ? true : _b;
29
+ var listener = useEventCallback_1.useEventCallback(callback);
29
30
  var cancelRef = react_1.useRef(function () { });
30
31
  var bindRef = react_1.useRef(function () { });
31
32
  var currentRefElm = utils_1.getRefElement(target);
@@ -40,7 +41,9 @@ function useEventListener(target) {
40
41
  cancelRef.current = function () {
41
42
  return currentElm.removeEventListener(key, listener, options);
42
43
  };
43
- bindRef.current();
44
+ if (startImmediately) {
45
+ bindRef.current();
46
+ }
44
47
  return cancelRef.current;
45
48
  // eslint-disable-next-line react-hooks/exhaustive-deps
46
49
  }, [target, currentRefElm]);
@@ -50,3 +53,19 @@ function useEventListener(target) {
50
53
  };
51
54
  }
52
55
  exports.useEventListener = useEventListener;
56
+ function getListenerOverloadOption(args) {
57
+ var options;
58
+ var config;
59
+ if (typeof args[0] === 'boolean') {
60
+ options = args[0];
61
+ config = args[1];
62
+ }
63
+ else {
64
+ var _a = tslib_1.__assign(tslib_1.__assign({}, (args[0] || {})), (args[1] || {})), startImmediately = _a.startImmediately, restOptions = tslib_1.__rest(_a, ["startImmediately"]);
65
+ options = restOptions;
66
+ config = {
67
+ startImmediately: startImmediately,
68
+ };
69
+ }
70
+ return { options: options, config: config };
71
+ }
@@ -0,0 +1,55 @@
1
+ import { SyntheticEvent } from 'react';
2
+ import { RefOrElementOrCallback } from '../../utils';
3
+ export declare const globalListenerEventMap: Map<string, {
4
+ /**
5
+ * listener exec method
6
+ */
7
+ exec: (e: any) => void;
8
+ /**
9
+ * all listener
10
+ */
11
+ listeners: Set<(e: any) => void>;
12
+ }>;
13
+ export declare type CreateGlobalListenerConfig = {
14
+ /**
15
+ * custom key for determining different event group, default is same as `key`
16
+ */
17
+ customKey?: string;
18
+ /**
19
+ * custom binding event target, default is `window`
20
+ */
21
+ target?: RefOrElementOrCallback | EventTarget;
22
+ };
23
+ /**
24
+ * create globalListener handler for share one listener event
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const globalListener = createGlobalListener('focus', () => { console.log('focus') });
29
+ * const globalListener2 = createGlobalListener('focus', () => { console.log('focus') });
30
+ * globalListener.listen();
31
+ * globalListener2.listen();
32
+ *
33
+ * => // that will only create an event listener on window, but every callback will get emit value when event triggered
34
+ * ```
35
+ */
36
+ export declare const createGlobalListener: <T extends SyntheticEvent<Element, Event>>(key: string, listener: (event?: T | undefined) => void, options?: boolean | AddEventListenerOptions | undefined, { customKey, target: targetProp, }?: CreateGlobalListenerConfig) => {
37
+ listen: () => void;
38
+ /**
39
+ * remove listener
40
+ */
41
+ remove: () => void;
42
+ /**
43
+ * current listener state
44
+ */
45
+ readonly state: {
46
+ /**
47
+ * current listener state count
48
+ */
49
+ readonly count: number;
50
+ /**
51
+ * is that be listening
52
+ */
53
+ listening: boolean;
54
+ };
55
+ };
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var utils_1 = require("../../utils");
4
+ exports.globalListenerEventMap = new Map();
5
+ /**
6
+ * create globalListener handler for share one listener event
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const globalListener = createGlobalListener('focus', () => { console.log('focus') });
11
+ * const globalListener2 = createGlobalListener('focus', () => { console.log('focus') });
12
+ * globalListener.listen();
13
+ * globalListener2.listen();
14
+ *
15
+ * => // that will only create an event listener on window, but every callback will get emit value when event triggered
16
+ * ```
17
+ */
18
+ exports.createGlobalListener = function (key, listener, options, _a) {
19
+ var _b = _a === void 0 ? {} : _a, _c = _b.customKey, customKey = _c === void 0 ? key : _c, _d = _b.target, targetProp = _d === void 0 ? window : _d;
20
+ var listening = false;
21
+ var getMapValue = function () { return exports.globalListenerEventMap.get(customKey); };
22
+ var addListener = function () {
23
+ if (listening)
24
+ return;
25
+ var savedEvent = getMapValue();
26
+ if (!(savedEvent === null || savedEvent === void 0 ? void 0 : savedEvent.exec)) {
27
+ var exec = function (e) {
28
+ var event = getMapValue();
29
+ event === null || event === void 0 ? void 0 : event.listeners.forEach(function (c) { return c(e); });
30
+ };
31
+ exports.globalListenerEventMap.set(customKey, {
32
+ exec: exec,
33
+ listeners: new Set([listener]),
34
+ });
35
+ var target = utils_1.getRefElement(targetProp);
36
+ target === null || target === void 0 ? void 0 : target.addEventListener(key, exec, options);
37
+ }
38
+ else {
39
+ savedEvent.listeners.add(listener);
40
+ }
41
+ listening = true;
42
+ };
43
+ var removeListener = function () {
44
+ if (!listening)
45
+ return;
46
+ listening = false;
47
+ var _savedEvent = getMapValue();
48
+ if (!_savedEvent)
49
+ return;
50
+ var listeners = _savedEvent.listeners;
51
+ listeners.delete(listener);
52
+ if (listeners.size === 0) {
53
+ var target = utils_1.getRefElement(targetProp);
54
+ target === null || target === void 0 ? void 0 : target.removeEventListener(key, _savedEvent.exec, options);
55
+ exports.globalListenerEventMap.delete(customKey);
56
+ }
57
+ };
58
+ return {
59
+ listen: function () { return addListener(); },
60
+ /**
61
+ * remove listener
62
+ */
63
+ remove: function () { return removeListener(); },
64
+ /**
65
+ * current listener state
66
+ */
67
+ get state() {
68
+ return {
69
+ /**
70
+ * current listener state count
71
+ */
72
+ get count() {
73
+ var _a;
74
+ return ((_a = getMapValue()) === null || _a === void 0 ? void 0 : _a.listeners.size) || 0;
75
+ },
76
+ /**
77
+ * is that be listening
78
+ */
79
+ listening: listening,
80
+ };
81
+ },
82
+ };
83
+ };
@@ -1 +1,2 @@
1
+ export * from './createGlobalListener';
1
2
  export * from './useGlobalListener';
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./createGlobalListener"), exports);
4
5
  tslib_1.__exportStar(require("./useGlobalListener"), exports);