Skip to content

Commit 91b66bd

Browse files
committed
docs: update demo
1 parent a0319b9 commit 91b66bd

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

docs/examples/two-buttons.tsx

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,17 @@ const builtinPlacements = {
2525

2626
const MovingPopupDemo = () => {
2727
const [useUniqueProvider, setUseUniqueProvider] = useState(true);
28+
const [triggerControl, setTriggerControl] = useState('none'); // 'button1', 'button2', 'none'
2829

2930
const content = (
3031
<div style={{ margin: 100 }}>
31-
<div style={{ marginBottom: 20 }}>
32-
<label>
33-
<input
34-
type="checkbox"
35-
checked={useUniqueProvider}
36-
onChange={(e) => setUseUniqueProvider(e.target.checked)}
37-
/>
38-
使用 UniqueProvider
39-
</label>
40-
</div>
4132
<div style={{ display: 'flex', gap: 20 }}>
4233
<Trigger
4334
mouseLeaveDelay={LEAVE_DELAY}
4435
action={['hover']}
4536
popupPlacement="top"
4637
builtinPlacements={builtinPlacements}
38+
popupVisible={triggerControl === 'button1' || undefined}
4739
popupMotion={{
4840
motionName: 'rc-trigger-popup-zoom',
4941
}}
@@ -63,6 +55,7 @@ const MovingPopupDemo = () => {
6355
action={['hover']}
6456
popupPlacement="top"
6557
builtinPlacements={builtinPlacements}
58+
popupVisible={triggerControl === 'button2' || undefined}
6659
popupMotion={{
6760
motionName: 'rc-trigger-popup-zoom',
6861
}}
@@ -77,6 +70,51 @@ const MovingPopupDemo = () => {
7770
<button type="button">Right Button</button>
7871
</Trigger>
7972
</div>
73+
74+
<div style={{ marginBottom: 20 }}>
75+
<label>
76+
<input
77+
type="checkbox"
78+
checked={useUniqueProvider}
79+
onChange={(e) => setUseUniqueProvider(e.target.checked)}
80+
/>
81+
使用 UniqueProvider
82+
</label>
83+
</div>
84+
85+
<div style={{ marginBottom: 20 }}>
86+
<div>Trigger 控制:</div>
87+
<label>
88+
<input
89+
type="radio"
90+
name="triggerControl"
91+
value="button1"
92+
checked={triggerControl === 'button1'}
93+
onChange={(e) => setTriggerControl(e.target.value)}
94+
/>
95+
Button 1 显示 Trigger
96+
</label>
97+
<label style={{ marginLeft: 20 }}>
98+
<input
99+
type="radio"
100+
name="triggerControl"
101+
value="button2"
102+
checked={triggerControl === 'button2'}
103+
onChange={(e) => setTriggerControl(e.target.value)}
104+
/>
105+
Button 2 显示 Trigger
106+
</label>
107+
<label style={{ marginLeft: 20 }}>
108+
<input
109+
type="radio"
110+
name="triggerControl"
111+
value="none"
112+
checked={triggerControl === 'none'}
113+
onChange={(e) => setTriggerControl(e.target.value)}
114+
/>
115+
都不受控 (Hover 控制)
116+
</label>
117+
</div>
80118
</div>
81119
);
82120

src/UniqueProvider/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ const UniqueProvider = ({ children }: UniqueProviderProps) => {
4747
const hide = (delay: number) => {
4848
delayInvoke(() => {
4949
trigger(false);
50-
// 不要立即清空 target, currentNode, options,等动画结束后再清空
50+
// Don't clear target, currentNode, options immediately, wait until animation completes
5151
}, delay);
5252
};
5353

54-
// 动画完成后的回调
54+
// Callback after animation completes
5555
const onVisibleChanged = useEvent((visible: boolean) => {
56-
// 调用 useTargetState 的回调来处理动画状态
56+
// Call useTargetState callback to handle animation state
5757
onTargetVisibleChanged(visible);
5858
// if (!visible) {
5959
// setTarget(null);

src/UniqueProvider/useTargetState.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,42 @@ export default function useTargetState(): [
2525

2626
const trigger = useEvent((nextOptions: UniqueShowOptions | false) => {
2727
if (nextOptions === false) {
28-
// 隐藏时清除待处理的选项
28+
// Clear pending options when hiding
2929
pendingOptionsRef.current = null;
3030
setOpen(false);
3131
} else {
3232
if (isAnimating && open) {
33-
// 如果正在动画中(appear enter),缓存新的 options
33+
// If animating (appear or enter), cache new options
3434
pendingOptionsRef.current = nextOptions;
3535
} else {
36-
// 没有动画或者是首次显示,直接应用
3736
setOpen(true);
37+
// Use functional update to ensure re-render is always triggered
3838
setOptions(nextOptions);
3939
pendingOptionsRef.current = null;
40+
41+
// Only mark as animating when transitioning from closed to open
42+
if (!open) {
43+
setIsAnimating(true);
44+
}
4045
}
4146
}
4247
});
4348

4449
const onVisibleChanged = useEvent((visible: boolean) => {
4550
if (visible) {
46-
// 动画进入完成,检查是否有待处理的选项
51+
// Animation enter completed, check if there are pending options
4752
setIsAnimating(false);
4853
if (pendingOptionsRef.current) {
49-
const pendingOptions = pendingOptionsRef.current;
54+
// Apply pending options - Use functional update to ensure re-render is triggered
55+
setOptions(pendingOptionsRef.current);
5056
pendingOptionsRef.current = null;
51-
// 应用待处理的选项
52-
setOptions(pendingOptions);
5357
}
5458
} else {
55-
// 动画离开完成
59+
// Animation leave completed
5660
setIsAnimating(false);
5761
pendingOptionsRef.current = null;
5862
}
5963
});
6064

61-
// 当开始显示时标记为动画中
62-
React.useEffect(() => {
63-
if (open && options) {
64-
setIsAnimating(true);
65-
}
66-
}, [open, options]);
67-
6865
return [trigger, open, options, onVisibleChanged];
6966
}

0 commit comments

Comments
 (0)