Skip to content

Commit 47d51da

Browse files
authored
refactor: Trigger use hooks instead of (#604)
* chore: init * chore: refactor * chore: get dom
1 parent 1f75252 commit 47d51da

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"@rc-component/motion": "^1.1.4",
4545
"@rc-component/portal": "^2.2.0",
46-
"@rc-component/resize-observer": "^1.0.0",
46+
"@rc-component/resize-observer": "^1.1.1",
4747
"@rc-component/util": "^1.2.1",
4848
"clsx": "^2.1.1"
4949
},

src/index.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Portal from '@rc-component/portal';
22
import { clsx } from 'clsx';
33
import type { CSSMotionProps } from '@rc-component/motion';
4-
import ResizeObserver from '@rc-component/resize-observer';
5-
import { isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
4+
import { useResizeObserver } from '@rc-component/resize-observer';
5+
import { getDOM, isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
66
import { getShadowRoot } from '@rc-component/util/lib/Dom/shadow';
7+
import { getNodeRef, useComposeRef } from '@rc-component/util/lib/ref';
78
import useEvent from '@rc-component/util/lib/hooks/useEvent';
89
import useId from '@rc-component/util/lib/hooks/useId';
910
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
@@ -261,9 +262,11 @@ export function generateTrigger(
261262
const externalForwardRef = React.useRef<HTMLElement>(null);
262263

263264
const setTargetRef = useEvent((node: HTMLElement) => {
264-
if (isDOM(node) && targetEle !== node) {
265-
setTargetEle(node);
266-
externalForwardRef.current = node;
265+
const domNode = getDOM(node) as HTMLElement;
266+
267+
if (isDOM(domNode) && targetEle !== domNode) {
268+
setTargetEle(domNode);
269+
externalForwardRef.current = domNode;
267270
}
268271
});
269272

@@ -782,22 +785,25 @@ export function generateTrigger(
782785
y: arrowY,
783786
};
784787

788+
// =================== Resize Observer ===================
789+
// Use hook to observe target element resize
790+
// Pass targetEle directly instead of a function so the hook will re-observe when target changes
791+
useResizeObserver(mergedOpen, targetEle, onTargetResize);
792+
793+
// Compose refs
794+
const mergedRef = useComposeRef(setTargetRef, getNodeRef(child));
795+
785796
// Child Node
786797
const triggerNode = React.cloneElement(child, {
787798
...mergedChildrenProps,
788799
...passedProps,
800+
ref: mergedRef,
789801
});
790802

791803
// Render
792804
return (
793805
<>
794-
<ResizeObserver
795-
disabled={!mergedOpen}
796-
ref={setTargetRef}
797-
onResize={onTargetResize}
798-
>
799-
{triggerNode}
800-
</ResizeObserver>
806+
{triggerNode}
801807
{rendedRef.current && (!uniqueContext || !unique) && (
802808
<TriggerContext.Provider value={context}>
803809
<Popup

tests/ref.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,36 @@ describe('Trigger.Ref', () => {
5050
document.querySelector('.rc-trigger-popup'),
5151
);
5252
});
53+
54+
it('should support ref.nativeElement', () => {
55+
const ChildComponent = React.forwardRef<
56+
{ nativeElement: HTMLElement },
57+
React.PropsWithChildren
58+
>((props, ref) => {
59+
const buttonRef = React.useRef<HTMLButtonElement>(null);
60+
61+
React.useImperativeHandle(ref, () => ({
62+
nativeElement: buttonRef.current,
63+
}));
64+
65+
return <button ref={buttonRef} {...props} />;
66+
});
67+
68+
const triggerRef = React.createRef<TriggerRef>();
69+
const childRef = React.createRef<{ nativeElement: HTMLElement }>();
70+
71+
const { container } = render(
72+
<Trigger ref={triggerRef} popup={<div />}>
73+
<ChildComponent ref={childRef} />
74+
</Trigger>,
75+
);
76+
77+
const buttonElement = container.querySelector('button');
78+
79+
// Check child ref returns object with nativeElement
80+
expect(childRef.current.nativeElement).toBe(buttonElement);
81+
82+
// Check Trigger can extract DOM from child ref
83+
expect(triggerRef.current.nativeElement).toBe(buttonElement);
84+
});
5385
});

0 commit comments

Comments
 (0)