Skip to content

chore: use rc-util handle shadow #387

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

Merged
merged 1 commit into from
May 23, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"rc-align": "^4.0.0",
"rc-motion": "^2.0.0",
"rc-resize-observer": "^1.3.1",
"rc-util": "^5.29.2"
"rc-util": "^5.31.1"
},
"peerDependencies": {
"react": ">=16.9.0",
Expand Down
24 changes: 8 additions & 16 deletions src/hooks/useWinClick.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { warning } from 'rc-util';
import { getShadowRoot } from 'rc-util/lib/Dom/shadow';
import raf from 'rc-util/lib/raf';
import * as React from 'react';
import { getWin } from '../util';
Expand Down Expand Up @@ -62,13 +63,10 @@ export default function useWinClick(
win.addEventListener('click', onWindowClick);

// shadow root
const inShadow = targetRoot && targetRoot !== targetEle.ownerDocument;
if (inShadow) {
(targetRoot as ShadowRoot).addEventListener(
'mousedown',
onWindowMouseDown,
);
(targetRoot as ShadowRoot).addEventListener('click', onWindowClick);
const targetShadowRoot = getShadowRoot(targetEle);
if (targetShadowRoot) {
targetShadowRoot.addEventListener('mousedown', onWindowMouseDown);
targetShadowRoot.addEventListener('click', onWindowClick);
}

// Warning if target and popup not in same root
Expand All @@ -85,15 +83,9 @@ export default function useWinClick(
win.removeEventListener('mousedown', onWindowMouseDown);
win.removeEventListener('click', onWindowClick);

if (inShadow) {
(targetRoot as ShadowRoot).removeEventListener(
'mousedown',
onWindowMouseDown,
);
(targetRoot as ShadowRoot).removeEventListener(
'click',
onWindowClick,
);
if (targetShadowRoot) {
targetShadowRoot.removeEventListener('mousedown', onWindowMouseDown);
targetShadowRoot.removeEventListener('click', onWindowClick);
}
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames';
import type { CSSMotionProps } from 'rc-motion';
import ResizeObserver from 'rc-resize-observer';
import { isDOM } from 'rc-util/lib/Dom/findDOMNode';
import { getShadowRoot } from 'rc-util/lib/Dom/shadow';
import useEvent from 'rc-util/lib/hooks/useEvent';
import useId from 'rc-util/lib/hooks/useId';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
Expand Down Expand Up @@ -257,10 +258,10 @@ export function generateTrigger(

return (
childDOM?.contains(ele) ||
(childDOM?.getRootNode() as ShadowRoot)?.host === ele ||
getShadowRoot(childDOM)?.host === ele ||
ele === childDOM ||
popupEle?.contains(ele) ||
(popupEle?.getRootNode() as ShadowRoot)?.host === ele ||
getShadowRoot(popupEle)?.host === ele ||
ele === popupEle ||
Object.values(subPopupElements.current).some(
(subPopupEle) => subPopupEle?.contains(ele) || ele === subPopupEle,
Expand Down
3 changes: 1 addition & 2 deletions tests/flipShift.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, cleanup, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import Trigger from '../src';

/*
Expand Down Expand Up @@ -135,8 +136,6 @@ describe('Trigger.Flip+Shift', () => {
await Promise.resolve();
});

console.log(document.body.innerHTML);

expect(
document.querySelector('.rc-trigger-popup-placement-bottom'),
).toBeTruthy();
Expand Down