Skip to content

Commit 8cfb4ce

Browse files
authored
chore: use rc-util handle shadow (#387)
1 parent 1bc06a5 commit 8cfb4ce

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"rc-align": "^4.0.0",
6767
"rc-motion": "^2.0.0",
6868
"rc-resize-observer": "^1.3.1",
69-
"rc-util": "^5.29.2"
69+
"rc-util": "^5.31.1"
7070
},
7171
"peerDependencies": {
7272
"react": ">=16.9.0",

src/hooks/useWinClick.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { warning } from 'rc-util';
2+
import { getShadowRoot } from 'rc-util/lib/Dom/shadow';
23
import raf from 'rc-util/lib/raf';
34
import * as React from 'react';
45
import { getWin } from '../util';
@@ -62,13 +63,10 @@ export default function useWinClick(
6263
win.addEventListener('click', onWindowClick);
6364

6465
// shadow root
65-
const inShadow = targetRoot && targetRoot !== targetEle.ownerDocument;
66-
if (inShadow) {
67-
(targetRoot as ShadowRoot).addEventListener(
68-
'mousedown',
69-
onWindowMouseDown,
70-
);
71-
(targetRoot as ShadowRoot).addEventListener('click', onWindowClick);
66+
const targetShadowRoot = getShadowRoot(targetEle);
67+
if (targetShadowRoot) {
68+
targetShadowRoot.addEventListener('mousedown', onWindowMouseDown);
69+
targetShadowRoot.addEventListener('click', onWindowClick);
7270
}
7371

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

88-
if (inShadow) {
89-
(targetRoot as ShadowRoot).removeEventListener(
90-
'mousedown',
91-
onWindowMouseDown,
92-
);
93-
(targetRoot as ShadowRoot).removeEventListener(
94-
'click',
95-
onWindowClick,
96-
);
86+
if (targetShadowRoot) {
87+
targetShadowRoot.removeEventListener('mousedown', onWindowMouseDown);
88+
targetShadowRoot.removeEventListener('click', onWindowClick);
9789
}
9890
};
9991
}

src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classNames from 'classnames';
33
import type { CSSMotionProps } from 'rc-motion';
44
import ResizeObserver from 'rc-resize-observer';
55
import { isDOM } from 'rc-util/lib/Dom/findDOMNode';
6+
import { getShadowRoot } from 'rc-util/lib/Dom/shadow';
67
import useEvent from 'rc-util/lib/hooks/useEvent';
78
import useId from 'rc-util/lib/hooks/useId';
89
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
@@ -257,10 +258,10 @@ export function generateTrigger(
257258

258259
return (
259260
childDOM?.contains(ele) ||
260-
(childDOM?.getRootNode() as ShadowRoot)?.host === ele ||
261+
getShadowRoot(childDOM)?.host === ele ||
261262
ele === childDOM ||
262263
popupEle?.contains(ele) ||
263-
(popupEle?.getRootNode() as ShadowRoot)?.host === ele ||
264+
getShadowRoot(popupEle)?.host === ele ||
264265
ele === popupEle ||
265266
Object.values(subPopupElements.current).some(
266267
(subPopupEle) => subPopupEle?.contains(ele) || ele === subPopupEle,

tests/flipShift.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { act, cleanup, render } from '@testing-library/react';
22
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
3+
import React from 'react';
34
import Trigger from '../src';
45

56
/*
@@ -135,8 +136,6 @@ describe('Trigger.Flip+Shift', () => {
135136
await Promise.resolve();
136137
});
137138

138-
console.log(document.body.innerHTML);
139-
140139
expect(
141140
document.querySelector('.rc-trigger-popup-placement-bottom'),
142141
).toBeTruthy();

0 commit comments

Comments
 (0)