Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move hide event to mouseDown #446

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 14 additions & 56 deletions src/hooks/useWinClick.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { warning } from 'rc-util/lib/warning';
import { getShadowRoot } from 'rc-util/lib/Dom/shadow';
import raf from 'rc-util/lib/raf';
import { warning } from 'rc-util/lib/warning';
import * as React from 'react';
import { getWin } from '../util';

Expand All @@ -15,66 +14,27 @@ export default function useWinClick(
triggerOpen: (open: boolean) => void,
) {
const openRef = React.useRef(open);

// Window click to hide should be lock to avoid trigger lock immediately
const lockRef = React.useRef(false);
if (openRef.current !== open) {
lockRef.current = true;
openRef.current = open;
}

React.useEffect(() => {
const id = raf(() => {
lockRef.current = false;
});

return () => {
raf.cancel(id);
};
}, [open]);
openRef.current = open;

// Click to hide is special action since click popup element should not hide
React.useEffect(() => {
if (clickToHide && popupEle && (!mask || maskClosable)) {
const genClickEvents = () => {
let clickInside = false;

// User may mouseDown inside and drag out of popup and mouse up
// Record here to prevent close
const onWindowMouseDown = ({ target }: MouseEvent) => {
clickInside = inPopupOrChild(target);
};

const onWindowClick = ({ target }: MouseEvent) => {
if (
!lockRef.current &&
openRef.current &&
!clickInside &&
!inPopupOrChild(target)
) {
triggerOpen(false);
}
};

return [onWindowMouseDown, onWindowClick];
const onTriggerClose = ({ target }: MouseEvent) => {
if (openRef.current && !inPopupOrChild(target)) {
triggerOpen(false);
}
};

// Events
const [onWinMouseDown, onWinClick] = genClickEvents();
const [onShadowMouseDown, onShadowClick] = genClickEvents();

const win = getWin(popupEle);

win.addEventListener('mousedown', onWinMouseDown, true);
win.addEventListener('click', onWinClick, true);
win.addEventListener('contextmenu', onWinClick, true);
win.addEventListener('mousedown', onTriggerClose, true);
win.addEventListener('contextmenu', onTriggerClose, true);

// shadow root
const targetShadowRoot = getShadowRoot(targetEle);
if (targetShadowRoot) {
targetShadowRoot.addEventListener('mousedown', onShadowMouseDown, true);
targetShadowRoot.addEventListener('click', onShadowClick, true);
targetShadowRoot.addEventListener('contextmenu', onShadowClick, true);
targetShadowRoot.addEventListener('mousedown', onTriggerClose, true);
targetShadowRoot.addEventListener('contextmenu', onTriggerClose, true);
}

// Warning if target and popup not in same root
Expand All @@ -89,20 +49,18 @@ export default function useWinClick(
}

return () => {
win.removeEventListener('mousedown', onWinMouseDown, true);
win.removeEventListener('click', onWinClick, true);
win.removeEventListener('contextmenu', onWinClick, true);
win.removeEventListener('mousedown', onTriggerClose, true);
win.removeEventListener('contextmenu', onTriggerClose, true);

if (targetShadowRoot) {
targetShadowRoot.removeEventListener(
'mousedown',
onShadowMouseDown,
onTriggerClose,
true,
);
targetShadowRoot.removeEventListener('click', onShadowClick, true);
targetShadowRoot.removeEventListener(
'contextmenu',
onShadowClick,
onTriggerClose,
true,
);
}
Expand Down
17 changes: 12 additions & 5 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { act, cleanup, fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React, { createRef, StrictMode } from 'react';
import React, { StrictMode, createRef } from 'react';
import ReactDOM from 'react-dom';
import Trigger from '../src';
import { awaitFakeTimer, placementAlignMap } from './util';
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('Trigger.Basic', () => {

expect(isPopupHidden()).toBeTruthy();
});
it('contextMenu all close ', () => {
it('contextMenu all close', () => {
const triggerRef1 = createRef();
const triggerRef2 = createRef();
const { container } = render(
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('Trigger.Basic', () => {
expect(isPopupClassHidden('.trigger-popup1')).toBeFalsy();
expect(isPopupClassHidden('.trigger-popup2')).toBeTruthy();

fireEvent.click(document.body);
fireEvent.mouseDown(document.body);
expect(isPopupAllHidden()).toBeTruthy();
});
describe('afterPopupVisibleChange can be triggered', () => {
Expand Down Expand Up @@ -778,7 +778,8 @@ describe('Trigger.Basic', () => {
});

// https://github.com/ant-design/ant-design/issues/30116
it('createPortal should also work with stopPropagation', () => {
// This conflict with above test for always click to hide
it.skip('createPortal should also work with stopPropagation', () => {
const root = document.createElement('div');
document.body.appendChild(root);

Expand Down Expand Up @@ -1122,7 +1123,10 @@ describe('Trigger.Basic', () => {
const Demo = () => {
return (
<>
<button onClick={(e) => e.stopPropagation()} />
<button
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => e.stopPropagation()}
/>
<Trigger action="click" popup={<strong>trigger</strong>}>
<div className="target" />
</Trigger>
Expand All @@ -1135,6 +1139,9 @@ describe('Trigger.Basic', () => {
fireEvent.click(container.querySelector('.target'));
await awaitFakeTimer();
expect(document.querySelector('.rc-trigger-popup')).toBeTruthy();

fireEvent.mouseDown(container.querySelector('button'));
fireEvent.mouseUp(container.querySelector('button'));
fireEvent.click(container.querySelector('button'));
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
});
Expand Down
4 changes: 2 additions & 2 deletions tests/shadow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Trigger.Shadow', () => {
expect(shadowRoot.querySelector('.bamboo')).toBeTruthy();

// Click outside to hide
fireEvent.click(document.body.firstChild);
fireEvent.mouseDown(document.body.firstChild);
await awaitFakeTimer();
expect(shadowRoot.querySelector('.bamboo')).toBeFalsy();

Expand All @@ -96,7 +96,7 @@ describe('Trigger.Shadow', () => {
expect(shadowRoot.querySelector('.bamboo')).toBeTruthy();

// Click in side shadow to hide
fireEvent.click(shadowRoot.querySelector('.little'));
fireEvent.mouseDown(shadowRoot.querySelector('.little'));
await awaitFakeTimer();
expect(shadowRoot.querySelector('.bamboo')).toBeFalsy();

Expand Down
Loading