@sheinx/hooks 3.10.4-beta.6 → 3.10.4-beta.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"use-popup.d.ts","sourceRoot":"","sources":["use-popup.ts"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOhE,QAAA,MAAM,QAAQ,UAAW,cAAc;;;;;;0BAwHK;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;0BAa9B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;qBAnC7B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAzEzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BA+KxD,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;6BAMxC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;;CA8BlE,CAAC;AAEF,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"use-popup.d.ts","sourceRoot":"","sources":["use-popup.ts"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOhE,QAAA,MAAM,QAAQ,UAAW,cAAc;;;;;;0BAyHK;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;0BAa9B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;qBAnC7B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;2BA1EzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BAgLxD,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;6BAMxC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;;CA8BlE,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -83,7 +83,7 @@ var usePopup = function usePopup(props) {
83
83
  var updatePosition = (0, _usePersistFn.default)(function () {
84
84
  // if (isPositionControl) return;
85
85
  // if (props.position === 'auto' || !props.position) {
86
- var newPosition = (0, _position.getPosition)(targetRef.current, props.priorityDirection, autoMode, popupRef.current || undefined, typeof props.container === 'function' ? props.container() : props.container);
86
+ var newPosition = (0, _position.getPosition)(targetRef.current, props.priorityDirection, autoMode, popupRef.current || undefined, typeof props.container === 'function' ? props.container() : props.container, props.positionPriority);
87
87
  if (newPosition !== position) setPositionState(newPosition);
88
88
  });
89
89
  (0, _react.useEffect)(function () {
@@ -25,6 +25,11 @@ export interface BasePopupProps {
25
25
  * @default 'vertical'
26
26
  */
27
27
  priorityDirection?: 'vertical' | 'horizontal' | 'auto';
28
+ /**
29
+ * @en Direction priority used when position is auto. The first direction with enough space wins; if none fits, the one with the most space is used. Takes precedence over `priorityDirection`
30
+ * @cn position 为 auto 时的方向优先级。按顺序选择第一个空间足够的方向,都放不下则选择空间最大的方向。优先于 `priorityDirection`
31
+ */
32
+ positionPriority?: ('top' | 'right' | 'bottom' | 'left')[];
28
33
  /**
29
34
  * @en The container where the popup is rendered. When set, the auto position is calculated within the container's visible area instead of the viewport
30
35
  * @cn 弹出层渲染的容器。设置后,自动位置按容器可见区域计算,而不是按视口计算
@@ -1 +1 @@
1
- {"version":3,"file":"use-popup.type.d.ts","sourceRoot":"","sources":["use-popup.type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,aAAa,GACb,WAAW,GACX,cA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WAAW,GAAG,IAAI,CAAA;SAAE,KAAK,IAAI,CAAC;KAC5D,CAAC;CACH"}
@@ -1,2 +1,3 @@
1
- export declare const getPosition: (target: HTMLElement | null, priorityDirection: "auto" | "vertical" | "horizontal" | undefined, mode: 'popover' | 'menu' | 'list', popup?: HTMLElement, container?: HTMLElement | null) => "bottom" | "left" | "right" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "left-top" | "left-bottom" | "right-top" | "right-bottom";
1
+ export type PositionDirection = 'top' | 'right' | 'bottom' | 'left';
2
+ export declare const getPosition: (target: HTMLElement | null, priorityDirection: "auto" | "vertical" | "horizontal" | undefined, mode: 'popover' | 'menu' | 'list', popup?: HTMLElement, container?: HTMLElement | null, positionPriority?: PositionDirection[]) => "bottom" | "left" | "right" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "left-top" | "left-bottom" | "right-top" | "right-bottom";
2
3
  //# sourceMappingURL=position.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["position.ts"],"names":[],"mappings":"AAqHA,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,cACP,WAAW,GAAG,IAAI,gKAK/B,CAAC"}
1
+ {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["position.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC;AA4MpE,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,cACP,WAAW,GAAG,IAAI,qBACX,iBAAiB,EAAE,gKAOvC,CAAC"}
@@ -60,10 +60,94 @@ var getBounds = function getBounds(container) {
60
60
  if (bounds.right <= bounds.left || bounds.bottom <= bounds.top) return viewport;
61
61
  return bounds;
62
62
  };
63
+
64
+ // 测量弹出层的自然尺寸(弹出层未显示时 display 为 none,需要克隆后测量)
65
+ var measurePopup = function measurePopup(popup) {
66
+ var parent = popup === null || popup === void 0 ? void 0 : popup.parentElement;
67
+ if (!popup || !parent) return {
68
+ width: 0,
69
+ height: 0
70
+ };
71
+ var el = popup.cloneNode(true);
72
+ el.style.visibility = 'hidden';
73
+ el.style.opacity = '0';
74
+ el.style.pointerEvents = 'none';
75
+ // 关闭状态下 CSS 类本身带有 display: none,必须内联覆盖为 block 才能量到真实尺寸
76
+ el.style.display = 'block';
77
+ // 清除上一次定位残留的样式,避免影响 shrink-to-fit 宽度
78
+ el.style.left = '0';
79
+ el.style.top = '0';
80
+ el.style.right = 'auto';
81
+ el.style.bottom = 'auto';
82
+ el.style.transform = 'none';
83
+ parent.appendChild(el);
84
+ var size = {
85
+ width: el.offsetWidth,
86
+ height: el.offsetHeight
87
+ };
88
+ el.remove();
89
+ return size;
90
+ };
91
+ // 指定方向后,沿另一条轴的对齐方式:与弹出层最终使用的 position 后缀保持一致
92
+ var getAlignSuffix = function getAlignSuffix(direction, rect, bounds) {
93
+ var width = bounds.right - bounds.left;
94
+ var height = bounds.bottom - bounds.top;
95
+ if (direction === 'top' || direction === 'bottom') {
96
+ var _point = rect.left + rect.width / 2 - bounds.left;
97
+ if (_point > width * 0.6) return '-right';
98
+ if (_point < width * 0.4) return '-left';
99
+ return '';
100
+ }
101
+ var point = rect.top + rect.height / 2 - bounds.top;
102
+ if (point > height * 0.6) return '-bottom';
103
+ if (point < height * 0.4) return '-top';
104
+ return '';
105
+ };
106
+
107
+ // 判断弹出层沿「另一条轴」排开后是否会超出可用区域
108
+ var fitsCrossAxis = function fitsCrossAxis(direction, rect, bounds, size) {
109
+ var suffix = getAlignSuffix(direction, rect, bounds);
110
+ var horizontal = direction === 'top' || direction === 'bottom';
111
+ var start = horizontal ? rect.left : rect.top;
112
+ var end = horizontal ? rect.right : rect.bottom;
113
+ var length = horizontal ? size.width : size.height;
114
+ var min = horizontal ? bounds.left : bounds.top;
115
+ var max = horizontal ? bounds.right : bounds.bottom;
116
+ var from;
117
+ if (suffix === '-right' || suffix === '-bottom') from = end - length;else if (suffix === '-left' || suffix === '-top') from = start;else from = (start + end) / 2 - length / 2;
118
+ return from >= min && from + length <= max;
119
+ };
120
+
121
+ // 按优先级选择第一个「空间足够放下弹出层」的方向(主轴放得下,且沿另一条轴排开后不超出可用区域);
122
+ // 都放不下时,选择主轴空间最大的方向(空间相同则靠前的优先)
123
+ var pickDirection = function pickDirection(rect, bounds, priority, popup) {
124
+ var space = {
125
+ top: rect.top - bounds.top,
126
+ bottom: bounds.bottom - rect.bottom,
127
+ left: rect.left - bounds.left,
128
+ right: bounds.right - rect.right
129
+ };
130
+ var list = priority.filter(function (d) {
131
+ return d in space;
132
+ });
133
+ if (!list.length) return undefined;
134
+ var size = measurePopup(popup);
135
+ var need = function need(d) {
136
+ return d === 'top' || d === 'bottom' ? size.height : size.width;
137
+ };
138
+ var fit = list.find(function (d) {
139
+ return space[d] > 0 && space[d] >= need(d) && fitsCrossAxis(d, rect, bounds, size);
140
+ });
141
+ if (fit) return fit;
142
+ return list.reduce(function (best, d) {
143
+ return space[d] > space[best] ? d : best;
144
+ }, list[0]);
145
+ };
63
146
  var getPopoverPosition = function getPopoverPosition(target) {
64
147
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
65
148
  var popup = arguments.length > 2 ? arguments[2] : undefined;
66
149
  var container = arguments.length > 3 ? arguments[3] : undefined;
150
+ var positionPriority = arguments.length > 4 ? arguments[4] : undefined;
67
151
  var position = 'bottom-left';
68
152
  if (!target) return position;
69
153
  var rect = target.getBoundingClientRect();
@@ -74,6 +158,11 @@ var getPopoverPosition = function getPopoverPosition(target) {
74
158
  var verticalPoint = rect.top + rect.height / 2 - bounds.top;
75
159
  var windowHeight = bounds.bottom - bounds.top;
76
160
  var windowWidth = bounds.right - bounds.left;
161
+ // 指定了 positionPriority 时,按方向优先级选择,优先于 priorityDirection
162
+ var direction = positionPriority !== null && positionPriority !== void 0 && positionPriority.length ? pickDirection(rect, bounds, positionPriority, popup) : undefined;
163
+ if (direction) {
164
+ return direction + getAlignSuffix(direction, rect, bounds);
165
+ }
77
166
  if (priorityDirection === 'auto') {
78
167
  var maxX = Math.max(rect.left - bounds.left, bounds.right - rect.right);
79
168
  var maxY = Math.max(rect.top - bounds.top, bounds.bottom - rect.bottom);
@@ -115,7 +204,10 @@ var getPosition = exports.getPosition = function getPosition(target) {
115
204
  var mode = arguments.length > 2 ? arguments[2] : undefined;
116
205
  var popup = arguments.length > 3 ? arguments[3] : undefined;
117
206
  var container = arguments.length > 4 ? arguments[4] : undefined;
118
- if (mode === 'popover') return getPopoverPosition(target, priorityDirection, popup, container);
207
+ var positionPriority = arguments.length > 5 ? arguments[5] : undefined;
208
+ if (mode === 'popover') {
209
+ return getPopoverPosition(target, priorityDirection, popup, container, positionPriority);
210
+ }
119
211
  if (mode === 'menu') return getMenuPosition(target, priorityDirection);
120
212
  return 'bottom-left';
121
213
  };
@@ -1 +1 @@
1
- {"version":3,"file":"use-popup.d.ts","sourceRoot":"","sources":["use-popup.ts"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOhE,QAAA,MAAM,QAAQ,UAAW,cAAc;;;;;;0BAwHK;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;0BAa9B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;qBAnC7B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAzEzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BA+KxD,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;6BAMxC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;;CA8BlE,CAAC;AAEF,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"use-popup.d.ts","sourceRoot":"","sources":["use-popup.ts"],"names":[],"mappings":"AAAA,OAAO,KAA4E,MAAM,OAAO,CAAC;AACjG,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOhE,QAAA,MAAM,QAAQ,UAAW,cAAc;;;;;;0BAyHK;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;0BAa9B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;qBAnC7B;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;2BA1EzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BAgLxD,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;6BAMxC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;;CA8BlE,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -76,7 +76,7 @@ var usePopup = function usePopup(props) {
76
76
  var updatePosition = usePersistFn(function () {
77
77
  // if (isPositionControl) return;
78
78
  // if (props.position === 'auto' || !props.position) {
79
- var newPosition = getPosition(targetRef.current, props.priorityDirection, autoMode, popupRef.current || undefined, typeof props.container === 'function' ? props.container() : props.container);
79
+ var newPosition = getPosition(targetRef.current, props.priorityDirection, autoMode, popupRef.current || undefined, typeof props.container === 'function' ? props.container() : props.container, props.positionPriority);
80
80
  if (newPosition !== position) setPositionState(newPosition);
81
81
  });
82
82
  useEffect(function () {
@@ -25,6 +25,11 @@ export interface BasePopupProps {
25
25
  * @default 'vertical'
26
26
  */
27
27
  priorityDirection?: 'vertical' | 'horizontal' | 'auto';
28
+ /**
29
+ * @en Direction priority used when position is auto. The first direction with enough space wins; if none fits, the one with the most space is used. Takes precedence over `priorityDirection`
30
+ * @cn position 为 auto 时的方向优先级。按顺序选择第一个空间足够的方向,都放不下则选择空间最大的方向。优先于 `priorityDirection`
31
+ */
32
+ positionPriority?: ('top' | 'right' | 'bottom' | 'left')[];
28
33
  /**
29
34
  * @en The container where the popup is rendered. When set, the auto position is calculated within the container's visible area instead of the viewport
30
35
  * @cn 弹出层渲染的容器。设置后,自动位置按容器可见区域计算,而不是按视口计算
@@ -1 +1 @@
1
- {"version":3,"file":"use-popup.type.d.ts","sourceRoot":"","sources":["use-popup.type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,aAAa,GACb,WAAW,GACX,cAAc,GACd,WAAW,GACX,UAAU,GACV,cAAc,GACd,aAAa,GACb,MAAM,GACN,OAAO,GACP,KAAK,GACL,QAAQ,CAAC;AAQb,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC;IACtC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;IACd;;;;OAIG;IACH,QAAQ,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC;IACjC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,UAAU,GAAG,YAAY,GAAG,MAAM,CAAC;IACvD;;;OAGG;IACH,SAAS,CAAC,EAAE,WAAW,GAAG,IAAI,GAAG,CAAC,MAAM,WAAW,GAAG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AAE,CAAC,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC,EAAE,CAAC;IAC3D;;;OAGG;IACH,SAAS,CAAC,EAAE,WAAW,GAAG,IAAI,GAAG,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,CAAC;IAC5D,YAAY,CAAC,EAAE;QACb,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE,KAAK,IAAI,CAAC;QACtD,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE,KAAK,IAAI,CAAC;QAC3D,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE;YAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAAA;SAAE,KAAK,IAAI,CAAC;KAC5D,CAAC;CACH"}
@@ -1,2 +1,3 @@
1
- export declare const getPosition: (target: HTMLElement | null, priorityDirection: "auto" | "vertical" | "horizontal" | undefined, mode: 'popover' | 'menu' | 'list', popup?: HTMLElement, container?: HTMLElement | null) => "bottom" | "left" | "right" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "left-top" | "left-bottom" | "right-top" | "right-bottom";
1
+ export type PositionDirection = 'top' | 'right' | 'bottom' | 'left';
2
+ export declare const getPosition: (target: HTMLElement | null, priorityDirection: "auto" | "vertical" | "horizontal" | undefined, mode: 'popover' | 'menu' | 'list', popup?: HTMLElement, container?: HTMLElement | null, positionPriority?: PositionDirection[]) => "bottom" | "left" | "right" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "left-top" | "left-bottom" | "right-top" | "right-bottom";
2
3
  //# sourceMappingURL=position.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["position.ts"],"names":[],"mappings":"AAqHA,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,cACP,WAAW,GAAG,IAAI,gKAK/B,CAAC"}
1
+ {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["position.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC;AA4MpE,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,cACP,WAAW,GAAG,IAAI,qBACX,iBAAiB,EAAE,gKAOvC,CAAC"}
@@ -54,10 +54,94 @@ var getBounds = function getBounds(container) {
54
54
  if (bounds.right <= bounds.left || bounds.bottom <= bounds.top) return viewport;
55
55
  return bounds;
56
56
  };
57
+
58
+ // 测量弹出层的自然尺寸(弹出层未显示时 display 为 none,需要克隆后测量)
59
+ var measurePopup = function measurePopup(popup) {
60
+ var parent = popup === null || popup === void 0 ? void 0 : popup.parentElement;
61
+ if (!popup || !parent) return {
62
+ width: 0,
63
+ height: 0
64
+ };
65
+ var el = popup.cloneNode(true);
66
+ el.style.visibility = 'hidden';
67
+ el.style.opacity = '0';
68
+ el.style.pointerEvents = 'none';
69
+ // 关闭状态下 CSS 类本身带有 display: none,必须内联覆盖为 block 才能量到真实尺寸
70
+ el.style.display = 'block';
71
+ // 清除上一次定位残留的样式,避免影响 shrink-to-fit 宽度
72
+ el.style.left = '0';
73
+ el.style.top = '0';
74
+ el.style.right = 'auto';
75
+ el.style.bottom = 'auto';
76
+ el.style.transform = 'none';
77
+ parent.appendChild(el);
78
+ var size = {
79
+ width: el.offsetWidth,
80
+ height: el.offsetHeight
81
+ };
82
+ el.remove();
83
+ return size;
84
+ };
85
+ // 指定方向后,沿另一条轴的对齐方式:与弹出层最终使用的 position 后缀保持一致
86
+ var getAlignSuffix = function getAlignSuffix(direction, rect, bounds) {
87
+ var width = bounds.right - bounds.left;
88
+ var height = bounds.bottom - bounds.top;
89
+ if (direction === 'top' || direction === 'bottom') {
90
+ var _point = rect.left + rect.width / 2 - bounds.left;
91
+ if (_point > width * 0.6) return '-right';
92
+ if (_point < width * 0.4) return '-left';
93
+ return '';
94
+ }
95
+ var point = rect.top + rect.height / 2 - bounds.top;
96
+ if (point > height * 0.6) return '-bottom';
97
+ if (point < height * 0.4) return '-top';
98
+ return '';
99
+ };
100
+
101
+ // 判断弹出层沿「另一条轴」排开后是否会超出可用区域
102
+ var fitsCrossAxis = function fitsCrossAxis(direction, rect, bounds, size) {
103
+ var suffix = getAlignSuffix(direction, rect, bounds);
104
+ var horizontal = direction === 'top' || direction === 'bottom';
105
+ var start = horizontal ? rect.left : rect.top;
106
+ var end = horizontal ? rect.right : rect.bottom;
107
+ var length = horizontal ? size.width : size.height;
108
+ var min = horizontal ? bounds.left : bounds.top;
109
+ var max = horizontal ? bounds.right : bounds.bottom;
110
+ var from;
111
+ if (suffix === '-right' || suffix === '-bottom') from = end - length;else if (suffix === '-left' || suffix === '-top') from = start;else from = (start + end) / 2 - length / 2;
112
+ return from >= min && from + length <= max;
113
+ };
114
+
115
+ // 按优先级选择第一个「空间足够放下弹出层」的方向(主轴放得下,且沿另一条轴排开后不超出可用区域);
116
+ // 都放不下时,选择主轴空间最大的方向(空间相同则靠前的优先)
117
+ var pickDirection = function pickDirection(rect, bounds, priority, popup) {
118
+ var space = {
119
+ top: rect.top - bounds.top,
120
+ bottom: bounds.bottom - rect.bottom,
121
+ left: rect.left - bounds.left,
122
+ right: bounds.right - rect.right
123
+ };
124
+ var list = priority.filter(function (d) {
125
+ return d in space;
126
+ });
127
+ if (!list.length) return undefined;
128
+ var size = measurePopup(popup);
129
+ var need = function need(d) {
130
+ return d === 'top' || d === 'bottom' ? size.height : size.width;
131
+ };
132
+ var fit = list.find(function (d) {
133
+ return space[d] > 0 && space[d] >= need(d) && fitsCrossAxis(d, rect, bounds, size);
134
+ });
135
+ if (fit) return fit;
136
+ return list.reduce(function (best, d) {
137
+ return space[d] > space[best] ? d : best;
138
+ }, list[0]);
139
+ };
57
140
  var getPopoverPosition = function getPopoverPosition(target) {
58
141
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
59
142
  var popup = arguments.length > 2 ? arguments[2] : undefined;
60
143
  var container = arguments.length > 3 ? arguments[3] : undefined;
144
+ var positionPriority = arguments.length > 4 ? arguments[4] : undefined;
61
145
  var position = 'bottom-left';
62
146
  if (!target) return position;
63
147
  var rect = target.getBoundingClientRect();
@@ -68,6 +152,11 @@ var getPopoverPosition = function getPopoverPosition(target) {
68
152
  var verticalPoint = rect.top + rect.height / 2 - bounds.top;
69
153
  var windowHeight = bounds.bottom - bounds.top;
70
154
  var windowWidth = bounds.right - bounds.left;
155
+ // 指定了 positionPriority 时,按方向优先级选择,优先于 priorityDirection
156
+ var direction = positionPriority !== null && positionPriority !== void 0 && positionPriority.length ? pickDirection(rect, bounds, positionPriority, popup) : undefined;
157
+ if (direction) {
158
+ return direction + getAlignSuffix(direction, rect, bounds);
159
+ }
71
160
  if (priorityDirection === 'auto') {
72
161
  var maxX = Math.max(rect.left - bounds.left, bounds.right - rect.right);
73
162
  var maxY = Math.max(rect.top - bounds.top, bounds.bottom - rect.bottom);
@@ -109,7 +198,10 @@ export var getPosition = function getPosition(target) {
109
198
  var mode = arguments.length > 2 ? arguments[2] : undefined;
110
199
  var popup = arguments.length > 3 ? arguments[3] : undefined;
111
200
  var container = arguments.length > 4 ? arguments[4] : undefined;
112
- if (mode === 'popover') return getPopoverPosition(target, priorityDirection, popup, container);
201
+ var positionPriority = arguments.length > 5 ? arguments[5] : undefined;
202
+ if (mode === 'popover') {
203
+ return getPopoverPosition(target, priorityDirection, popup, container, positionPriority);
204
+ }
113
205
  if (mode === 'menu') return getMenuPosition(target, priorityDirection);
114
206
  return 'bottom-left';
115
207
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheinx/hooks",
3
- "version": "3.10.4-beta.6",
3
+ "version": "3.10.4-beta.7",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",