@gingkoo/pandora-metabase 1.0.0-alpha.3 → 1.0.0-alpha.4

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.
@@ -11,6 +11,7 @@ export declare function generateTrigger(PortalComponent: any): {
11
11
  getCurrentNodePos: () => {
12
12
  x: number;
13
13
  y: number;
14
+ t: number;
14
15
  h: any;
15
16
  };
16
17
  getContainer: () => HTMLSpanElement;
@@ -54,6 +55,7 @@ declare const _default: {
54
55
  getCurrentNodePos: () => {
55
56
  x: number;
56
57
  y: number;
58
+ t: number;
57
59
  h: any;
58
60
  };
59
61
  getContainer: () => HTMLSpanElement;
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @gingkoo/pandora-metabase v1.0.0-alpha.3
2
+ * @gingkoo/pandora-metabase v1.0.0-alpha.4
3
3
  */
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
  import * as React from 'react';
@@ -1813,6 +1813,7 @@ const getComputedTranslate = obj => {
1813
1813
  };
1814
1814
  // 获取元素距离浏览器顶部的距离
1815
1815
  const getElementTop = elem => {
1816
+ if (!elem) return 0;
1816
1817
  let elemTop = elem.offsetTop;
1817
1818
  let pElem = elem.offsetParent;
1818
1819
  while (pElem != null) {
@@ -1826,6 +1827,7 @@ const getElementTop = elem => {
1826
1827
  };
1827
1828
  // 获取元素距离浏览器顶部的距离
1828
1829
  const getElementLeft = elem => {
1830
+ if (!elem) return 0;
1829
1831
  let elemLeft = elem.offsetLeft;
1830
1832
  let pElem = elem.offsetParent;
1831
1833
  while (pElem != null) {
@@ -1837,8 +1839,23 @@ const getElementLeft = elem => {
1837
1839
  }
1838
1840
  return elemLeft;
1839
1841
  };
1840
- const getScrollTop = () => {
1841
- return document.documentElement.scrollTop;
1842
+ // 获取元素可见范围内高度
1843
+ const getContainerVisibleHeight = container => {
1844
+ if (!container) return 0;
1845
+ const rect = container.getBoundingClientRect();
1846
+ const windowHeight = window.innerHeight || document.documentElement.clientHeight;
1847
+ // 元素顶部在视口上方 -> 不可见
1848
+ if (rect.bottom < 0) return 0;
1849
+ // 元素底部在视口下方 -> 不可见
1850
+ if (rect.top > windowHeight) return 0;
1851
+ // 可见区域的 top 和 bottom
1852
+ const visibleTop = Math.max(rect.top, 0);
1853
+ const visibleBottom = Math.min(rect.bottom, windowHeight);
1854
+ // 可见高度
1855
+ return visibleBottom - visibleTop;
1856
+ };
1857
+ const getScrollTop = elem => {
1858
+ return elem?.scrollTop || document.documentElement.scrollTop;
1842
1859
  };
1843
1860
  // 浏览器可视宽高
1844
1861
  const getWindowSize = () => {
@@ -3445,7 +3462,7 @@ function generateTrigger(PortalComponent) {
3445
3462
  this.props.visible && this.props.closable && this.props.hideVisible();
3446
3463
  };
3447
3464
  attachParent = popupContainer => {
3448
- let mountNode = returnDocument().body;
3465
+ let mountNode = this.props.container || returnDocument().body;
3449
3466
  mountNode.appendChild(popupContainer);
3450
3467
  };
3451
3468
  getCurrentNodePos = () => {
@@ -3454,8 +3471,9 @@ function generateTrigger(PortalComponent) {
3454
3471
  container
3455
3472
  } = this.props;
3456
3473
  return {
3457
- x: getElementLeft(node),
3458
- y: getElementTop(node) - (container?.scrollTop || 0),
3474
+ x: getElementLeft(node) - getElementLeft(container),
3475
+ y: getElementTop(node) - getElementTop(container),
3476
+ t: getElementTop(container),
3459
3477
  h: node.offsetHeight
3460
3478
  };
3461
3479
  };
@@ -3498,19 +3516,21 @@ function generateTrigger(PortalComponent) {
3498
3516
  didUpdate = () => {
3499
3517
  if (!this.props.node) return false;
3500
3518
  let {
3501
- innerSpacing = 10
3519
+ innerSpacing = 10,
3520
+ container
3502
3521
  } = this.props;
3503
3522
  let pos = this.getCurrentNodePos();
3504
- let posY = pos.y - getScrollTop();
3523
+ let posY = pos.y - getScrollTop(container);
3505
3524
  if (!this.ref) return false;
3506
3525
  let realHeight = this.ref?.current?.childNodes?.[0]?.offsetHeight || 0;
3507
3526
  if (!realHeight) return false;
3508
3527
  let {
3509
3528
  height: winH
3510
3529
  } = getWindowSize();
3511
- let downH = winH - posY - pos.h; // 元素下面可用高度
3530
+ let containerH = getContainerVisibleHeight(container);
3531
+ let downH = (containerH || winH) - posY - pos.h; // 元素下面可用高度
3512
3532
  let maxHeight = 0;
3513
- let topHeight = getScrollTop();
3533
+ let topHeight = getScrollTop(container);
3514
3534
  if (downH >= posY || realHeight <= downH - innerSpacing - outSpacing) {
3515
3535
  // 下面比上面宽敞 或 下面足够放下所有 放下面
3516
3536
  maxHeight = Math.min(realHeight, downH - innerSpacing - outSpacing);