@votercircle/clay-v2-trigger 1.0.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/index.d.ts +3 -0
- package/index.js +16 -0
- package/package.json +11 -0
- package/popup.d.ts +15 -0
- package/popup.js +258 -0
- package/target.d.ts +9 -0
- package/target.js +44 -0
- package/trigger.d.ts +29 -0
- package/trigger.js +86 -0
- package/trigger.utils.d.ts +15 -0
- package/trigger.utils.js +35 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
exports.__esModule = true;
|
|
14
|
+
exports.Trigger = void 0;
|
|
15
|
+
var trigger_1 = require("./trigger");
|
|
16
|
+
__createBinding(exports, trigger_1, "Trigger");
|
package/package.json
ADDED
package/popup.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HideActionType, PositionProps } from './trigger.utils';
|
|
2
|
+
declare const directionList: readonly ["bottom", "bottom-left", "bottom-right", "top", "top-left", "top-right", "left", "right"];
|
|
3
|
+
export declare type PopupDirectionTypes = typeof directionList[number];
|
|
4
|
+
declare type PopupProps = {
|
|
5
|
+
children?: any;
|
|
6
|
+
width?: number;
|
|
7
|
+
direction?: PopupDirectionTypes;
|
|
8
|
+
hideActionEventList?: HideActionType[];
|
|
9
|
+
position?: PositionProps;
|
|
10
|
+
portal?: Element;
|
|
11
|
+
autoAdjustDirection?: boolean;
|
|
12
|
+
onHide: Function;
|
|
13
|
+
};
|
|
14
|
+
export declare const PopUp: ({ children, width, position, direction, hideActionEventList, onHide, autoAdjustDirection, portal, }: PopupProps) => JSX.Element;
|
|
15
|
+
export {};
|
package/popup.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
exports.__esModule = true;
|
|
29
|
+
exports.PopUp = void 0;
|
|
30
|
+
var react_1 = __importStar(require("react"));
|
|
31
|
+
var react_dom_1 = __importDefault(require("react-dom"));
|
|
32
|
+
var trigger_1 = require("./trigger");
|
|
33
|
+
var trigger_utils_1 = require("./trigger.utils");
|
|
34
|
+
var directionList = [
|
|
35
|
+
'bottom',
|
|
36
|
+
'bottom-left',
|
|
37
|
+
'bottom-right',
|
|
38
|
+
'top',
|
|
39
|
+
'top-left',
|
|
40
|
+
'top-right',
|
|
41
|
+
'left',
|
|
42
|
+
'right',
|
|
43
|
+
];
|
|
44
|
+
var PopUp = function (_a) {
|
|
45
|
+
var children = _a.children, width = _a.width, position = _a.position, _b = _a.direction, direction = _b === void 0 ? 'bottom-left' : _b, hideActionEventList = _a.hideActionEventList, onHide = _a.onHide, _c = _a.autoAdjustDirection, autoAdjustDirection = _c === void 0 ? true : _c, portal = _a.portal;
|
|
46
|
+
var _d = (0, react_1.useState)(), _popup = _d[0], setPopup = _d[1];
|
|
47
|
+
var TriggerContext = (0, trigger_1.useTriggerContext)();
|
|
48
|
+
(0, react_1.useEffect)(function () {
|
|
49
|
+
setPopup(createPopup());
|
|
50
|
+
return function () {
|
|
51
|
+
_popup === null || _popup === void 0 ? void 0 : _popup.remove();
|
|
52
|
+
removeListeners();
|
|
53
|
+
};
|
|
54
|
+
}, []);
|
|
55
|
+
(0, react_1.useEffect)(function () {
|
|
56
|
+
setPopupAxis();
|
|
57
|
+
setListeners();
|
|
58
|
+
return function () {
|
|
59
|
+
removeListeners();
|
|
60
|
+
};
|
|
61
|
+
}, [_popup]);
|
|
62
|
+
function setPopupAxis() {
|
|
63
|
+
if (!_popup) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (width) {
|
|
67
|
+
_popup.style.width = width + 'px';
|
|
68
|
+
}
|
|
69
|
+
setDirection(direction);
|
|
70
|
+
if (autoAdjustDirection) {
|
|
71
|
+
autoAdjust(direction);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function setDirection(newDirection) {
|
|
75
|
+
if (!_popup) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
var popupRect = _popup.getBoundingClientRect();
|
|
79
|
+
switch (newDirection) {
|
|
80
|
+
case 'bottom':
|
|
81
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px - ").concat(_popup.offsetWidth / 2, "px + ").concat(((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerWidth) || 0) / 2, "px)");
|
|
82
|
+
_popup.style.top = (position === null || position === void 0 ? void 0 : position.y) + 'px';
|
|
83
|
+
break;
|
|
84
|
+
case 'bottom-right':
|
|
85
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px - ").concat(_popup.offsetWidth, "px + ").concat(TriggerContext.getTriggerWidth, "px)");
|
|
86
|
+
_popup.style.top = (position === null || position === void 0 ? void 0 : position.y) + 'px';
|
|
87
|
+
break;
|
|
88
|
+
case 'bottom-left':
|
|
89
|
+
_popup.style.left = Math.ceil((position === null || position === void 0 ? void 0 : position.x) || 0) + 'px';
|
|
90
|
+
_popup.style.top = (position === null || position === void 0 ? void 0 : position.y) + 'px';
|
|
91
|
+
break;
|
|
92
|
+
case 'top':
|
|
93
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px - ").concat(_popup.offsetWidth / 2, "px + ").concat(((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerWidth) || 0) / 2, "px)");
|
|
94
|
+
_popup.style.top =
|
|
95
|
+
((position === null || position === void 0 ? void 0 : position.y) || 0) - popupRect.height - ((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerHeight) || 0) + 'px';
|
|
96
|
+
break;
|
|
97
|
+
case 'top-right':
|
|
98
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px - ").concat(_popup.offsetWidth, "px + ").concat(TriggerContext.getTriggerWidth, "px)");
|
|
99
|
+
_popup.style.top =
|
|
100
|
+
((position === null || position === void 0 ? void 0 : position.y) || 0) - popupRect.height - ((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerHeight) || 0) + 'px';
|
|
101
|
+
break;
|
|
102
|
+
case 'top-left':
|
|
103
|
+
_popup.style.left = Math.ceil((position === null || position === void 0 ? void 0 : position.x) || 0) + 'px';
|
|
104
|
+
_popup.style.top =
|
|
105
|
+
((position === null || position === void 0 ? void 0 : position.y) || 0) - popupRect.height - ((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerHeight) || 0) + 'px';
|
|
106
|
+
break;
|
|
107
|
+
case 'left':
|
|
108
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px - ").concat(_popup.offsetWidth, "px)");
|
|
109
|
+
_popup.style.top =
|
|
110
|
+
((position === null || position === void 0 ? void 0 : position.y) || 0) -
|
|
111
|
+
popupRect.height / 2 -
|
|
112
|
+
((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerHeight) || 0) / 2 +
|
|
113
|
+
'px';
|
|
114
|
+
break;
|
|
115
|
+
case 'right':
|
|
116
|
+
_popup.style.left = "calc(".concat(position === null || position === void 0 ? void 0 : position.x, "px + ").concat(TriggerContext.getTriggerWidth, "px)");
|
|
117
|
+
_popup.style.top =
|
|
118
|
+
((position === null || position === void 0 ? void 0 : position.y) || 0) -
|
|
119
|
+
popupRect.height / 2 -
|
|
120
|
+
((TriggerContext === null || TriggerContext === void 0 ? void 0 : TriggerContext.getTriggerHeight) || 0) / 2 +
|
|
121
|
+
'px';
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// if the popup goes beyond bounding box, adjust the direction
|
|
126
|
+
function autoAdjust(newDirection) {
|
|
127
|
+
if (!_popup) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
var popupRect = _popup.getBoundingClientRect();
|
|
131
|
+
var _a = document.documentElement, clientHeight = _a.clientHeight, clientWidth = _a.clientWidth;
|
|
132
|
+
var isOverflowBottom = popupRect.bottom > clientHeight;
|
|
133
|
+
var isOverflowTop = popupRect.top < 0;
|
|
134
|
+
var isOverflowRight = popupRect.right > clientWidth;
|
|
135
|
+
var isOverflowLeft = popupRect.left < 0;
|
|
136
|
+
switch (newDirection) {
|
|
137
|
+
case 'left':
|
|
138
|
+
if (isOverflowLeft) {
|
|
139
|
+
setDirection('right');
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
case 'right':
|
|
143
|
+
if (isOverflowRight) {
|
|
144
|
+
setDirection('left');
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
case 'top':
|
|
148
|
+
if (isOverflowTop) {
|
|
149
|
+
setDirection('bottom');
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
case 'top-right':
|
|
153
|
+
if (isOverflowTop) {
|
|
154
|
+
setDirection('bottom-right');
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case 'top-left':
|
|
158
|
+
if (isOverflowTop) {
|
|
159
|
+
setDirection('bottom-left');
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
case 'bottom':
|
|
163
|
+
if (isOverflowBottom) {
|
|
164
|
+
setDirection('top');
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
case 'bottom-right':
|
|
168
|
+
if (isOverflowBottom) {
|
|
169
|
+
setDirection('top-right');
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
case 'bottom-left':
|
|
173
|
+
if (isOverflowBottom) {
|
|
174
|
+
setDirection('top-left');
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
switch (newDirection) {
|
|
179
|
+
case 'left':
|
|
180
|
+
case 'right':
|
|
181
|
+
if (isOverflowBottom) {
|
|
182
|
+
_popup.style.top = "calc(".concat(_popup.style.top, " + ").concat(clientHeight - popupRect.bottom, "px)");
|
|
183
|
+
}
|
|
184
|
+
else if (isOverflowTop) {
|
|
185
|
+
_popup.style.top = '0px';
|
|
186
|
+
}
|
|
187
|
+
break;
|
|
188
|
+
case 'top':
|
|
189
|
+
case 'bottom':
|
|
190
|
+
if (isOverflowLeft) {
|
|
191
|
+
_popup.style.left = '0px';
|
|
192
|
+
}
|
|
193
|
+
else if (isOverflowRight) {
|
|
194
|
+
_popup.style.left = 'auto';
|
|
195
|
+
_popup.style.right = '0px';
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
case 'top-right':
|
|
199
|
+
case 'bottom-right':
|
|
200
|
+
if (isOverflowLeft) {
|
|
201
|
+
_popup.style.left = '0px';
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
case 'top-left':
|
|
205
|
+
case 'bottom-left':
|
|
206
|
+
if (isOverflowRight) {
|
|
207
|
+
_popup.style.left = 'auto';
|
|
208
|
+
_popup.style.right = '0px';
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* We need to construct all the event listeners mentioned in the type HideActionType
|
|
215
|
+
*/
|
|
216
|
+
function setListeners() {
|
|
217
|
+
if (hideActionEventList === null || hideActionEventList === void 0 ? void 0 : hideActionEventList.find(function (item) { return ['onClick', 'onOutsideClick'].includes(item); })) {
|
|
218
|
+
document.addEventListener('click', handleClickEvents, true);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* We need to destruct all the event listeners mentioned in the type HideActionType
|
|
223
|
+
*/
|
|
224
|
+
function removeListeners() {
|
|
225
|
+
if (hideActionEventList === null || hideActionEventList === void 0 ? void 0 : hideActionEventList.find(function (item) { return ['onClick', 'onOutsideClick'].includes(item); })) {
|
|
226
|
+
document.removeEventListener('click', handleClickEvents, true);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function createPopup() {
|
|
230
|
+
var popup = getContainer();
|
|
231
|
+
var popupPortal = portal;
|
|
232
|
+
if (!popupPortal) {
|
|
233
|
+
popupPortal = document.createElement('div');
|
|
234
|
+
popupPortal.setAttribute('id', trigger_utils_1.PORTAL_MAP.POPUP);
|
|
235
|
+
document.body.insertBefore(popupPortal, null);
|
|
236
|
+
}
|
|
237
|
+
popupPortal === null || popupPortal === void 0 ? void 0 : popupPortal.appendChild(popup);
|
|
238
|
+
return popup;
|
|
239
|
+
}
|
|
240
|
+
function getContainer() {
|
|
241
|
+
var popupContainer = window.document.createElement('div');
|
|
242
|
+
popupContainer.style.position = 'absolute';
|
|
243
|
+
popupContainer.style.top = '0';
|
|
244
|
+
popupContainer.style.left = '0';
|
|
245
|
+
popupContainer.style.zIndex = '99';
|
|
246
|
+
return popupContainer;
|
|
247
|
+
}
|
|
248
|
+
function handleClickEvents(event) {
|
|
249
|
+
if ((hideActionEventList === null || hideActionEventList === void 0 ? void 0 : hideActionEventList.includes('onOutsideClick')) && !(_popup === null || _popup === void 0 ? void 0 : _popup.contains(event.target))) {
|
|
250
|
+
onHide();
|
|
251
|
+
}
|
|
252
|
+
else if ((hideActionEventList === null || hideActionEventList === void 0 ? void 0 : hideActionEventList.includes('onClick')) && (_popup === null || _popup === void 0 ? void 0 : _popup.contains(event.target))) {
|
|
253
|
+
onHide();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return _popup ? react_dom_1["default"].createPortal(children, _popup) : react_1["default"].createElement(react_1["default"].Fragment, null);
|
|
257
|
+
};
|
|
258
|
+
exports.PopUp = PopUp;
|
package/target.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ShowActionType } from './trigger.utils';
|
|
3
|
+
export declare type TargetProps = {
|
|
4
|
+
children?: any;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
showAction?: ShowActionType[];
|
|
7
|
+
onTrigger: Function;
|
|
8
|
+
};
|
|
9
|
+
export declare const Target: React.ForwardRefExoticComponent<TargetProps & React.RefAttributes<HTMLElement>>;
|
package/target.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
exports.__esModule = true;
|
|
17
|
+
exports.Target = void 0;
|
|
18
|
+
var react_1 = __importDefault(require("react"));
|
|
19
|
+
exports.Target = react_1["default"].forwardRef(function (_a, ref) {
|
|
20
|
+
var children = _a.children, onTrigger = _a.onTrigger, showAction = _a.showAction, disabled = _a.disabled;
|
|
21
|
+
/**
|
|
22
|
+
* Handles onClick event from Child node
|
|
23
|
+
*/
|
|
24
|
+
var onClick = function () {
|
|
25
|
+
onTrigger();
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Shallow bind required events to the child node
|
|
29
|
+
*/
|
|
30
|
+
function destructureProps() {
|
|
31
|
+
var cloneProps = {
|
|
32
|
+
ref: ref
|
|
33
|
+
};
|
|
34
|
+
if (disabled) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (showAction === null || showAction === void 0 ? void 0 : showAction.includes('onClick')) {
|
|
38
|
+
cloneProps.onClickCapture = onClick;
|
|
39
|
+
}
|
|
40
|
+
return cloneProps;
|
|
41
|
+
}
|
|
42
|
+
var TriggerChild = react_1["default"].Children.only(children);
|
|
43
|
+
return react_1["default"].cloneElement(TriggerChild, __assign({}, destructureProps()));
|
|
44
|
+
});
|
package/trigger.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PopupDirectionTypes } from './popup';
|
|
2
|
+
import { ShowActionType, HideActionType } from './trigger.utils';
|
|
3
|
+
export declare type TriggerProps = {
|
|
4
|
+
children?: any;
|
|
5
|
+
target?: any;
|
|
6
|
+
popup?: any;
|
|
7
|
+
popupWidth?: number;
|
|
8
|
+
popupDirection?: PopupDirectionTypes;
|
|
9
|
+
showActionEvents?: ShowActionType[];
|
|
10
|
+
hideActionEvents?: HideActionType[];
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
autoAdjustDirection?: boolean;
|
|
13
|
+
onClose?: Function;
|
|
14
|
+
onOpen?: Function;
|
|
15
|
+
};
|
|
16
|
+
declare type TriggerCloseProps = {
|
|
17
|
+
children?: any;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare const useTriggerContext: () => {
|
|
21
|
+
close: () => void;
|
|
22
|
+
getTriggerWidth?: number;
|
|
23
|
+
getTriggerHeight?: number;
|
|
24
|
+
};
|
|
25
|
+
export declare function Trigger({ target, popup, popupWidth, popupDirection, showActionEvents, hideActionEvents, disabled, autoAdjustDirection, onClose, onOpen, }: TriggerProps): JSX.Element;
|
|
26
|
+
export declare namespace Trigger {
|
|
27
|
+
var Close: ({ children, disabled }: TriggerCloseProps) => JSX.Element;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
package/trigger.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
exports.__esModule = true;
|
|
26
|
+
exports.Trigger = exports.useTriggerContext = void 0;
|
|
27
|
+
var react_1 = __importStar(require("react"));
|
|
28
|
+
var popup_1 = require("./popup");
|
|
29
|
+
var target_1 = require("./target");
|
|
30
|
+
var trigger_utils_1 = require("./trigger.utils");
|
|
31
|
+
var TriggerContext = react_1["default"].createContext({});
|
|
32
|
+
var useTriggerContext = function () { return (0, react_1.useContext)(TriggerContext); };
|
|
33
|
+
exports.useTriggerContext = useTriggerContext;
|
|
34
|
+
function Trigger(_a) {
|
|
35
|
+
var target = _a.target, popup = _a.popup, popupWidth = _a.popupWidth, popupDirection = _a.popupDirection, showActionEvents = _a.showActionEvents, hideActionEvents = _a.hideActionEvents, disabled = _a.disabled, autoAdjustDirection = _a.autoAdjustDirection, onClose = _a.onClose, onOpen = _a.onOpen;
|
|
36
|
+
var _b = (0, react_1.useState)(false), showPopup = _b[0], setPopupState = _b[1];
|
|
37
|
+
var _c = (0, react_1.useState)(), popupPortal = _c[0], setPopupPortal = _c[1];
|
|
38
|
+
var _d = (0, react_1.useState)(), popupPosition = _d[0], setPopupPosition = _d[1];
|
|
39
|
+
var _e = (0, react_1.useState)(), triggerDimension = _e[0], setTriggerDimension = _e[1];
|
|
40
|
+
var elRef = (0, react_1.createRef)();
|
|
41
|
+
(0, react_1.useEffect)(function () {
|
|
42
|
+
var parentPortal = (0, trigger_utils_1.getParentPortal)(elRef === null || elRef === void 0 ? void 0 : elRef.current);
|
|
43
|
+
if (parentPortal) {
|
|
44
|
+
setPopupPortal(parentPortal);
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
function showPopupState() {
|
|
48
|
+
var _a;
|
|
49
|
+
var el = (_a = elRef === null || elRef === void 0 ? void 0 : elRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
|
|
50
|
+
var absolutePosition = (0, trigger_utils_1.getElementPosition)(elRef === null || elRef === void 0 ? void 0 : elRef.current);
|
|
51
|
+
var p = { x: el.x || 0, y: (absolutePosition === null || absolutePosition === void 0 ? void 0 : absolutePosition.y) || 0 };
|
|
52
|
+
if (!showPopup && p) {
|
|
53
|
+
setPopupPosition(p);
|
|
54
|
+
setTriggerDimension({ width: el.width, height: el.height });
|
|
55
|
+
}
|
|
56
|
+
setPopupState(true);
|
|
57
|
+
if (onOpen) {
|
|
58
|
+
onOpen();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function hidePopup() {
|
|
62
|
+
setTimeout(function () { return setPopupState(false); });
|
|
63
|
+
if (onClose) {
|
|
64
|
+
onClose();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return (react_1["default"].createElement(TriggerContext.Provider, { value: {
|
|
68
|
+
close: hidePopup,
|
|
69
|
+
getTriggerWidth: triggerDimension === null || triggerDimension === void 0 ? void 0 : triggerDimension.width,
|
|
70
|
+
getTriggerHeight: triggerDimension === null || triggerDimension === void 0 ? void 0 : triggerDimension.height
|
|
71
|
+
} },
|
|
72
|
+
react_1["default"].createElement(target_1.Target, { ref: elRef, onTrigger: showPopupState, showAction: showActionEvents, disabled: disabled }, target),
|
|
73
|
+
showPopup && (react_1["default"].createElement(popup_1.PopUp, { width: popupWidth, direction: popupDirection, position: popupPosition, hideActionEventList: hideActionEvents, onHide: hidePopup, portal: popupPortal, autoAdjustDirection: autoAdjustDirection }, popup))));
|
|
74
|
+
}
|
|
75
|
+
exports.Trigger = Trigger;
|
|
76
|
+
/**
|
|
77
|
+
* This helps to manually hide the popup by trigerring a close event inside the popup
|
|
78
|
+
*/
|
|
79
|
+
Trigger.Close = function (_a) {
|
|
80
|
+
var children = _a.children, disabled = _a.disabled;
|
|
81
|
+
var triggerContext = (0, exports.useTriggerContext)();
|
|
82
|
+
function closePopup() {
|
|
83
|
+
triggerContext.close();
|
|
84
|
+
}
|
|
85
|
+
return (react_1["default"].createElement(target_1.Target, { onTrigger: closePopup, disabled: disabled, showAction: ['onClick'] }, children));
|
|
86
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const ShowActionList: readonly ["onClick"];
|
|
2
|
+
declare const HideActionList: readonly ["onClick", "onOutsideClick"];
|
|
3
|
+
export declare enum PORTAL_MAP {
|
|
4
|
+
DIALOG = "clay-ui-dialog-portal-container",
|
|
5
|
+
POPUP = "clay-ui-popup-portal-container"
|
|
6
|
+
}
|
|
7
|
+
export declare type ShowActionType = typeof ShowActionList[number];
|
|
8
|
+
export declare type HideActionType = typeof HideActionList[number];
|
|
9
|
+
export declare type PositionProps = {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function getElementPosition(targetEl: any): PositionProps | null;
|
|
14
|
+
export declare function getParentPortal(targetEl: Element): Element | null;
|
|
15
|
+
export {};
|
package/trigger.utils.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.getParentPortal = exports.getElementPosition = exports.PORTAL_MAP = void 0;
|
|
4
|
+
var ShowActionList = ['onClick'];
|
|
5
|
+
var HideActionList = ['onClick', 'onOutsideClick'];
|
|
6
|
+
var PORTAL_MAP;
|
|
7
|
+
(function (PORTAL_MAP) {
|
|
8
|
+
PORTAL_MAP["DIALOG"] = "clay-ui-dialog-portal-container";
|
|
9
|
+
PORTAL_MAP["POPUP"] = "clay-ui-popup-portal-container";
|
|
10
|
+
})(PORTAL_MAP = exports.PORTAL_MAP || (exports.PORTAL_MAP = {}));
|
|
11
|
+
function getElementPosition(targetEl) {
|
|
12
|
+
if (!targetEl) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
var element = targetEl;
|
|
16
|
+
var p = {
|
|
17
|
+
x: element.offsetLeft || 0,
|
|
18
|
+
y: targetEl.offsetHeight + (element.offsetTop || 0)
|
|
19
|
+
};
|
|
20
|
+
while ((element = element.offsetParent)) {
|
|
21
|
+
p.x += element.offsetLeft;
|
|
22
|
+
p.y += element.offsetTop;
|
|
23
|
+
}
|
|
24
|
+
return p;
|
|
25
|
+
}
|
|
26
|
+
exports.getElementPosition = getElementPosition;
|
|
27
|
+
function getParentPortal(targetEl) {
|
|
28
|
+
if (!targetEl) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return [PORTAL_MAP.DIALOG].includes(targetEl.id)
|
|
32
|
+
? targetEl
|
|
33
|
+
: getParentPortal(targetEl.parentNode);
|
|
34
|
+
}
|
|
35
|
+
exports.getParentPortal = getParentPortal;
|