Skip to content

Commit 04e498b

Browse files
committed
fix: release unique popup ownership on trigger unmount
1 parent 3ff7d68 commit 04e498b

5 files changed

Lines changed: 362 additions & 19 deletions

File tree

‎src/UniqueProvider/index.tsx‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const UniqueProvider = ({
2525
children,
2626
postTriggerProps,
2727
}: UniqueProviderProps) => {
28-
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();
28+
const [trigger, open, options, onTargetVisibleChanged, releaseTarget] =
29+
useTargetState();
2930

3031
// ========================== Options ===========================
3132
const mergedOptions = React.useMemo(() => {
@@ -55,34 +56,55 @@ const UniqueProvider = ({
5556
});
5657

5758
// ========================== Register ==========================
58-
// Store the isOpen function from the latest show call
59-
const isOpenRef = React.useRef<(() => boolean) | null>(null);
59+
const ownerRef = React.useRef<string | null>(null);
60+
const delayedOwnerRef = React.useRef<string | null>(null);
61+
const isOpenRef = React.useRef(new Map<string, () => boolean>());
6062

6163
const delayInvoke = useDelay();
6264

6365
const show = useEvent(
6466
(showOptions: UniqueShowOptions, isOpen: () => boolean) => {
65-
// Store the isOpen function for later use in hide
66-
isOpenRef.current = isOpen;
67+
ownerRef.current = showOptions.id;
68+
isOpenRef.current.set(showOptions.id, isOpen);
6769

70+
delayedOwnerRef.current = showOptions.id;
6871
delayInvoke(() => {
72+
delayedOwnerRef.current = null;
6973
trigger(showOptions);
7074
}, showOptions.delay);
7175
},
7276
);
7377

74-
const hide = (delay: number) => {
78+
const hide = useEvent((id: string, delay: number) => {
79+
if (ownerRef.current !== id) {
80+
return;
81+
}
82+
83+
delayedOwnerRef.current = id;
7584
delayInvoke(() => {
76-
// Check if we should still hide by calling the isOpen function
77-
// If isOpen returns true, it means another trigger wants to keep it open
78-
if (isOpenRef.current?.()) {
79-
return; // Don't hide if something else wants it open
85+
delayedOwnerRef.current = null;
86+
// A controlled owner may still need to keep its popup open.
87+
if (ownerRef.current !== id || isOpenRef.current.get(id)?.()) {
88+
return;
8089
}
8190

8291
trigger(false);
8392
// Don't clear target, currentNode, options immediately, wait until animation completes
8493
}, delay);
85-
};
94+
});
95+
96+
const release = useEvent((id: string) => {
97+
// Another owner may already have a show or hide scheduled.
98+
if (delayedOwnerRef.current === id) {
99+
delayInvoke(() => {}, 0);
100+
delayedOwnerRef.current = null;
101+
}
102+
isOpenRef.current.delete(id);
103+
const remainingOwner = releaseTarget(id);
104+
if (ownerRef.current === id) {
105+
ownerRef.current = remainingOwner ?? null;
106+
}
107+
});
86108

87109
// Callback after animation completes
88110
const onVisibleChanged = useEvent((visible: boolean) => {
@@ -142,8 +164,9 @@ const UniqueProvider = ({
142164
() => ({
143165
show,
144166
hide,
167+
release,
145168
}),
146-
[],
169+
[show, hide, release],
147170
);
148171

149172
// =========================== Align ============================

‎src/UniqueProvider/useTargetState.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,34 @@ export default function useTargetState(): [
1717
/* Will always cache last which is not null */
1818
cacheOptions: UniqueShowOptions | null,
1919
onVisibleChanged: (visible: boolean) => void,
20+
release: (id: string) => string | undefined,
2021
] {
2122
const [options, setOptions] = React.useState<UniqueShowOptions | null>(null);
2223
const [open, setOpen] = React.useState(false);
2324
const [isAnimating, setIsAnimating] = React.useState(false);
25+
// Unlike cached options, ownership must update between batched cleanups.
26+
const currentOptionsRef = React.useRef<UniqueShowOptions | null>(null);
2427
const pendingOptionsRef = React.useRef<UniqueShowOptions | null>(null);
2528

29+
const updateOptions = (nextOptions: UniqueShowOptions) => {
30+
currentOptionsRef.current = nextOptions;
31+
setOptions(nextOptions);
32+
};
33+
2634
const trigger = useEvent((nextOptions: UniqueShowOptions | false) => {
2735
if (nextOptions === false) {
2836
// Clear pending options when hiding
2937
pendingOptionsRef.current = null;
38+
currentOptionsRef.current = null;
3039
setOpen(false);
3140
} else {
32-
if (isAnimating && open) {
41+
if (isAnimating && open && currentOptionsRef.current) {
3342
// If animating (appear or enter), cache new options
3443
pendingOptionsRef.current = nextOptions;
3544
} else {
3645
setOpen(true);
3746
// Set new options
38-
setOptions(nextOptions);
47+
updateOptions(nextOptions);
3948
pendingOptionsRef.current = null;
4049

4150
// Only mark as animating when transitioning from closed to open
@@ -52,7 +61,7 @@ export default function useTargetState(): [
5261
setIsAnimating(false);
5362
if (pendingOptionsRef.current) {
5463
// Apply pending options
55-
setOptions(pendingOptionsRef.current);
64+
updateOptions(pendingOptionsRef.current);
5665
pendingOptionsRef.current = null;
5766
}
5867
} else {
@@ -62,5 +71,25 @@ export default function useTargetState(): [
6271
}
6372
});
6473

65-
return [trigger, open, options, onVisibleChanged];
74+
const release = useEvent((id: string) => {
75+
if (pendingOptionsRef.current?.id === id) {
76+
pendingOptionsRef.current = null;
77+
}
78+
79+
if (currentOptionsRef.current?.id === id) {
80+
const nextOptions = pendingOptionsRef.current;
81+
pendingOptionsRef.current = null;
82+
if (nextOptions) {
83+
updateOptions(nextOptions);
84+
} else {
85+
currentOptionsRef.current = null;
86+
setOpen(false);
87+
}
88+
return nextOptions?.id;
89+
}
90+
91+
return pendingOptionsRef.current?.id ?? currentOptionsRef.current?.id;
92+
});
93+
94+
return [trigger, open, options, onVisibleChanged, release];
6695
}

‎src/context.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export interface UniqueShowOptions {
4040

4141
export interface UniqueContextProps {
4242
show: (options: UniqueShowOptions, isOpen: () => boolean) => void;
43-
hide: (delay: number) => void;
43+
hide: (id: string, delay: number) => void;
44+
release: (id: string) => void;
4445
}
4546

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

‎src/index.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ export function generateTrigger(
364364
onEsc,
365365
}));
366366

367+
useLayoutEffect(() => {
368+
return () => {
369+
if (unique && !parentContext) {
370+
uniqueContext?.release(id);
371+
}
372+
};
373+
}, [uniqueContext, unique, parentContext, id]);
374+
367375
// Handle controlled state changes for UniqueProvider
368376
// Only sync to UniqueProvider when it's controlled mode
369377
// If there is a parentContext, don't call uniqueContext methods
@@ -378,7 +386,7 @@ export function generateTrigger(
378386
if (mergedOpen) {
379387
uniqueContext.show(getUniqueOptions(mouseEnterDelay), isOpen);
380388
} else {
381-
uniqueContext.hide(mouseLeaveDelay);
389+
uniqueContext.hide(id, mouseLeaveDelay);
382390
}
383391
}
384392
}, [mergedOpen, targetEle]);
@@ -415,7 +423,7 @@ export function generateTrigger(
415423
if (nextOpen) {
416424
uniqueContext.show(getUniqueOptions(delay), isOpen);
417425
} else {
418-
uniqueContext.hide(delay);
426+
uniqueContext.hide(id, delay);
419427
}
420428
return;
421429
}

0 commit comments

Comments
 (0)