@ringcentral/juno 2.5.0 → 2.8.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.
- package/components/Buttons/IconButton/IconButton.d.ts +5 -1
- package/components/Buttons/IconButton/IconButton.js +2 -2
- package/components/Buttons/IconButton/styles/StyledIconButton.js +11 -14
- package/components/Dialer/DialPad/DialPad.d.ts +26 -8
- package/components/Dialer/DialPad/DialPad.js +3 -3
- package/components/Dialer/DialPad/styles/StyledDialPad.d.ts +3 -1
- package/components/Dialer/DialPad/styles/StyledDialPad.js +9 -2
- package/components/Dialer/DialPadButton/DialPadButton.d.ts +3 -3
- package/components/Dialer/DialPadButton/DialPadButton.js +4 -4
- package/components/Dialer/DialPadButton/styles/StyledDialPadButton.js +1 -2
- package/components/Downshift/styles/DownshiftStyle.d.ts +1 -1
- package/components/Forms/Picker/DatePicker/styles/StyledDatePickerHeader.d.ts +1 -1
- package/components/Forms/Picker/DatePicker/styles/StyledYear.d.ts +1 -1
- package/components/Forms/Picker/TimePicker/styles/StyledSelectionItem.d.ts +1 -1
- package/components/Forms/Picker/TimePicker/styles/StyledTimeIconButton.d.ts +1 -1
- package/components/Forms/Picker/styles/PickerBaseIconButton.d.ts +1 -1
- package/components/Forms/TextField/styles/ClearIconButton.d.ts +2 -2
- package/components/InlineEditable/InlineEditable.d.ts +1 -1
- package/es6/components/Buttons/IconButton/IconButton.js +2 -2
- package/es6/components/Buttons/IconButton/styles/StyledIconButton.js +11 -14
- package/es6/components/Dialer/DialPad/DialPad.js +5 -5
- package/es6/components/Dialer/DialPad/styles/StyledDialPad.js +9 -2
- package/es6/components/Dialer/DialPadButton/DialPadButton.js +4 -4
- package/es6/components/Dialer/DialPadButton/styles/StyledDialPadButton.js +1 -2
- package/es6/foundation/hooks/useEventListener/useEventListener.js +27 -8
- package/es6/foundation/hooks/useGlobalListener/createGlobalListener.js +81 -0
- package/es6/foundation/hooks/useGlobalListener/index.js +1 -0
- package/es6/foundation/hooks/useGlobalListener/useGlobalListener.js +67 -97
- package/es6/foundation/hooks/useLongPress/useLongPress.js +50 -31
- package/es6/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.js +27 -28
- package/foundation/hooks/useEventListener/useEventListener.d.ts +12 -16
- package/foundation/hooks/useEventListener/useEventListener.js +26 -7
- package/foundation/hooks/useGlobalListener/createGlobalListener.d.ts +55 -0
- package/foundation/hooks/useGlobalListener/createGlobalListener.js +83 -0
- package/foundation/hooks/useGlobalListener/index.d.ts +1 -0
- package/foundation/hooks/useGlobalListener/index.js +1 -0
- package/foundation/hooks/useGlobalListener/useGlobalListener.d.ts +11 -105
- package/foundation/hooks/useGlobalListener/useGlobalListener.js +67 -97
- package/foundation/hooks/useLongPress/useLongPress.d.ts +8 -8
- package/foundation/hooks/useLongPress/useLongPress.js +48 -29
- package/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.d.ts +27 -14
- package/foundation/hooks/useTouchMouseEvent/useTouchMouseEvent.js +27 -28
- package/foundation/typings/deepPartial.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
import { __assign, __rest } from "tslib";
|
|
2
|
-
import {
|
|
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 {
|
|
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 {
|
|
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,
|
|
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
|
|
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
|
-
|
|
33
|
-
if (!isEmittedRef.current &&
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|
-
}, {
|
|
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
|
-
|
|
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
|
|
28
|
-
var
|
|
29
|
-
var
|
|
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
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
-
|
|
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
|
|
24
|
-
for (var _i =
|
|
25
|
-
|
|
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 =
|
|
28
|
-
var
|
|
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
|
-
|
|
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,108 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare
|
|
1
|
+
import { SyntheticEvent } from 'react';
|
|
2
|
+
import { createGlobalListener, CreateGlobalListenerConfig } from './createGlobalListener';
|
|
3
|
+
declare type GlobalListener = ReturnType<typeof createGlobalListener>;
|
|
4
|
+
export declare type UseGlobalListenerConfig = CreateGlobalListenerConfig & {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* start listening when component mounted
|
|
7
|
+
*
|
|
8
|
+
* @default true
|
|
7
9
|
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* all listener
|
|
11
|
-
*/
|
|
12
|
-
listeners: Set<(e: any) => void>;
|
|
13
|
-
}>;
|
|
14
|
-
/**
|
|
15
|
-
* create globalListener handler for share one listener event
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const globalListener = createGlobalListener('focus', () => { console.log('focus') });
|
|
20
|
-
* const globalListener2 = createGlobalListener('focus', () => { console.log('focus') });
|
|
21
|
-
* globalListener.listen();
|
|
22
|
-
* globalListener2.listen();
|
|
23
|
-
*
|
|
24
|
-
* => // that will only create an event listener on window, but every callback will get emit value when event triggered
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare const createGlobalListener: (key: string, listener: EventListener, { customKey, target: targetProp, }?: {
|
|
28
|
-
/**
|
|
29
|
-
* custom key for determining different event group, default is same as `key`
|
|
30
|
-
*/
|
|
31
|
-
customKey?: string | undefined;
|
|
32
|
-
/**
|
|
33
|
-
* custom binding event target, default is `window`
|
|
34
|
-
*/
|
|
35
|
-
target?: EventTarget | HTMLElement | import("react").RefObject<HTMLElement> | (() => HTMLElement) | undefined;
|
|
36
|
-
}) => {
|
|
37
|
-
/**
|
|
38
|
-
* bind listener again
|
|
39
|
-
*/
|
|
40
|
-
listen: () => void;
|
|
41
|
-
/**
|
|
42
|
-
* remove listener
|
|
43
|
-
*/
|
|
44
|
-
remove: () => void;
|
|
45
|
-
/**
|
|
46
|
-
* current listener state
|
|
47
|
-
*/
|
|
48
|
-
readonly state: {
|
|
49
|
-
/**
|
|
50
|
-
* current listener state count
|
|
51
|
-
*/
|
|
52
|
-
readonly count: number;
|
|
53
|
-
/**
|
|
54
|
-
* is that be listening
|
|
55
|
-
*/
|
|
56
|
-
listening: boolean;
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* bind global event, when you bind same key event,
|
|
61
|
-
* that will reuse one event listener and trigger both callback once that listener be triggered.
|
|
62
|
-
*
|
|
63
|
-
* also you can control listener with method `listen` and `remove`
|
|
64
|
-
* and get listener `state` for check listener count number and current listing state.
|
|
65
|
-
*
|
|
66
|
-
* - `key` `options` only work when set first time, never change after first render
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* useGlobalListener('keyup', () => console.log('window key up1'))
|
|
71
|
-
* useGlobalListener('keyup', () => console.log('window key up2'))
|
|
72
|
-
* useGlobalListener('keyup', () => console.log('window key up3'))
|
|
73
|
-
*
|
|
74
|
-
* => // that will only create an event listener on `window`, but every callback will get emit value when event triggered
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
export declare function useGlobalListener(key: string, listener: EventListener, options?: {
|
|
78
|
-
/**
|
|
79
|
-
* custom key for determining different event group, default is same as `key`
|
|
80
|
-
*/
|
|
81
|
-
customKey?: string;
|
|
82
|
-
/**
|
|
83
|
-
* custom binding event target, default is `window`
|
|
84
|
-
*/
|
|
85
|
-
target?: RefOrElementOrCallback | EventTarget;
|
|
86
|
-
}): {
|
|
87
|
-
/**
|
|
88
|
-
* bind listener again
|
|
89
|
-
*/
|
|
90
|
-
listen: () => void;
|
|
91
|
-
/**
|
|
92
|
-
* remove listener
|
|
93
|
-
*/
|
|
94
|
-
remove: () => void;
|
|
95
|
-
/**
|
|
96
|
-
* current listener state
|
|
97
|
-
*/
|
|
98
|
-
readonly state: {
|
|
99
|
-
/**
|
|
100
|
-
* current listener state count
|
|
101
|
-
*/
|
|
102
|
-
readonly count: number;
|
|
103
|
-
/**
|
|
104
|
-
* is that be listening
|
|
105
|
-
*/
|
|
106
|
-
listening: boolean;
|
|
107
|
-
};
|
|
10
|
+
startImmediately?: boolean;
|
|
108
11
|
};
|
|
12
|
+
export declare function useGlobalListener<T extends SyntheticEvent>(key: string, listener: (event?: T) => void, config?: UseGlobalListenerConfig): GlobalListener;
|
|
13
|
+
export declare function useGlobalListener<T extends SyntheticEvent>(key: string, listener: (event?: T) => void, options?: AddEventListenerOptions | boolean, config?: UseGlobalListenerConfig): GlobalListener;
|
|
14
|
+
export {};
|