Skip to content

Commit adf32ea

Browse files
committed
fix: close unique popup when trigger unmounted
1 parent 3ff7d68 commit adf32ea

4 files changed

Lines changed: 114 additions & 2 deletions

File tree

‎src/UniqueProvider/index.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ const UniqueProvider = ({
6060

6161
const delayInvoke = useDelay();
6262

63+
// Store the target of the latest show call, used to detect removed targets
64+
const targetRef = React.useRef<HTMLElement>(null);
65+
6366
const show = useEvent(
6467
(showOptions: UniqueShowOptions, isOpen: () => boolean) => {
6568
// Store the isOpen function for later use in hide
6669
isOpenRef.current = isOpen;
70+
targetRef.current = showOptions.target;
6771

6872
delayInvoke(() => {
6973
trigger(showOptions);
@@ -75,7 +79,9 @@ const UniqueProvider = ({
7579
delayInvoke(() => {
7680
// Check if we should still hide by calling the isOpen function
7781
// If isOpen returns true, it means another trigger wants to keep it open
78-
if (isOpenRef.current?.()) {
82+
// Skip the check when the target is removed from the DOM (e.g. trigger unmounted),
83+
// since its isOpen function is frozen and would keep returning true
84+
if (isOpenRef.current?.() && targetRef.current?.isConnected) {
7985
return; // Don't hide if something else wants it open
8086
}
8187

@@ -138,10 +144,18 @@ const UniqueProvider = ({
138144
mergedOptions?.prefixCls,
139145
]);
140146

147+
const release = useEvent((target: HTMLElement) => {
148+
// Close the popup only when it is currently showing for the removed target
149+
if (open && targetRef.current === target) {
150+
trigger(false);
151+
}
152+
});
153+
141154
const contextValue = React.useMemo<UniqueContextProps>(
142155
() => ({
143156
show,
144157
hide,
158+
release,
145159
}),
146160
[],
147161
);

‎src/context.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export interface UniqueShowOptions {
4141
export interface UniqueContextProps {
4242
show: (options: UniqueShowOptions, isOpen: () => boolean) => void;
4343
hide: (delay: number) => void;
44+
/** Notify the provider a trigger target is removed (e.g. trigger unmounted) */
45+
release: (target: HTMLElement) => void;
4446
}
4547

4648
export const UniqueContext = React.createContext<UniqueContextProps | null>(

‎src/index.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ export function generateTrigger(
265265
// Use state to control here since `useRef` update not trigger render
266266
const [targetEle, setTargetEle] = React.useState<HTMLElement>(null);
267267

268+
// Keep the latest target for unmount checking
269+
const targetEleRef = React.useRef<HTMLElement>(null);
270+
targetEleRef.current = targetEle;
271+
268272
// Used for forwardRef target. Not use internal
269273
const externalForwardRef = React.useRef<HTMLElement>(null);
270274

@@ -386,6 +390,17 @@ export function generateTrigger(
386390
const openRef = React.useRef(mergedOpen);
387391
openRef.current = mergedOpen;
388392

393+
// Close the shared popup when the trigger is unmounted
394+
React.useEffect(() => {
395+
if (uniqueContext && unique && !parentContext) {
396+
return () => {
397+
if (targetEleRef.current) {
398+
uniqueContext.release(targetEleRef.current);
399+
}
400+
};
401+
}
402+
}, []);
403+
389404
const internalTriggerOpen = useEvent((nextOpen: boolean) => {
390405
flushSync(() => {
391406
if (rawOpen !== nextOpen) {

‎tests/unique.test.tsx‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('Trigger.Unique', () => {
376376
});
377377

378378
it('esc should close unique popup', async () => {
379-
const { container,baseElement } = render(
379+
const { container, baseElement } = render(
380380
<UniqueProvider>
381381
<Trigger action={['click']} popup={<div>Popup</div>} unique>
382382
<div className="target" />
@@ -389,4 +389,85 @@ describe('Trigger.Unique', () => {
389389
fireEvent.keyDown(window, { key: 'Escape' });
390390
expect(baseElement.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
391391
});
392+
393+
it('should hide popup when the trigger is unmounted', async () => {
394+
const Demo = ({ show }: { show: boolean }) => (
395+
<UniqueProvider>
396+
{show && (
397+
<Trigger
398+
action={['hover']}
399+
popup={<strong className="x-content">tooltip</strong>}
400+
unique
401+
mouseEnterDelay={0.1}
402+
mouseLeaveDelay={0.1}
403+
>
404+
<div className="target">hover me</div>
405+
</Trigger>
406+
)}
407+
</UniqueProvider>
408+
);
409+
410+
const { container, rerender } = render(<Demo show />);
411+
412+
// Hover to open the popup
413+
fireEvent.mouseEnter(container.querySelector('.target'));
414+
await awaitFakeTimer();
415+
416+
expect(document.querySelector('.rc-trigger-popup')).toBeTruthy();
417+
expect(document.querySelector('.rc-trigger-popup').className).not.toContain(
418+
'-hidden',
419+
);
420+
421+
// Unmount the trigger while the popup is open
422+
rerender(<Demo show={false} />);
423+
await awaitFakeTimer();
424+
425+
// Popup should be hidden
426+
expect(document.querySelector('.rc-trigger-popup').className).toContain(
427+
'-hidden',
428+
);
429+
});
430+
431+
it('should not hide popup when an inactive trigger is unmounted', async () => {
432+
const Demo = ({ showSecond }: { showSecond: boolean }) => (
433+
<UniqueProvider>
434+
<Trigger
435+
action={['hover']}
436+
popup={<strong className="x-content">tooltip1</strong>}
437+
unique
438+
mouseEnterDelay={0.1}
439+
mouseLeaveDelay={0.1}
440+
>
441+
<div className="target1">hover1</div>
442+
</Trigger>
443+
{showSecond && (
444+
<Trigger
445+
action={['hover']}
446+
popup={<strong className="x-content">tooltip2</strong>}
447+
unique
448+
>
449+
<div className="target2">hover2</div>
450+
</Trigger>
451+
)}
452+
</UniqueProvider>
453+
);
454+
455+
const { container, rerender } = render(<Demo showSecond />);
456+
457+
// Hover first trigger to open the popup
458+
fireEvent.mouseEnter(container.querySelector('.target1'));
459+
await awaitFakeTimer();
460+
expect(document.querySelector('.x-content').textContent).toBe('tooltip1');
461+
expect(document.querySelector('.rc-trigger-popup').className).not.toContain(
462+
'-hidden',
463+
);
464+
465+
// Unmount the second (inactive) trigger, popup should keep open
466+
rerender(<Demo showSecond={false} />);
467+
await awaitFakeTimer();
468+
469+
expect(document.querySelector('.rc-trigger-popup').className).not.toContain(
470+
'-hidden',
471+
);
472+
});
392473
});

0 commit comments

Comments
 (0)