Skip to content

Commit

Permalink
fix: update popover maskCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed May 17, 2024
1 parent 9b3b42e commit 9dcfa74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/designer/src/components/components-popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const ComponentsPopover = observer(
}
footer={tipsTextMap[type]}
width="330px"
maskClosable
body={
<ComponentsPanel
isScope
Expand Down
36 changes: 30 additions & 6 deletions packages/ui/src/popover.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect, useMemo, useCallback } from 'react';
import React, { useState, useRef, useEffect, useMemo, useCallback, useLayoutEffect } from 'react';
import ReactDOM from 'react-dom';
import { noop } from '@music163/tango-helpers';
import { Box } from 'coral-system';
Expand All @@ -17,6 +17,10 @@ export interface PopoverProps {
* 浮层被遮挡时自动调整位置
*/
autoAdjustOverflow?: boolean;
/**
* 点击蒙层是否允许关闭
*/
maskClosable?: boolean;
/**
* 手动唤起时的位置
*/
Expand All @@ -39,6 +43,7 @@ export interface PopoverProps {
export const Popover: React.FC<PopoverProps> = ({
open,
overlay,
maskClosable = false,
autoAdjustOverflow = true,
left: controlledLeft,
top: controlledTop,
Expand All @@ -59,16 +64,35 @@ export const Popover: React.FC<PopoverProps> = ({
);

useEffect(() => {
if (typeof controlledTop === 'number') {
setTop(controlledTop);
}
if (typeof controlledLeft === 'number') {
setLeft(controlledLeft);
}
}, [controlledLeft]);
}, [controlledTop, controlledLeft]);

useEffect(() => {
if (typeof controlledTop === 'number') {
setTop(controlledTop);
useLayoutEffect(() => {
const handleDocumentClick = (e: MouseEvent) => {
if (
maskClosable &&
visible &&
popoverRef.current &&
!popoverRef.current.contains(e.target as Node)
) {
setVisible(false);
onOpenChange(false);
}
};

if (maskClosable && visible) {
document.addEventListener('click', handleDocumentClick, true);
}
}, [controlledTop]);

return () => {
document.removeEventListener('click', handleDocumentClick);
};
}, [maskClosable, onOpenChange, visible]);

const handleClick = useCallback(
(e: React.MouseEvent) => {
Expand Down

0 comments on commit 9dcfa74

Please sign in to comment.