@sheinx/hooks 3.10.4-beta.5 → 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;;;;;;0BAuHK;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;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAxEzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BA8KxD,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);
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 () {
@@ -20,11 +20,21 @@ export interface BasePopupProps {
20
20
  mouseLeaveDelay?: number;
21
21
  autoMode?: 'menu' | 'popover';
22
22
  /**
23
- * @en Popup location priority, default is left and right priority, only valid when position is not set
23
+ * @en Popup location priority, default is top and bottom priority, only valid when position is not set
24
24
  * @cn 弹出位置优先级, 默认为上下优先, 只在未设置 position 时生效
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')[];
33
+ /**
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
35
+ * @cn 弹出层渲染的容器。设置后,自动位置按容器可见区域计算,而不是按视口计算
36
+ */
37
+ container?: HTMLElement | null | (() => HTMLElement | null);
28
38
  targetEvents?: {
29
39
  onClick?: (e: {
30
40
  target: EventTarget | null;
@@ -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,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,GAA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E,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) => "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":"AAkGA,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,gKAKpB,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"}
@@ -39,20 +39,133 @@ var getMenuPosition = function getMenuPosition(target) {
39
39
  }
40
40
  return position;
41
41
  };
42
+
43
+ // 计算可用区域:有自定义容器时取「容器 ∩ 视口」,否则取视口
44
+ var getBounds = function getBounds(container) {
45
+ var viewport = {
46
+ left: 0,
47
+ top: 0,
48
+ right: _document.docSize.width,
49
+ bottom: _document.docSize.height
50
+ };
51
+ if (!container) return viewport;
52
+ var rect = container.getBoundingClientRect();
53
+ var bounds = {
54
+ left: Math.max(viewport.left, rect.left),
55
+ top: Math.max(viewport.top, rect.top),
56
+ right: Math.min(viewport.right, rect.right),
57
+ bottom: Math.min(viewport.bottom, rect.bottom)
58
+ };
59
+ // 容器与视口无交集(如容器被隐藏)时回退到视口
60
+ if (bounds.right <= bounds.left || bounds.bottom <= bounds.top) return viewport;
61
+ return bounds;
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
+ };
42
146
  var getPopoverPosition = function getPopoverPosition(target) {
43
147
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
44
148
  var popup = arguments.length > 2 ? arguments[2] : undefined;
149
+ var container = arguments.length > 3 ? arguments[3] : undefined;
150
+ var positionPriority = arguments.length > 4 ? arguments[4] : undefined;
45
151
  var position = 'bottom-left';
46
152
  if (!target) return position;
47
153
  var rect = target.getBoundingClientRect();
48
154
  var tempPriorityDirection = priorityDirection;
49
- var horizontalPoint = rect.left + rect.width / 2;
50
- var verticalPoint = rect.top + rect.height / 2;
51
- var windowHeight = _document.docSize.height;
52
- var windowWidth = _document.docSize.width;
155
+ var bounds = getBounds(container);
156
+ // 以可用区域左上角为原点
157
+ var horizontalPoint = rect.left + rect.width / 2 - bounds.left;
158
+ var verticalPoint = rect.top + rect.height / 2 - bounds.top;
159
+ var windowHeight = bounds.bottom - bounds.top;
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
+ }
53
166
  if (priorityDirection === 'auto') {
54
- var maxX = Math.max(rect.left, windowWidth - rect.right);
55
- var maxY = Math.max(rect.top, windowHeight - rect.bottom);
167
+ var maxX = Math.max(rect.left - bounds.left, bounds.right - rect.right);
168
+ var maxY = Math.max(rect.top - bounds.top, bounds.bottom - rect.bottom);
56
169
  tempPriorityDirection = maxX > maxY ? 'horizontal' : 'vertical';
57
170
  }
58
171
  if (tempPriorityDirection === 'horizontal') {
@@ -90,7 +203,11 @@ var getPosition = exports.getPosition = function getPosition(target) {
90
203
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
91
204
  var mode = arguments.length > 2 ? arguments[2] : undefined;
92
205
  var popup = arguments.length > 3 ? arguments[3] : undefined;
93
- if (mode === 'popover') return getPopoverPosition(target, priorityDirection, popup);
206
+ var container = arguments.length > 4 ? arguments[4] : undefined;
207
+ var positionPriority = arguments.length > 5 ? arguments[5] : undefined;
208
+ if (mode === 'popover') {
209
+ return getPopoverPosition(target, priorityDirection, popup, container, positionPriority);
210
+ }
94
211
  if (mode === 'menu') return getMenuPosition(target, priorityDirection);
95
212
  return 'bottom-left';
96
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;;;;;;0BAuHK;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;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAxEzC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;8BAOvC,MAAM,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC;2BA8KxD,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);
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 () {
@@ -20,11 +20,21 @@ export interface BasePopupProps {
20
20
  mouseLeaveDelay?: number;
21
21
  autoMode?: 'menu' | 'popover';
22
22
  /**
23
- * @en Popup location priority, default is left and right priority, only valid when position is not set
23
+ * @en Popup location priority, default is top and bottom priority, only valid when position is not set
24
24
  * @cn 弹出位置优先级, 默认为上下优先, 只在未设置 position 时生效
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')[];
33
+ /**
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
35
+ * @cn 弹出层渲染的容器。设置后,自动位置按容器可见区域计算,而不是按视口计算
36
+ */
37
+ container?: HTMLElement | null | (() => HTMLElement | null);
28
38
  targetEvents?: {
29
39
  onClick?: (e: {
30
40
  target: EventTarget | null;
@@ -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,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,CA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AC5D,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) => "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":"AAkGA,eAAO,MAAM,WAAW,WACd,WAAW,GAAG,IAAI,2EAEpB,SAAS,GAAG,MAAM,GAAG,MAAM,UACzB,WAAW,gKAKpB,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"}
@@ -33,20 +33,133 @@ var getMenuPosition = function getMenuPosition(target) {
33
33
  }
34
34
  return position;
35
35
  };
36
+
37
+ // 计算可用区域:有自定义容器时取「容器 ∩ 视口」,否则取视口
38
+ var getBounds = function getBounds(container) {
39
+ var viewport = {
40
+ left: 0,
41
+ top: 0,
42
+ right: docSize.width,
43
+ bottom: docSize.height
44
+ };
45
+ if (!container) return viewport;
46
+ var rect = container.getBoundingClientRect();
47
+ var bounds = {
48
+ left: Math.max(viewport.left, rect.left),
49
+ top: Math.max(viewport.top, rect.top),
50
+ right: Math.min(viewport.right, rect.right),
51
+ bottom: Math.min(viewport.bottom, rect.bottom)
52
+ };
53
+ // 容器与视口无交集(如容器被隐藏)时回退到视口
54
+ if (bounds.right <= bounds.left || bounds.bottom <= bounds.top) return viewport;
55
+ return bounds;
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
+ };
36
140
  var getPopoverPosition = function getPopoverPosition(target) {
37
141
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
38
142
  var popup = arguments.length > 2 ? arguments[2] : undefined;
143
+ var container = arguments.length > 3 ? arguments[3] : undefined;
144
+ var positionPriority = arguments.length > 4 ? arguments[4] : undefined;
39
145
  var position = 'bottom-left';
40
146
  if (!target) return position;
41
147
  var rect = target.getBoundingClientRect();
42
148
  var tempPriorityDirection = priorityDirection;
43
- var horizontalPoint = rect.left + rect.width / 2;
44
- var verticalPoint = rect.top + rect.height / 2;
45
- var windowHeight = docSize.height;
46
- var windowWidth = docSize.width;
149
+ var bounds = getBounds(container);
150
+ // 以可用区域左上角为原点
151
+ var horizontalPoint = rect.left + rect.width / 2 - bounds.left;
152
+ var verticalPoint = rect.top + rect.height / 2 - bounds.top;
153
+ var windowHeight = bounds.bottom - bounds.top;
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
+ }
47
160
  if (priorityDirection === 'auto') {
48
- var maxX = Math.max(rect.left, windowWidth - rect.right);
49
- var maxY = Math.max(rect.top, windowHeight - rect.bottom);
161
+ var maxX = Math.max(rect.left - bounds.left, bounds.right - rect.right);
162
+ var maxY = Math.max(rect.top - bounds.top, bounds.bottom - rect.bottom);
50
163
  tempPriorityDirection = maxX > maxY ? 'horizontal' : 'vertical';
51
164
  }
52
165
  if (tempPriorityDirection === 'horizontal') {
@@ -84,7 +197,11 @@ export var getPosition = function getPosition(target) {
84
197
  var priorityDirection = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'vertical';
85
198
  var mode = arguments.length > 2 ? arguments[2] : undefined;
86
199
  var popup = arguments.length > 3 ? arguments[3] : undefined;
87
- if (mode === 'popover') return getPopoverPosition(target, priorityDirection, popup);
200
+ var container = arguments.length > 4 ? arguments[4] : undefined;
201
+ var positionPriority = arguments.length > 5 ? arguments[5] : undefined;
202
+ if (mode === 'popover') {
203
+ return getPopoverPosition(target, priorityDirection, popup, container, positionPriority);
204
+ }
88
205
  if (mode === 'menu') return getMenuPosition(target, priorityDirection);
89
206
  return 'bottom-left';
90
207
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheinx/hooks",
3
- "version": "3.10.4-beta.5",
3
+ "version": "3.10.4-beta.7",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",