Skip to content

Commit

Permalink
fix: trigger open logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 8, 2024
1 parent e976903 commit c779293
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/examples/portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const PortalPopup = () =>
style={popupBorderStyle}
onMouseDown={(e) => {
console.log('Portal Down', e);
e.stopPropagation();
e.preventDefault();
}}
>
i am a portal element
Expand Down Expand Up @@ -84,6 +86,9 @@ const Test = () => {
<PortalPopup />
</div>
}
onPopupVisibleChange={(visible) => {
console.log('visible change:', visible);
}}
>
<button>Click Me</button>
</Trigger>
Expand Down
3 changes: 3 additions & 0 deletions src/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface PopupProps {
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
onPointerEnter?: React.MouseEventHandler<HTMLDivElement>;
onMouseDownCapture?: React.MouseEventHandler<HTMLDivElement>;
zIndex?: number;

mask?: boolean;
Expand Down Expand Up @@ -105,6 +106,7 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
onMouseEnter,
onMouseLeave,
onPointerEnter,
onMouseDownCapture,

ready,
offsetX,
Expand Down Expand Up @@ -255,6 +257,7 @@ const Popup = React.forwardRef<HTMLDivElement, PopupProps>((props, ref) => {
onMouseLeave={onMouseLeave}
onPointerEnter={onPointerEnter}
onClick={onClick}
onMouseDownCapture={onMouseDownCapture}
>
{arrow && (
<Arrow
Expand Down
12 changes: 11 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ export function generateTrigger(
React.useState<VoidFunction>(null);

// =========================== Align ============================
const [mousePos, setMousePos] = React.useState<[x: number, y: number] | null>(null);
const [mousePos, setMousePos] = React.useState<
[x: number, y: number] | null
>(null);

const setMousePosByEvent = (
event: Pick<React.MouseEvent, 'clientX' | 'clientY'>,
Expand Down Expand Up @@ -720,6 +722,14 @@ export function generateTrigger(
fresh={fresh}
// Click
onClick={onPopupClick}
onMouseDownCapture={() => {
// Additional check for click to hide
// Since `createPortal` will not included in the popup element
// So we use capture to handle this
if (clickToHide) {
triggerOpen(true);
}
}}
// Mask
mask={mask}
// Motion
Expand Down

0 comments on commit c779293

Please sign in to comment.