@bifrostui/utils 1.4.2 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/directionLocationUtil.d.ts +25 -6
- package/dist/directionLocationUtil.js +96 -36
- package/dist/getRootElement/index.miniapp.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -0
- package/es/directionLocationUtil.d.ts +25 -6
- package/es/directionLocationUtil.js +95 -36
- package/es/index.d.ts +1 -1
- package/es/index.js +3 -1
- package/package.json +1 -1
@@ -1,11 +1,14 @@
|
|
1
1
|
/**
|
2
2
|
* 根据元素宽高判断是否超出边界,超出边界则重新定义方向
|
3
3
|
*/
|
4
|
-
export declare const getNewDirectionLocation: ({
|
5
|
-
|
4
|
+
export declare const getNewDirectionLocation: ({ scrollRoot, scrollRootOffset, childrenOffset, arrowDirection, tipOffset, arrowLocation, offsetSpacing, }: {
|
5
|
+
scrollRoot: any;
|
6
|
+
scrollRootOffset: any;
|
7
|
+
childrenOffset: any;
|
6
8
|
arrowDirection: any;
|
7
9
|
tipOffset: any;
|
8
10
|
arrowLocation: any;
|
11
|
+
offsetSpacing: any;
|
9
12
|
}) => {
|
10
13
|
newArrowDirection: any;
|
11
14
|
newArrowLocation: any;
|
@@ -13,22 +16,29 @@ export declare const getNewDirectionLocation: ({ rootOffset, arrowDirection, tip
|
|
13
16
|
/**
|
14
17
|
* 根据新的气泡位置和箭头位置 计算气泡位置以及箭头位置
|
15
18
|
*/
|
16
|
-
export declare const getDirectionLocationStyle: ({
|
17
|
-
|
19
|
+
export declare const getDirectionLocationStyle: ({ childrenOffset, arrowDirection, tipOffset, arrowLocation, offsetSpacing, }: {
|
20
|
+
childrenOffset: any;
|
18
21
|
arrowDirection: any;
|
19
22
|
tipOffset: any;
|
20
23
|
arrowLocation: any;
|
21
|
-
|
24
|
+
offsetSpacing: any;
|
25
|
+
}) => {
|
26
|
+
styles: any;
|
27
|
+
childrenStyle: any;
|
28
|
+
};
|
22
29
|
/**
|
23
30
|
* 获取气泡位置和箭头位置
|
24
31
|
*/
|
25
|
-
export declare const getStylesAndLocation: ({ childrenRef, arrowDirection, arrowLocation, selector, }: {
|
32
|
+
export declare const getStylesAndLocation: ({ scrollRoot, childrenRef, arrowDirection, arrowLocation, offsetSpacing, selector, }: {
|
33
|
+
scrollRoot?: Element;
|
26
34
|
childrenRef: any;
|
27
35
|
arrowDirection: any;
|
28
36
|
arrowLocation: any;
|
37
|
+
offsetSpacing: any;
|
29
38
|
selector: any;
|
30
39
|
}) => {
|
31
40
|
styles: any;
|
41
|
+
childrenStyle: any;
|
32
42
|
newArrowDirection: any;
|
33
43
|
newArrowLocation: any;
|
34
44
|
};
|
@@ -38,3 +48,12 @@ export declare const triggerEventTransform: ({ trigger, click, show, hide }: {
|
|
38
48
|
show: any;
|
39
49
|
hide: any;
|
40
50
|
}) => {};
|
51
|
+
/**
|
52
|
+
* for example: placement = 'topLeft', return { direction: 'top', location: 'left' }
|
53
|
+
* @param placement
|
54
|
+
* @returns
|
55
|
+
*/
|
56
|
+
export declare const parsePlacement: (placement: any) => {
|
57
|
+
direction: any;
|
58
|
+
location: any;
|
59
|
+
};
|
@@ -20,6 +20,7 @@ __export(directionLocationUtil_exports, {
|
|
20
20
|
getDirectionLocationStyle: () => getDirectionLocationStyle,
|
21
21
|
getNewDirectionLocation: () => getNewDirectionLocation,
|
22
22
|
getStylesAndLocation: () => getStylesAndLocation,
|
23
|
+
parsePlacement: () => parsePlacement,
|
23
24
|
triggerEventTransform: () => triggerEventTransform
|
24
25
|
});
|
25
26
|
module.exports = __toCommonJS(directionLocationUtil_exports);
|
@@ -29,55 +30,82 @@ const directionCssMap = {
|
|
29
30
|
top: "bottom",
|
30
31
|
bottom: "top"
|
31
32
|
};
|
33
|
+
const isBodyScroll = (scrollRoot) => {
|
34
|
+
return scrollRoot === document.body;
|
35
|
+
};
|
32
36
|
const getNewDirectionLocation = ({
|
33
|
-
|
37
|
+
scrollRoot,
|
38
|
+
scrollRootOffset,
|
39
|
+
childrenOffset,
|
34
40
|
arrowDirection,
|
35
41
|
tipOffset,
|
36
|
-
arrowLocation
|
42
|
+
arrowLocation,
|
43
|
+
offsetSpacing
|
37
44
|
}) => {
|
38
|
-
const {
|
45
|
+
const {
|
46
|
+
left: cLeft,
|
47
|
+
right: cRight,
|
48
|
+
top: cTop,
|
49
|
+
bottom: cBottom,
|
50
|
+
width: cWidth,
|
51
|
+
height: cHeight
|
52
|
+
} = childrenOffset;
|
39
53
|
const { width, height } = tipOffset;
|
40
|
-
const
|
41
|
-
|
54
|
+
const {
|
55
|
+
top: sTop,
|
56
|
+
bottom: sBottom,
|
57
|
+
left: sLeft,
|
58
|
+
right: sRight
|
59
|
+
} = scrollRootOffset;
|
60
|
+
const pageWidth = document.documentElement.clientWidth || document.body.clientWidth;
|
61
|
+
const pageHeight = document.documentElement.clientHeight || document.body.clientHeight;
|
62
|
+
const maxTop = isBodyScroll(scrollRoot) ? 0 : sTop;
|
63
|
+
const maxBottom = isBodyScroll(scrollRoot) ? pageHeight : sBottom;
|
64
|
+
const maxLeft = isBodyScroll(scrollRoot) ? 0 : sLeft;
|
65
|
+
const maxRight = isBodyScroll(scrollRoot) ? pageWidth : sRight;
|
42
66
|
let newArrowDirection = arrowDirection;
|
43
67
|
let newArrowLocation = arrowLocation;
|
44
68
|
const isDirectionTop = arrowDirection === "top";
|
45
69
|
const isDirectionBottom = arrowDirection === "bottom";
|
46
70
|
const isDirectionLeft = arrowDirection === "left";
|
47
71
|
const isDirectionRight = arrowDirection === "right";
|
48
|
-
if (isDirectionTop && cTop - height <
|
72
|
+
if (isDirectionTop && cTop - height - offsetSpacing < maxTop || isDirectionBottom && cBottom + height + offsetSpacing > maxBottom || isDirectionLeft && cLeft - width - offsetSpacing < maxLeft || isDirectionRight && cRight + width + offsetSpacing > maxRight) {
|
49
73
|
newArrowDirection = directionCssMap[arrowDirection];
|
50
74
|
}
|
51
|
-
if (arrowLocation === "top" && cTop + height >
|
75
|
+
if (arrowLocation === "top" && cTop + height > maxBottom || arrowLocation === "bottom" && cBottom - height < maxTop || arrowLocation === "left" && cLeft + width > maxRight || arrowLocation === "right" && cRight - width < maxLeft) {
|
52
76
|
newArrowLocation = directionCssMap[arrowLocation];
|
53
77
|
}
|
54
78
|
const isCenter = arrowLocation === "center";
|
55
79
|
if (isCenter && (isDirectionTop || isDirectionBottom)) {
|
56
|
-
if (cLeft + width >
|
80
|
+
if (cLeft + (cWidth - width) / 2 + width > maxRight) {
|
57
81
|
newArrowLocation = directionCssMap.left;
|
58
|
-
} else if (
|
82
|
+
} else if (cLeft + (cWidth - width) / 2 < maxLeft) {
|
59
83
|
newArrowLocation = directionCssMap.right;
|
60
84
|
}
|
61
85
|
} else if (isCenter && (isDirectionLeft || isDirectionRight)) {
|
62
|
-
if (cTop + height >
|
86
|
+
if (cTop + (cHeight - height) / 2 + cHeight > maxBottom) {
|
63
87
|
newArrowLocation = directionCssMap.top;
|
64
|
-
} else if (
|
88
|
+
} else if (cTop + (cHeight - height) / 2 < maxTop) {
|
65
89
|
newArrowLocation = directionCssMap.bottom;
|
66
90
|
}
|
67
91
|
}
|
92
|
+
if (arrowLocation === "none") {
|
93
|
+
newArrowLocation = "none";
|
94
|
+
}
|
68
95
|
return {
|
69
96
|
newArrowDirection,
|
70
97
|
newArrowLocation
|
71
98
|
};
|
72
99
|
};
|
73
100
|
const getDirectionLocationStyle = ({
|
74
|
-
|
101
|
+
childrenOffset,
|
75
102
|
arrowDirection,
|
76
103
|
tipOffset,
|
77
|
-
arrowLocation
|
104
|
+
arrowLocation,
|
105
|
+
offsetSpacing
|
78
106
|
}) => {
|
79
107
|
const scrollTop = window.scrollY >= 0 && window.scrollY || document.documentElement.scrollTop;
|
80
|
-
const scrollLeft = window.
|
108
|
+
const scrollLeft = window.scrollX >= 0 && window.scrollX || document.documentElement.scrollLeft;
|
81
109
|
const styles = {};
|
82
110
|
const {
|
83
111
|
width: cWidth,
|
@@ -86,11 +114,14 @@ const getDirectionLocationStyle = ({
|
|
86
114
|
right: cRight,
|
87
115
|
top: cTop,
|
88
116
|
bottom: cBottom
|
89
|
-
} =
|
117
|
+
} = childrenOffset;
|
118
|
+
let childrenStyle = {};
|
119
|
+
if (cWidth && cHeight) {
|
120
|
+
childrenStyle = { width: `${cWidth}px`, height: `${cHeight}px` };
|
121
|
+
}
|
90
122
|
const { width, height } = tipOffset;
|
91
123
|
if (arrowDirection === "top") {
|
92
|
-
styles.top = cTop;
|
93
|
-
styles.transform = `translateY(-100%)`;
|
124
|
+
styles.top = cTop - offsetSpacing - height;
|
94
125
|
switch (arrowLocation) {
|
95
126
|
case "left":
|
96
127
|
styles.left = cLeft;
|
@@ -99,14 +130,16 @@ const getDirectionLocationStyle = ({
|
|
99
130
|
styles.left = cLeft + (cWidth - width) / 2;
|
100
131
|
break;
|
101
132
|
case "right":
|
102
|
-
styles.left = cRight;
|
103
|
-
|
133
|
+
styles.left = cRight - width;
|
134
|
+
break;
|
135
|
+
case "none":
|
136
|
+
styles.left = cLeft;
|
104
137
|
break;
|
105
138
|
default:
|
106
139
|
break;
|
107
140
|
}
|
108
141
|
} else if (arrowDirection === "bottom") {
|
109
|
-
styles.top = cBottom;
|
142
|
+
styles.top = cBottom + offsetSpacing;
|
110
143
|
switch (arrowLocation) {
|
111
144
|
case "left":
|
112
145
|
styles.left = cLeft;
|
@@ -115,15 +148,16 @@ const getDirectionLocationStyle = ({
|
|
115
148
|
styles.left = cLeft + (cWidth - width) / 2;
|
116
149
|
break;
|
117
150
|
case "right":
|
118
|
-
styles.left = cRight;
|
119
|
-
|
151
|
+
styles.left = cRight - width;
|
152
|
+
break;
|
153
|
+
case "none":
|
154
|
+
styles.left = cLeft;
|
120
155
|
break;
|
121
156
|
default:
|
122
157
|
break;
|
123
158
|
}
|
124
159
|
} else if (arrowDirection === "left") {
|
125
|
-
styles.left = cLeft;
|
126
|
-
styles.transform = `translateX(-100%)`;
|
160
|
+
styles.left = cLeft - offsetSpacing - width;
|
127
161
|
switch (arrowLocation) {
|
128
162
|
case "top":
|
129
163
|
styles.top = cTop;
|
@@ -132,14 +166,16 @@ const getDirectionLocationStyle = ({
|
|
132
166
|
styles.top = cTop + (cHeight - height) / 2;
|
133
167
|
break;
|
134
168
|
case "bottom":
|
135
|
-
styles.top = cBottom;
|
136
|
-
|
169
|
+
styles.top = cBottom - height;
|
170
|
+
break;
|
171
|
+
case "none":
|
172
|
+
styles.top = cTop;
|
137
173
|
break;
|
138
174
|
default:
|
139
175
|
break;
|
140
176
|
}
|
141
177
|
} else if (arrowDirection === "right") {
|
142
|
-
styles.left = cRight;
|
178
|
+
styles.left = cRight + offsetSpacing;
|
143
179
|
switch (arrowLocation) {
|
144
180
|
case "top":
|
145
181
|
styles.top = cTop;
|
@@ -148,8 +184,10 @@ const getDirectionLocationStyle = ({
|
|
148
184
|
styles.top = cTop + (cHeight - height) / 2;
|
149
185
|
break;
|
150
186
|
case "bottom":
|
151
|
-
styles.top = cBottom;
|
152
|
-
|
187
|
+
styles.top = cBottom - height;
|
188
|
+
break;
|
189
|
+
case "none":
|
190
|
+
styles.top = cTop;
|
153
191
|
break;
|
154
192
|
default:
|
155
193
|
break;
|
@@ -161,12 +199,14 @@ const getDirectionLocationStyle = ({
|
|
161
199
|
if (styles.left) {
|
162
200
|
styles.left = `${styles.left + scrollLeft}px`;
|
163
201
|
}
|
164
|
-
return styles;
|
202
|
+
return { styles, childrenStyle };
|
165
203
|
};
|
166
204
|
const getStylesAndLocation = ({
|
205
|
+
scrollRoot = document.body,
|
167
206
|
childrenRef,
|
168
207
|
arrowDirection,
|
169
208
|
arrowLocation,
|
209
|
+
offsetSpacing,
|
170
210
|
selector
|
171
211
|
}) => {
|
172
212
|
if (!(childrenRef == null ? void 0 : childrenRef.current)) {
|
@@ -175,26 +215,32 @@ const getStylesAndLocation = ({
|
|
175
215
|
);
|
176
216
|
return null;
|
177
217
|
}
|
178
|
-
const
|
218
|
+
const childrenOffset = childrenRef.current.getBoundingClientRect();
|
179
219
|
const $rtDom = document.querySelector(selector);
|
180
220
|
if (!$rtDom)
|
181
221
|
return null;
|
182
222
|
const tipOffset = $rtDom.getBoundingClientRect();
|
223
|
+
const scrollRootOffset = scrollRoot.getBoundingClientRect();
|
183
224
|
const { newArrowDirection, newArrowLocation } = getNewDirectionLocation({
|
184
|
-
|
225
|
+
scrollRoot,
|
226
|
+
scrollRootOffset,
|
227
|
+
childrenOffset,
|
185
228
|
arrowDirection,
|
186
229
|
tipOffset,
|
187
|
-
arrowLocation
|
230
|
+
arrowLocation,
|
231
|
+
offsetSpacing
|
188
232
|
});
|
189
|
-
const styles = getDirectionLocationStyle({
|
190
|
-
|
233
|
+
const { styles, childrenStyle } = getDirectionLocationStyle({
|
234
|
+
childrenOffset,
|
191
235
|
arrowDirection: newArrowDirection,
|
192
236
|
tipOffset,
|
193
|
-
arrowLocation: newArrowLocation
|
237
|
+
arrowLocation: newArrowLocation,
|
238
|
+
offsetSpacing
|
194
239
|
});
|
195
240
|
styles.visibility = "visible";
|
196
241
|
return {
|
197
242
|
styles,
|
243
|
+
childrenStyle,
|
198
244
|
newArrowDirection,
|
199
245
|
newArrowLocation
|
200
246
|
};
|
@@ -232,10 +278,24 @@ const triggerEventTransform = ({ trigger, click, show, hide }) => {
|
|
232
278
|
});
|
233
279
|
return option;
|
234
280
|
};
|
281
|
+
const parsePlacement = (placement) => {
|
282
|
+
const positionArr = placement.split(/([A-Z])/);
|
283
|
+
const direction = positionArr[0];
|
284
|
+
let location;
|
285
|
+
if (positionArr.length > 1) {
|
286
|
+
positionArr.splice(0, 1);
|
287
|
+
location = positionArr.join("").toLowerCase();
|
288
|
+
}
|
289
|
+
return {
|
290
|
+
direction,
|
291
|
+
location
|
292
|
+
};
|
293
|
+
};
|
235
294
|
// Annotate the CommonJS export names for ESM import in node:
|
236
295
|
0 && (module.exports = {
|
237
296
|
getDirectionLocationStyle,
|
238
297
|
getNewDirectionLocation,
|
239
298
|
getStylesAndLocation,
|
299
|
+
parsePlacement,
|
240
300
|
triggerEventTransform
|
241
301
|
});
|
@@ -1,3 +1,3 @@
|
|
1
1
|
import type { TaroElement } from '@tarojs/runtime';
|
2
|
-
declare const getRootElement: (rootEle?: TaroElement | (() => TaroElement)) =>
|
2
|
+
declare const getRootElement: (rootEle?: TaroElement | (() => TaroElement)) => HTMLElement | TaroElement;
|
3
3
|
export default getRootElement;
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
export { default as debounce } from './debounce';
|
2
|
-
export { getStylesAndLocation, triggerEventTransform, } from './directionLocationUtil';
|
2
|
+
export { getStylesAndLocation, triggerEventTransform, parsePlacement, } from './directionLocationUtil';
|
3
3
|
export { default as convertHexToRGBA } from './hex2rgba';
|
4
4
|
export { useDidMountEffect, useEventCallback, useForkRef, useTouchEmulator, useValue, useDomReady, useSize, useDomCss, useTouch, useUniqueId, } from './hooks';
|
5
5
|
export { default as isDev } from './isDev';
|
package/dist/index.js
CHANGED
@@ -43,6 +43,7 @@ __export(src_exports, {
|
|
43
43
|
isMini: () => import_isMini.isMini,
|
44
44
|
isMiniapp: () => import_isMini.isMiniapp,
|
45
45
|
isWeapp: () => import_isMini.isWeapp,
|
46
|
+
parsePlacement: () => import_directionLocationUtil.parsePlacement,
|
46
47
|
setRef: () => import_setRef.default,
|
47
48
|
throttle: () => import_throttle.default,
|
48
49
|
toArray: () => import_toArray.default,
|
@@ -91,6 +92,7 @@ __reExport(src_exports, require("./render"), module.exports);
|
|
91
92
|
isMini,
|
92
93
|
isMiniapp,
|
93
94
|
isWeapp,
|
95
|
+
parsePlacement,
|
94
96
|
setRef,
|
95
97
|
throttle,
|
96
98
|
toArray,
|
@@ -1,11 +1,14 @@
|
|
1
1
|
/**
|
2
2
|
* 根据元素宽高判断是否超出边界,超出边界则重新定义方向
|
3
3
|
*/
|
4
|
-
export declare const getNewDirectionLocation: ({
|
5
|
-
|
4
|
+
export declare const getNewDirectionLocation: ({ scrollRoot, scrollRootOffset, childrenOffset, arrowDirection, tipOffset, arrowLocation, offsetSpacing, }: {
|
5
|
+
scrollRoot: any;
|
6
|
+
scrollRootOffset: any;
|
7
|
+
childrenOffset: any;
|
6
8
|
arrowDirection: any;
|
7
9
|
tipOffset: any;
|
8
10
|
arrowLocation: any;
|
11
|
+
offsetSpacing: any;
|
9
12
|
}) => {
|
10
13
|
newArrowDirection: any;
|
11
14
|
newArrowLocation: any;
|
@@ -13,22 +16,29 @@ export declare const getNewDirectionLocation: ({ rootOffset, arrowDirection, tip
|
|
13
16
|
/**
|
14
17
|
* 根据新的气泡位置和箭头位置 计算气泡位置以及箭头位置
|
15
18
|
*/
|
16
|
-
export declare const getDirectionLocationStyle: ({
|
17
|
-
|
19
|
+
export declare const getDirectionLocationStyle: ({ childrenOffset, arrowDirection, tipOffset, arrowLocation, offsetSpacing, }: {
|
20
|
+
childrenOffset: any;
|
18
21
|
arrowDirection: any;
|
19
22
|
tipOffset: any;
|
20
23
|
arrowLocation: any;
|
21
|
-
|
24
|
+
offsetSpacing: any;
|
25
|
+
}) => {
|
26
|
+
styles: any;
|
27
|
+
childrenStyle: any;
|
28
|
+
};
|
22
29
|
/**
|
23
30
|
* 获取气泡位置和箭头位置
|
24
31
|
*/
|
25
|
-
export declare const getStylesAndLocation: ({ childrenRef, arrowDirection, arrowLocation, selector, }: {
|
32
|
+
export declare const getStylesAndLocation: ({ scrollRoot, childrenRef, arrowDirection, arrowLocation, offsetSpacing, selector, }: {
|
33
|
+
scrollRoot?: Element;
|
26
34
|
childrenRef: any;
|
27
35
|
arrowDirection: any;
|
28
36
|
arrowLocation: any;
|
37
|
+
offsetSpacing: any;
|
29
38
|
selector: any;
|
30
39
|
}) => {
|
31
40
|
styles: any;
|
41
|
+
childrenStyle: any;
|
32
42
|
newArrowDirection: any;
|
33
43
|
newArrowLocation: any;
|
34
44
|
};
|
@@ -38,3 +48,12 @@ export declare const triggerEventTransform: ({ trigger, click, show, hide }: {
|
|
38
48
|
show: any;
|
39
49
|
hide: any;
|
40
50
|
}) => {};
|
51
|
+
/**
|
52
|
+
* for example: placement = 'topLeft', return { direction: 'top', location: 'left' }
|
53
|
+
* @param placement
|
54
|
+
* @returns
|
55
|
+
*/
|
56
|
+
export declare const parsePlacement: (placement: any) => {
|
57
|
+
direction: any;
|
58
|
+
location: any;
|
59
|
+
};
|
@@ -4,55 +4,82 @@ const directionCssMap = {
|
|
4
4
|
top: "bottom",
|
5
5
|
bottom: "top"
|
6
6
|
};
|
7
|
+
const isBodyScroll = (scrollRoot) => {
|
8
|
+
return scrollRoot === document.body;
|
9
|
+
};
|
7
10
|
const getNewDirectionLocation = ({
|
8
|
-
|
11
|
+
scrollRoot,
|
12
|
+
scrollRootOffset,
|
13
|
+
childrenOffset,
|
9
14
|
arrowDirection,
|
10
15
|
tipOffset,
|
11
|
-
arrowLocation
|
16
|
+
arrowLocation,
|
17
|
+
offsetSpacing
|
12
18
|
}) => {
|
13
|
-
const {
|
19
|
+
const {
|
20
|
+
left: cLeft,
|
21
|
+
right: cRight,
|
22
|
+
top: cTop,
|
23
|
+
bottom: cBottom,
|
24
|
+
width: cWidth,
|
25
|
+
height: cHeight
|
26
|
+
} = childrenOffset;
|
14
27
|
const { width, height } = tipOffset;
|
15
|
-
const
|
16
|
-
|
28
|
+
const {
|
29
|
+
top: sTop,
|
30
|
+
bottom: sBottom,
|
31
|
+
left: sLeft,
|
32
|
+
right: sRight
|
33
|
+
} = scrollRootOffset;
|
34
|
+
const pageWidth = document.documentElement.clientWidth || document.body.clientWidth;
|
35
|
+
const pageHeight = document.documentElement.clientHeight || document.body.clientHeight;
|
36
|
+
const maxTop = isBodyScroll(scrollRoot) ? 0 : sTop;
|
37
|
+
const maxBottom = isBodyScroll(scrollRoot) ? pageHeight : sBottom;
|
38
|
+
const maxLeft = isBodyScroll(scrollRoot) ? 0 : sLeft;
|
39
|
+
const maxRight = isBodyScroll(scrollRoot) ? pageWidth : sRight;
|
17
40
|
let newArrowDirection = arrowDirection;
|
18
41
|
let newArrowLocation = arrowLocation;
|
19
42
|
const isDirectionTop = arrowDirection === "top";
|
20
43
|
const isDirectionBottom = arrowDirection === "bottom";
|
21
44
|
const isDirectionLeft = arrowDirection === "left";
|
22
45
|
const isDirectionRight = arrowDirection === "right";
|
23
|
-
if (isDirectionTop && cTop - height <
|
46
|
+
if (isDirectionTop && cTop - height - offsetSpacing < maxTop || isDirectionBottom && cBottom + height + offsetSpacing > maxBottom || isDirectionLeft && cLeft - width - offsetSpacing < maxLeft || isDirectionRight && cRight + width + offsetSpacing > maxRight) {
|
24
47
|
newArrowDirection = directionCssMap[arrowDirection];
|
25
48
|
}
|
26
|
-
if (arrowLocation === "top" && cTop + height >
|
49
|
+
if (arrowLocation === "top" && cTop + height > maxBottom || arrowLocation === "bottom" && cBottom - height < maxTop || arrowLocation === "left" && cLeft + width > maxRight || arrowLocation === "right" && cRight - width < maxLeft) {
|
27
50
|
newArrowLocation = directionCssMap[arrowLocation];
|
28
51
|
}
|
29
52
|
const isCenter = arrowLocation === "center";
|
30
53
|
if (isCenter && (isDirectionTop || isDirectionBottom)) {
|
31
|
-
if (cLeft + width >
|
54
|
+
if (cLeft + (cWidth - width) / 2 + width > maxRight) {
|
32
55
|
newArrowLocation = directionCssMap.left;
|
33
|
-
} else if (
|
56
|
+
} else if (cLeft + (cWidth - width) / 2 < maxLeft) {
|
34
57
|
newArrowLocation = directionCssMap.right;
|
35
58
|
}
|
36
59
|
} else if (isCenter && (isDirectionLeft || isDirectionRight)) {
|
37
|
-
if (cTop + height >
|
60
|
+
if (cTop + (cHeight - height) / 2 + cHeight > maxBottom) {
|
38
61
|
newArrowLocation = directionCssMap.top;
|
39
|
-
} else if (
|
62
|
+
} else if (cTop + (cHeight - height) / 2 < maxTop) {
|
40
63
|
newArrowLocation = directionCssMap.bottom;
|
41
64
|
}
|
42
65
|
}
|
66
|
+
if (arrowLocation === "none") {
|
67
|
+
newArrowLocation = "none";
|
68
|
+
}
|
43
69
|
return {
|
44
70
|
newArrowDirection,
|
45
71
|
newArrowLocation
|
46
72
|
};
|
47
73
|
};
|
48
74
|
const getDirectionLocationStyle = ({
|
49
|
-
|
75
|
+
childrenOffset,
|
50
76
|
arrowDirection,
|
51
77
|
tipOffset,
|
52
|
-
arrowLocation
|
78
|
+
arrowLocation,
|
79
|
+
offsetSpacing
|
53
80
|
}) => {
|
54
81
|
const scrollTop = window.scrollY >= 0 && window.scrollY || document.documentElement.scrollTop;
|
55
|
-
const scrollLeft = window.
|
82
|
+
const scrollLeft = window.scrollX >= 0 && window.scrollX || document.documentElement.scrollLeft;
|
56
83
|
const styles = {};
|
57
84
|
const {
|
58
85
|
width: cWidth,
|
@@ -61,11 +88,14 @@ const getDirectionLocationStyle = ({
|
|
61
88
|
right: cRight,
|
62
89
|
top: cTop,
|
63
90
|
bottom: cBottom
|
64
|
-
} =
|
91
|
+
} = childrenOffset;
|
92
|
+
let childrenStyle = {};
|
93
|
+
if (cWidth && cHeight) {
|
94
|
+
childrenStyle = { width: `${cWidth}px`, height: `${cHeight}px` };
|
95
|
+
}
|
65
96
|
const { width, height } = tipOffset;
|
66
97
|
if (arrowDirection === "top") {
|
67
|
-
styles.top = cTop;
|
68
|
-
styles.transform = `translateY(-100%)`;
|
98
|
+
styles.top = cTop - offsetSpacing - height;
|
69
99
|
switch (arrowLocation) {
|
70
100
|
case "left":
|
71
101
|
styles.left = cLeft;
|
@@ -74,14 +104,16 @@ const getDirectionLocationStyle = ({
|
|
74
104
|
styles.left = cLeft + (cWidth - width) / 2;
|
75
105
|
break;
|
76
106
|
case "right":
|
77
|
-
styles.left = cRight;
|
78
|
-
|
107
|
+
styles.left = cRight - width;
|
108
|
+
break;
|
109
|
+
case "none":
|
110
|
+
styles.left = cLeft;
|
79
111
|
break;
|
80
112
|
default:
|
81
113
|
break;
|
82
114
|
}
|
83
115
|
} else if (arrowDirection === "bottom") {
|
84
|
-
styles.top = cBottom;
|
116
|
+
styles.top = cBottom + offsetSpacing;
|
85
117
|
switch (arrowLocation) {
|
86
118
|
case "left":
|
87
119
|
styles.left = cLeft;
|
@@ -90,15 +122,16 @@ const getDirectionLocationStyle = ({
|
|
90
122
|
styles.left = cLeft + (cWidth - width) / 2;
|
91
123
|
break;
|
92
124
|
case "right":
|
93
|
-
styles.left = cRight;
|
94
|
-
|
125
|
+
styles.left = cRight - width;
|
126
|
+
break;
|
127
|
+
case "none":
|
128
|
+
styles.left = cLeft;
|
95
129
|
break;
|
96
130
|
default:
|
97
131
|
break;
|
98
132
|
}
|
99
133
|
} else if (arrowDirection === "left") {
|
100
|
-
styles.left = cLeft;
|
101
|
-
styles.transform = `translateX(-100%)`;
|
134
|
+
styles.left = cLeft - offsetSpacing - width;
|
102
135
|
switch (arrowLocation) {
|
103
136
|
case "top":
|
104
137
|
styles.top = cTop;
|
@@ -107,14 +140,16 @@ const getDirectionLocationStyle = ({
|
|
107
140
|
styles.top = cTop + (cHeight - height) / 2;
|
108
141
|
break;
|
109
142
|
case "bottom":
|
110
|
-
styles.top = cBottom;
|
111
|
-
|
143
|
+
styles.top = cBottom - height;
|
144
|
+
break;
|
145
|
+
case "none":
|
146
|
+
styles.top = cTop;
|
112
147
|
break;
|
113
148
|
default:
|
114
149
|
break;
|
115
150
|
}
|
116
151
|
} else if (arrowDirection === "right") {
|
117
|
-
styles.left = cRight;
|
152
|
+
styles.left = cRight + offsetSpacing;
|
118
153
|
switch (arrowLocation) {
|
119
154
|
case "top":
|
120
155
|
styles.top = cTop;
|
@@ -123,8 +158,10 @@ const getDirectionLocationStyle = ({
|
|
123
158
|
styles.top = cTop + (cHeight - height) / 2;
|
124
159
|
break;
|
125
160
|
case "bottom":
|
126
|
-
styles.top = cBottom;
|
127
|
-
|
161
|
+
styles.top = cBottom - height;
|
162
|
+
break;
|
163
|
+
case "none":
|
164
|
+
styles.top = cTop;
|
128
165
|
break;
|
129
166
|
default:
|
130
167
|
break;
|
@@ -136,12 +173,14 @@ const getDirectionLocationStyle = ({
|
|
136
173
|
if (styles.left) {
|
137
174
|
styles.left = `${styles.left + scrollLeft}px`;
|
138
175
|
}
|
139
|
-
return styles;
|
176
|
+
return { styles, childrenStyle };
|
140
177
|
};
|
141
178
|
const getStylesAndLocation = ({
|
179
|
+
scrollRoot = document.body,
|
142
180
|
childrenRef,
|
143
181
|
arrowDirection,
|
144
182
|
arrowLocation,
|
183
|
+
offsetSpacing,
|
145
184
|
selector
|
146
185
|
}) => {
|
147
186
|
if (!(childrenRef == null ? void 0 : childrenRef.current)) {
|
@@ -150,26 +189,32 @@ const getStylesAndLocation = ({
|
|
150
189
|
);
|
151
190
|
return null;
|
152
191
|
}
|
153
|
-
const
|
192
|
+
const childrenOffset = childrenRef.current.getBoundingClientRect();
|
154
193
|
const $rtDom = document.querySelector(selector);
|
155
194
|
if (!$rtDom)
|
156
195
|
return null;
|
157
196
|
const tipOffset = $rtDom.getBoundingClientRect();
|
197
|
+
const scrollRootOffset = scrollRoot.getBoundingClientRect();
|
158
198
|
const { newArrowDirection, newArrowLocation } = getNewDirectionLocation({
|
159
|
-
|
199
|
+
scrollRoot,
|
200
|
+
scrollRootOffset,
|
201
|
+
childrenOffset,
|
160
202
|
arrowDirection,
|
161
203
|
tipOffset,
|
162
|
-
arrowLocation
|
204
|
+
arrowLocation,
|
205
|
+
offsetSpacing
|
163
206
|
});
|
164
|
-
const styles = getDirectionLocationStyle({
|
165
|
-
|
207
|
+
const { styles, childrenStyle } = getDirectionLocationStyle({
|
208
|
+
childrenOffset,
|
166
209
|
arrowDirection: newArrowDirection,
|
167
210
|
tipOffset,
|
168
|
-
arrowLocation: newArrowLocation
|
211
|
+
arrowLocation: newArrowLocation,
|
212
|
+
offsetSpacing
|
169
213
|
});
|
170
214
|
styles.visibility = "visible";
|
171
215
|
return {
|
172
216
|
styles,
|
217
|
+
childrenStyle,
|
173
218
|
newArrowDirection,
|
174
219
|
newArrowLocation
|
175
220
|
};
|
@@ -207,9 +252,23 @@ const triggerEventTransform = ({ trigger, click, show, hide }) => {
|
|
207
252
|
});
|
208
253
|
return option;
|
209
254
|
};
|
255
|
+
const parsePlacement = (placement) => {
|
256
|
+
const positionArr = placement.split(/([A-Z])/);
|
257
|
+
const direction = positionArr[0];
|
258
|
+
let location;
|
259
|
+
if (positionArr.length > 1) {
|
260
|
+
positionArr.splice(0, 1);
|
261
|
+
location = positionArr.join("").toLowerCase();
|
262
|
+
}
|
263
|
+
return {
|
264
|
+
direction,
|
265
|
+
location
|
266
|
+
};
|
267
|
+
};
|
210
268
|
export {
|
211
269
|
getDirectionLocationStyle,
|
212
270
|
getNewDirectionLocation,
|
213
271
|
getStylesAndLocation,
|
272
|
+
parsePlacement,
|
214
273
|
triggerEventTransform
|
215
274
|
};
|
package/es/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
export { default as debounce } from './debounce';
|
2
|
-
export { getStylesAndLocation, triggerEventTransform, } from './directionLocationUtil';
|
2
|
+
export { getStylesAndLocation, triggerEventTransform, parsePlacement, } from './directionLocationUtil';
|
3
3
|
export { default as convertHexToRGBA } from './hex2rgba';
|
4
4
|
export { useDidMountEffect, useEventCallback, useForkRef, useTouchEmulator, useValue, useDomReady, useSize, useDomCss, useTouch, useUniqueId, } from './hooks';
|
5
5
|
export { default as isDev } from './isDev';
|
package/es/index.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
import { default as default2 } from "./debounce";
|
2
2
|
import {
|
3
3
|
getStylesAndLocation,
|
4
|
-
triggerEventTransform
|
4
|
+
triggerEventTransform,
|
5
|
+
parsePlacement
|
5
6
|
} from "./directionLocationUtil";
|
6
7
|
import { default as default3 } from "./hex2rgba";
|
7
8
|
import {
|
@@ -48,6 +49,7 @@ export {
|
|
48
49
|
isMini,
|
49
50
|
isMiniapp,
|
50
51
|
isWeapp,
|
52
|
+
parsePlacement,
|
51
53
|
default5 as setRef,
|
52
54
|
default6 as throttle,
|
53
55
|
default7 as toArray,
|