x-ui-design 0.8.26 → 0.8.27
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/dist/esm/types/hooks/usePosition.d.ts +1 -1
- package/dist/index.esm.js +43 -10
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +43 -10
- package/dist/index.js.map +1 -1
- package/lib/components/Dropdown/Dropdown.tsx +2 -2
- package/lib/components/Popover/Popover.tsx +2 -2
- package/lib/hooks/usePosition.ts +57 -11
- package/package.json +1 -1
|
@@ -9,7 +9,7 @@ type TPosition = {
|
|
|
9
9
|
offset?: number;
|
|
10
10
|
};
|
|
11
11
|
export declare const usePosition: ({ isOpen, offset, popupRef, placement, triggerRef, getPopupContainer }: TPosition) => {
|
|
12
|
-
|
|
12
|
+
showPlacement: string;
|
|
13
13
|
dropdownPosition: CSSProperties;
|
|
14
14
|
};
|
|
15
15
|
export {};
|
package/dist/index.esm.js
CHANGED
|
@@ -2594,6 +2594,15 @@ function getScrollParent(el, includeSelf = false) {
|
|
|
2594
2594
|
}
|
|
2595
2595
|
return document.scrollingElement;
|
|
2596
2596
|
}
|
|
2597
|
+
const clampWithinContainer = (left, popupWidth, containerRect) => {
|
|
2598
|
+
const minLeft = containerRect.left + document.documentElement.scrollLeft;
|
|
2599
|
+
const maxLeft = containerRect.right + document.documentElement.scrollLeft - popupWidth;
|
|
2600
|
+
return {
|
|
2601
|
+
minLeft,
|
|
2602
|
+
maxLeft,
|
|
2603
|
+
leftPosition: Math.min(Math.max(left, minLeft), maxLeft)
|
|
2604
|
+
};
|
|
2605
|
+
};
|
|
2597
2606
|
const usePosition = ({
|
|
2598
2607
|
isOpen,
|
|
2599
2608
|
offset = 4,
|
|
@@ -2602,7 +2611,7 @@ const usePosition = ({
|
|
|
2602
2611
|
triggerRef,
|
|
2603
2612
|
getPopupContainer
|
|
2604
2613
|
}) => {
|
|
2605
|
-
const [
|
|
2614
|
+
const [showPlacement, setShowPlacement] = useState('');
|
|
2606
2615
|
const [_dropdownPosition, setDropdownPosition] = useState({});
|
|
2607
2616
|
const dropdownPosition = useCallback(() => {
|
|
2608
2617
|
if (!triggerRef.current) {
|
|
@@ -2615,9 +2624,14 @@ const usePosition = ({
|
|
|
2615
2624
|
const spaceBelow = containerRect.bottom - inputRect.bottom;
|
|
2616
2625
|
const _shouldShowAbove = spaceBelow < dropdownHeight && spaceAbove > dropdownHeight;
|
|
2617
2626
|
const hasRight = placement?.includes('Right');
|
|
2618
|
-
setShouldShowAbove(_shouldShowAbove);
|
|
2619
2627
|
if (getPopupContainer) {
|
|
2620
|
-
const
|
|
2628
|
+
const {
|
|
2629
|
+
minLeft,
|
|
2630
|
+
maxLeft,
|
|
2631
|
+
leftPosition
|
|
2632
|
+
} = clampWithinContainer(hasRight ? (inputRect.left || 0) + (triggerRef.current?.offsetWidth || 0) - (popupRef.current?.offsetWidth || 0) : (inputRect.left || 0) + document.documentElement.scrollLeft, popupRef.current?.offsetWidth || 0, containerRect);
|
|
2633
|
+
const _center = minLeft + maxLeft < (popupRef.current?.offsetWidth || 0) ? 'center' : '';
|
|
2634
|
+
setShowPlacement(_shouldShowAbove ? `bottom ${_center}` : `${_center}`);
|
|
2621
2635
|
const _top = (inputRect.top || 0) + document.documentElement.scrollTop;
|
|
2622
2636
|
if (_shouldShowAbove) {
|
|
2623
2637
|
setDropdownPosition({
|
|
@@ -2634,9 +2648,27 @@ const usePosition = ({
|
|
|
2634
2648
|
setDropdownPosition({
|
|
2635
2649
|
top: (_shouldShowAbove ? triggerRef.current.offsetTop - (popupRef.current?.offsetHeight || dropdownHeight) - offset * 2 : triggerRef.current.offsetTop + triggerRef.current?.offsetHeight) + offset,
|
|
2636
2650
|
...(hasRight ? {
|
|
2637
|
-
left:
|
|
2651
|
+
left: (() => {
|
|
2652
|
+
const {
|
|
2653
|
+
minLeft,
|
|
2654
|
+
maxLeft,
|
|
2655
|
+
leftPosition
|
|
2656
|
+
} = clampWithinContainer(triggerRef.current.offsetLeft + (triggerRef.current?.offsetWidth || 0) - (popupRef.current?.offsetWidth || dropdownHeight), popupRef.current?.offsetWidth || dropdownHeight, containerRect);
|
|
2657
|
+
const _center = minLeft + maxLeft < (popupRef.current?.offsetWidth || 0) ? 'center' : '';
|
|
2658
|
+
setShowPlacement(_shouldShowAbove ? `bottom ${_center}` : `${_center}`);
|
|
2659
|
+
return leftPosition;
|
|
2660
|
+
})()
|
|
2638
2661
|
} : {
|
|
2639
|
-
left:
|
|
2662
|
+
left: (() => {
|
|
2663
|
+
const {
|
|
2664
|
+
minLeft,
|
|
2665
|
+
maxLeft,
|
|
2666
|
+
leftPosition
|
|
2667
|
+
} = clampWithinContainer(triggerRef.current.offsetLeft, popupRef.current?.offsetWidth || dropdownHeight, containerRect);
|
|
2668
|
+
const _center = minLeft + maxLeft < (popupRef.current?.offsetWidth || 0) ? 'center' : '';
|
|
2669
|
+
setShowPlacement(_shouldShowAbove ? `bottom ${_center}` : `${_center}`);
|
|
2670
|
+
return leftPosition;
|
|
2671
|
+
})()
|
|
2640
2672
|
})
|
|
2641
2673
|
});
|
|
2642
2674
|
}
|
|
@@ -2644,6 +2676,7 @@ const usePosition = ({
|
|
|
2644
2676
|
useEffect(() => {
|
|
2645
2677
|
if (!isOpen) return;
|
|
2646
2678
|
const _dropdownPosition = () => dropdownPosition();
|
|
2679
|
+
console.log(_dropdownPosition);
|
|
2647
2680
|
_dropdownPosition();
|
|
2648
2681
|
const controller = new AbortController();
|
|
2649
2682
|
const scrollableParents = getScrollParent(triggerRef.current, true);
|
|
@@ -2663,7 +2696,7 @@ const usePosition = ({
|
|
|
2663
2696
|
};
|
|
2664
2697
|
}, [isOpen, triggerRef, getPopupContainer, dropdownPosition]);
|
|
2665
2698
|
return {
|
|
2666
|
-
|
|
2699
|
+
showPlacement,
|
|
2667
2700
|
dropdownPosition: {
|
|
2668
2701
|
..._dropdownPosition,
|
|
2669
2702
|
opacity: Object.keys(_dropdownPosition).length ? 1 : 0
|
|
@@ -5351,7 +5384,7 @@ const Dropdown = ({
|
|
|
5351
5384
|
const popupRef = useRef(null);
|
|
5352
5385
|
const menuRef = useRef(null);
|
|
5353
5386
|
const {
|
|
5354
|
-
|
|
5387
|
+
showPlacement,
|
|
5355
5388
|
dropdownPosition
|
|
5356
5389
|
} = usePosition({
|
|
5357
5390
|
popupRef,
|
|
@@ -5428,7 +5461,7 @@ const Dropdown = ({
|
|
|
5428
5461
|
...dropdownPosition
|
|
5429
5462
|
}
|
|
5430
5463
|
}, arrow && /*#__PURE__*/React.createElement("div", {
|
|
5431
|
-
className: `${prefixCls}-arrow ${
|
|
5464
|
+
className: `${prefixCls}-arrow ${showPlacement ? 'bottom' : ''}`
|
|
5432
5465
|
}), overlay ? typeof overlay === 'function' ? overlay() : overlay : popupRender ? popupRender(menu ? /*#__PURE__*/React.createElement(MenuInner, {
|
|
5433
5466
|
prefixCls: prefixCls,
|
|
5434
5467
|
items: menu.items,
|
|
@@ -5521,7 +5554,7 @@ const Popover = ({
|
|
|
5521
5554
|
const isOpen = visible !== undefined ? visible : open !== undefined ? open : innerOpen;
|
|
5522
5555
|
const {
|
|
5523
5556
|
dropdownPosition,
|
|
5524
|
-
|
|
5557
|
+
showPlacement
|
|
5525
5558
|
} = usePosition({
|
|
5526
5559
|
isOpen,
|
|
5527
5560
|
offset: 10,
|
|
@@ -5596,7 +5629,7 @@ const Popover = ({
|
|
|
5596
5629
|
}, title), /*#__PURE__*/React.createElement("div", {
|
|
5597
5630
|
className: `${prefixCls}-inner`
|
|
5598
5631
|
}, content), /*#__PURE__*/React.createElement("div", {
|
|
5599
|
-
className: `${prefixCls}-arrow ${
|
|
5632
|
+
className: `${prefixCls}-arrow ${showPlacement}`
|
|
5600
5633
|
}))));
|
|
5601
5634
|
};
|
|
5602
5635
|
|