@smart-react-components/ui 2.0.0-alpha-14 → 2.0.0-alpha-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.
- package/FixedBox/index.js +4 -4
- package/hooks/useFixedBoxMethods.d.ts +1 -1
- package/hooks/useFixedBoxMethods.js +33 -5
- package/package.json +1 -1
- package/types/index.d.ts +2 -1
- package/types/index.js +1 -0
- package/util/fixed-box.d.ts +2 -1
- package/util/fixed-box.js +29 -20
package/FixedBox/index.js
CHANGED
|
@@ -22,16 +22,16 @@ const FixedBox = ({ afterHide, afterShow, beforeHide, beforeShow, breakpoint = '
|
|
|
22
22
|
const triggerEl = react_1.default.useRef(null);
|
|
23
23
|
const boxEl = react_1.default.useRef(null);
|
|
24
24
|
const getTriggerEl = () => { var _a; return ((_a = children[0].ref) !== null && _a !== void 0 ? _a : triggerEl).current; };
|
|
25
|
-
const handlePosition =
|
|
25
|
+
const handlePosition = e => (0, fixed_box_1.calculatePosition)(getTriggerEl(), boxEl.current, e, position, maxWidth, minWidth, space);
|
|
26
26
|
const { getStatus, handleBeforeShow, } = (0, useFixedBoxMethods_1.default)({ beforeShow, boxEl, getTriggerEl, handlePosition, isDismissible, key: 'fixed-box', setStatus, status, triggerInteraction });
|
|
27
27
|
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
28
28
|
react_1.default.cloneElement(children[0], { ref: (_a = children[0].ref) !== null && _a !== void 0 ? _a : triggerEl }),
|
|
29
29
|
react_1.default.createElement(CSSTransition_1.default, { afterHide: afterHide, afterShow: afterShow, beforeHide: beforeHide, beforeShow: handleBeforeShow, className: transitionClassName !== null && transitionClassName !== void 0 ? transitionClassName : 'src-fixed-box', duration: transitionDuration !== null && transitionDuration !== void 0 ? transitionDuration : theme.$.transition.fixedBoxDuration, hasHideAnimation: hasHideAnimation, hasShowAnimation: hasShowAnimation, status: getStatus() },
|
|
30
|
-
react_1.default.createElement(Overlay_1.default, { breakpoint: breakpoint, elementProps: {
|
|
30
|
+
react_1.default.createElement(Overlay_1.default, { breakpoint: triggerInteraction & types_1.TriggerInteraction.RIGHT_CLICK ? null : breakpoint, elementProps: {
|
|
31
31
|
as: OverlayElement_1.default,
|
|
32
32
|
duration: transitionDuration !== null && transitionDuration !== void 0 ? transitionDuration : theme.$.transition.fixedBoxDuration,
|
|
33
|
-
isDisplayedWhenBreakpointNull:
|
|
34
|
-
} },
|
|
33
|
+
isDisplayedWhenBreakpointNull: triggerInteraction & types_1.TriggerInteraction.RIGHT_CLICK,
|
|
34
|
+
}, hasBackground: !(triggerInteraction & types_1.TriggerInteraction.RIGHT_CLICK) },
|
|
35
35
|
react_1.default.createElement(FixedBoxElement_1.default, Object.assign({}, elementProps, { breakpoint: breakpoint, ref: boxEl }), children[1])))));
|
|
36
36
|
};
|
|
37
37
|
exports.default = FixedBox;
|
|
@@ -6,7 +6,7 @@ interface Props {
|
|
|
6
6
|
beforeShow?: TransitionBeforeCallback;
|
|
7
7
|
boxEl: React.MutableRefObject<HTMLDivElement>;
|
|
8
8
|
getTriggerEl: () => HTMLElement;
|
|
9
|
-
handlePosition: () => void;
|
|
9
|
+
handlePosition: (e: MouseEvent) => void;
|
|
10
10
|
isDismissible: boolean;
|
|
11
11
|
key: string;
|
|
12
12
|
setStatus?: SetState<boolean>;
|
|
@@ -15,21 +15,34 @@ const types_1 = require("../types");
|
|
|
15
15
|
const dom_2 = require("../util/dom");
|
|
16
16
|
const useFixedBoxMethods = ({ beforeShow, boxEl, getTriggerEl, handlePosition, isDismissible, key, setStatus, status, triggerInteraction }) => {
|
|
17
17
|
const leaveTimeout = (0, react_1.useRef)(null);
|
|
18
|
+
const mouseEvent = (0, react_1.useRef)(null);
|
|
18
19
|
const [localStatus, setLocalStatus] = (0, react_1.useState)(() => false);
|
|
19
20
|
const getStatus = () => status !== null && status !== void 0 ? status : localStatus;
|
|
20
21
|
const getSetStatus = () => setStatus !== null && setStatus !== void 0 ? setStatus : setLocalStatus;
|
|
21
22
|
const handleTriggerClick = (e) => {
|
|
23
|
+
mouseEvent.current = null;
|
|
22
24
|
const triggerEl = getTriggerEl();
|
|
23
25
|
if ((0, dom_2.isTargetClickable)(e.target, triggerEl)) {
|
|
24
26
|
getSetStatus()(!getStatus());
|
|
25
27
|
}
|
|
26
28
|
};
|
|
29
|
+
const handleTriggerRightClick = e => {
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
mouseEvent.current = e;
|
|
32
|
+
const triggerEl = getTriggerEl();
|
|
33
|
+
if ((0, dom_2.isTargetClickable)(e.target, triggerEl)) {
|
|
34
|
+
getSetStatus()(true);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
27
37
|
const handleTriggerMouseEnter = e => {
|
|
38
|
+
mouseEvent.current = null;
|
|
28
39
|
if (e.target === getTriggerEl()) {
|
|
29
40
|
getSetStatus()(true);
|
|
30
41
|
}
|
|
31
42
|
};
|
|
32
43
|
const handleWindowClick = (e) => {
|
|
44
|
+
e.preventDefault();
|
|
45
|
+
mouseEvent.current = null;
|
|
33
46
|
const target = e.target;
|
|
34
47
|
const triggerEl = getTriggerEl();
|
|
35
48
|
if (!triggerEl.contains(target) && !boxEl.current.contains(target)) {
|
|
@@ -55,18 +68,19 @@ const useFixedBoxMethods = ({ beforeShow, boxEl, getTriggerEl, handlePosition, i
|
|
|
55
68
|
};
|
|
56
69
|
const handleBeforeShow = el => new Promise((resolve) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
70
|
yield (beforeShow === null || beforeShow === void 0 ? void 0 : beforeShow(el));
|
|
58
|
-
handlePosition();
|
|
71
|
+
handlePosition(mouseEvent.current);
|
|
59
72
|
resolve();
|
|
60
73
|
}));
|
|
61
74
|
(0, react_1.useEffect)(() => {
|
|
62
75
|
if (getStatus()) {
|
|
63
|
-
handlePosition();
|
|
76
|
+
handlePosition(null);
|
|
64
77
|
}
|
|
65
78
|
}, []);
|
|
66
79
|
(0, react_1.useEffect)(() => {
|
|
67
80
|
const triggerEl = getTriggerEl();
|
|
68
81
|
const debounceWindowMouseMove = (0, dom_1.debounce)(handleWindowMouseMove);
|
|
69
|
-
const debouncePosition = (0, dom_1.debounce)(handlePosition);
|
|
82
|
+
const debouncePosition = (0, dom_1.debounce)(() => handlePosition(null));
|
|
83
|
+
const handlePositionBind = () => handlePosition(null);
|
|
70
84
|
if (triggerInteraction & types_1.TriggerInteraction.CLICK) {
|
|
71
85
|
(0, dom_1.addEventListener)(triggerEl, ['click'], handleTriggerClick);
|
|
72
86
|
if (getStatus() && isDismissible) {
|
|
@@ -81,10 +95,17 @@ const useFixedBoxMethods = ({ beforeShow, boxEl, getTriggerEl, handlePosition, i
|
|
|
81
95
|
(0, dom_1.addEventListener)(window, ['mousemove'], debounceWindowMouseMove);
|
|
82
96
|
}
|
|
83
97
|
}
|
|
98
|
+
if (triggerInteraction & types_1.TriggerInteraction.RIGHT_CLICK) {
|
|
99
|
+
(0, dom_1.addEventListener)(triggerEl, ['contextmenu'], handleTriggerRightClick);
|
|
100
|
+
if (getStatus()) {
|
|
101
|
+
(0, dom_1.addEventListener)(triggerEl, ['click'], handleTriggerClick);
|
|
102
|
+
(0, dom_1.addEventListener)(window, ['click', 'contextmenu'], handleWindowClick);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
84
105
|
if (getStatus()) {
|
|
85
106
|
(0, dom_1.addEventListener)(window, ['resize', 'scroll'], debouncePosition);
|
|
86
107
|
}
|
|
87
|
-
(0, dom_1.addEventListener)(triggerEl, ['src.fixedBox.setPosition'],
|
|
108
|
+
(0, dom_1.addEventListener)(triggerEl, ['src.fixedBox.setPosition'], handlePositionBind);
|
|
88
109
|
return () => {
|
|
89
110
|
if (triggerInteraction & types_1.TriggerInteraction.CLICK) {
|
|
90
111
|
(0, dom_1.removeEventListener)(triggerEl, ['click'], handleTriggerClick);
|
|
@@ -100,10 +121,17 @@ const useFixedBoxMethods = ({ beforeShow, boxEl, getTriggerEl, handlePosition, i
|
|
|
100
121
|
(0, dom_1.removeEventListener)(window, ['mousemove'], debounceWindowMouseMove);
|
|
101
122
|
}
|
|
102
123
|
}
|
|
124
|
+
if (triggerInteraction & types_1.TriggerInteraction.RIGHT_CLICK) {
|
|
125
|
+
(0, dom_1.removeEventListener)(triggerEl, ['contextmenu'], handleTriggerRightClick);
|
|
126
|
+
if (getStatus()) {
|
|
127
|
+
(0, dom_1.removeEventListener)(triggerEl, ['click'], handleTriggerClick);
|
|
128
|
+
(0, dom_1.removeEventListener)(window, ['click', 'contextmenu'], handleWindowClick);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
103
131
|
if (getStatus()) {
|
|
104
132
|
(0, dom_1.removeEventListener)(window, ['resize', 'scroll'], debouncePosition);
|
|
105
133
|
}
|
|
106
|
-
(0, dom_1.removeEventListener)(triggerEl, [`src.${key}.setPosition`],
|
|
134
|
+
(0, dom_1.removeEventListener)(triggerEl, [`src.${key}.setPosition`], handlePositionBind);
|
|
107
135
|
};
|
|
108
136
|
}, [status, setStatus, localStatus, setLocalStatus, triggerInteraction]);
|
|
109
137
|
return {
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
package/types/index.js
CHANGED
|
@@ -27,4 +27,5 @@ var TriggerInteraction;
|
|
|
27
27
|
TriggerInteraction[TriggerInteraction["NONE"] = 0] = "NONE";
|
|
28
28
|
TriggerInteraction[TriggerInteraction["CLICK"] = 1] = "CLICK";
|
|
29
29
|
TriggerInteraction[TriggerInteraction["HOVER"] = 2] = "HOVER";
|
|
30
|
+
TriggerInteraction[TriggerInteraction["RIGHT_CLICK"] = 4] = "RIGHT_CLICK";
|
|
30
31
|
})(TriggerInteraction = exports.TriggerInteraction || (exports.TriggerInteraction = {}));
|
package/util/fixed-box.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Nullable } from '@smart-react-components/core/types';
|
|
1
2
|
import { Position } from '../types';
|
|
2
3
|
/**
|
|
3
4
|
* Calculated fixed box position and size.
|
|
4
5
|
*/
|
|
5
|
-
export declare const calculatePosition: (triggerEl: HTMLElement, boxEl: HTMLDivElement | null, position: Position, maxWidth: number | undefined, minWidth: number | undefined, space: number) => void;
|
|
6
|
+
export declare const calculatePosition: (triggerEl: HTMLElement, boxEl: HTMLDivElement | null, e: Nullable<MouseEvent>, position: Position, maxWidth: number | undefined, minWidth: number | undefined, space: number) => void;
|
package/util/fixed-box.js
CHANGED
|
@@ -6,7 +6,7 @@ const dom_1 = require("./dom");
|
|
|
6
6
|
/**
|
|
7
7
|
* Calculated fixed box position and size.
|
|
8
8
|
*/
|
|
9
|
-
const calculatePosition = (triggerEl, boxEl, position, maxWidth, minWidth, space) => {
|
|
9
|
+
const calculatePosition = (triggerEl, boxEl, e, position, maxWidth, minWidth, space) => {
|
|
10
10
|
if (!boxEl) {
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
@@ -27,39 +27,43 @@ const calculatePosition = (triggerEl, boxEl, position, maxWidth, minWidth, space
|
|
|
27
27
|
}
|
|
28
28
|
boxEl.style.width = `${width}px`;
|
|
29
29
|
boxEl.setAttribute('style', position & (types_1.Position.LEFT | types_1.Position.RIGHT)
|
|
30
|
-
? calculatePositionBasedOnXAxis(triggerRect, boxEl, position, space, windowWidth, windowHeight)
|
|
31
|
-
: calculatePositionBasedOnYAxis(triggerRect, boxEl, position, space, windowWidth, windowHeight));
|
|
30
|
+
? calculatePositionBasedOnXAxis(triggerRect, boxEl, e, position, space, windowWidth, windowHeight)
|
|
31
|
+
: calculatePositionBasedOnYAxis(triggerRect, boxEl, e, position, space, windowWidth, windowHeight));
|
|
32
32
|
};
|
|
33
33
|
exports.calculatePosition = calculatePosition;
|
|
34
34
|
/**
|
|
35
35
|
* Calculates fixed box position and sized based on X axis position.
|
|
36
36
|
*/
|
|
37
|
-
const calculatePositionBasedOnXAxis = (triggerRect, boxEl, position, space, windowWidth, windowHeight) => {
|
|
37
|
+
const calculatePositionBasedOnXAxis = (triggerRect, boxEl, e, position, space, windowWidth, windowHeight) => {
|
|
38
|
+
var _a, _b;
|
|
38
39
|
// left & width
|
|
39
40
|
let left;
|
|
40
41
|
const width = boxEl.offsetWidth;
|
|
42
|
+
const triggerLeft = (_a = e === null || e === void 0 ? void 0 : e.clientX) !== null && _a !== void 0 ? _a : triggerRect.left;
|
|
43
|
+
const triggerWidth = e ? 0 : triggerRect.width;
|
|
41
44
|
const diffLeft = triggerRect.left - (space + width);
|
|
42
|
-
const diffRight = windowWidth - (
|
|
45
|
+
const diffRight = windowWidth - (triggerLeft + triggerWidth + space + width);
|
|
43
46
|
if ((position === types_1.Position.LEFT && (diffLeft >= 0 || diffLeft >= diffRight))
|
|
44
47
|
|| (position === types_1.Position.RIGHT && diffRight < 0 && diffLeft > diffRight)) {
|
|
45
48
|
left = diffLeft;
|
|
46
49
|
}
|
|
47
50
|
else {
|
|
48
|
-
left =
|
|
51
|
+
left = triggerLeft + triggerWidth + space;
|
|
49
52
|
}
|
|
50
53
|
// top & height
|
|
51
54
|
let top;
|
|
52
55
|
let height = boxEl.offsetHeight;
|
|
53
|
-
const
|
|
56
|
+
const triggerTop = (_b = e === null || e === void 0 ? void 0 : e.clientY) !== null && _b !== void 0 ? _b : triggerRect.top;
|
|
57
|
+
const triggerHeight = e ? 0 : triggerRect.height + triggerRect.top <= windowHeight ? triggerRect.height : (windowHeight - triggerRect.top);
|
|
54
58
|
const diffBottom = windowHeight - (triggerRect.top + height);
|
|
55
59
|
const diffTop = (triggerRect.top + triggerHeight) - height;
|
|
56
60
|
if (diffBottom >= 0 || diffBottom >= diffTop) {
|
|
57
|
-
height = diffBottom > 0 ? height : windowHeight - (
|
|
58
|
-
top =
|
|
61
|
+
height = diffBottom > 0 ? height : windowHeight - (triggerTop + height);
|
|
62
|
+
top = triggerTop;
|
|
59
63
|
}
|
|
60
64
|
else {
|
|
61
65
|
height = height + Math.min(diffTop, 0);
|
|
62
|
-
top = (height > triggerHeight) ? ((
|
|
66
|
+
top = (height > triggerHeight) ? ((triggerTop + triggerHeight) - height) : triggerTop;
|
|
63
67
|
}
|
|
64
68
|
return `
|
|
65
69
|
left: ${left}px;
|
|
@@ -71,31 +75,36 @@ const calculatePositionBasedOnXAxis = (triggerRect, boxEl, position, space, wind
|
|
|
71
75
|
/**
|
|
72
76
|
* Calculates fixed box position and sized based on Y axis position.
|
|
73
77
|
*/
|
|
74
|
-
const calculatePositionBasedOnYAxis = (triggerRect, boxEl, position, space, windowWidth, windowHeight) => {
|
|
78
|
+
const calculatePositionBasedOnYAxis = (triggerRect, boxEl, e, position, space, windowWidth, windowHeight) => {
|
|
79
|
+
var _a, _b;
|
|
75
80
|
// left & width
|
|
76
81
|
let left;
|
|
77
82
|
const width = boxEl.offsetWidth;
|
|
78
|
-
const
|
|
79
|
-
const
|
|
83
|
+
const triggerLeft = (_a = e === null || e === void 0 ? void 0 : e.clientX) !== null && _a !== void 0 ? _a : triggerRect.left;
|
|
84
|
+
const triggerWidth = e ? 0 : triggerRect.width;
|
|
85
|
+
const diffLeft = windowWidth - (triggerLeft + width);
|
|
86
|
+
const diffRight = triggerLeft - width;
|
|
80
87
|
if (diffLeft >= 0 || diffLeft >= diffRight) {
|
|
81
|
-
left =
|
|
88
|
+
left = triggerLeft;
|
|
82
89
|
}
|
|
83
90
|
else {
|
|
84
|
-
left =
|
|
91
|
+
left = triggerLeft + triggerWidth - width;
|
|
85
92
|
}
|
|
86
93
|
// top & height
|
|
87
94
|
let top;
|
|
88
95
|
let height = boxEl.offsetHeight;
|
|
89
|
-
const
|
|
90
|
-
const
|
|
96
|
+
const triggerTop = (_b = e === null || e === void 0 ? void 0 : e.clientY) !== null && _b !== void 0 ? _b : triggerRect.top;
|
|
97
|
+
const triggerHeight = e ? 0 : triggerRect.height;
|
|
98
|
+
const diffBottom = windowHeight - (triggerTop + triggerHeight + height + space);
|
|
99
|
+
const diffTop = (triggerTop + triggerHeight) - (height + 1);
|
|
91
100
|
if ((position === types_1.Position.BOTTOM && (diffBottom >= 0 || diffBottom >= diffTop))
|
|
92
101
|
|| (position === types_1.Position.TOP && diffTop < 0 && diffBottom > diffTop)) {
|
|
93
|
-
height = diffBottom > 0 ? height : (windowHeight - (
|
|
94
|
-
top =
|
|
102
|
+
height = diffBottom > 0 ? height : (windowHeight - (triggerTop + triggerHeight + space));
|
|
103
|
+
top = triggerTop + triggerHeight + space;
|
|
95
104
|
}
|
|
96
105
|
else {
|
|
97
106
|
height = diffTop > 0 ? height : triggerRect.top;
|
|
98
|
-
top = (
|
|
107
|
+
top = (triggerTop + triggerHeight) - (height + 1);
|
|
99
108
|
}
|
|
100
109
|
return `
|
|
101
110
|
left: ${left}px;
|