Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
// Behavior
duration = 4.5,
showProgress,
times,
hovering: forcedHovering,
pauseOnHover = true,

Expand All @@ -118,7 +119,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
// ======================== Duration ========================
const [hovering, setHovering] = React.useState(false);

const [onResume, onPause] = useNoticeTimer(duration, onInternalClose, setPercent);
const [onResume, onPause] = useNoticeTimer(duration, times, onInternalClose, setPercent);

const validPercent = 100 - Math.min(Math.max(percent * 100, 0), 100);
const Progress = components?.progress || DefaultProgress;
Expand Down
14 changes: 12 additions & 2 deletions src/hooks/useNoticeTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { raf, useEvent } from '@rc-component/util';
*/
export default function useNoticeTimer(
duration: number | false | null,
times: number | undefined,
onClose: VoidFunction,
onUpdate: (ptg: number) => void,
) {
Expand All @@ -16,6 +17,8 @@ export default function useNoticeTimer(
const onEventUpdate = useEvent(onUpdate);

const [walking, setWalking] = React.useState(durationMs > 0);
const walkingRef = React.useRef(walking);
walkingRef.current = walking;
const passTimeRef = React.useRef(0);
const lastRafTimeRef = React.useRef<number | null>(null);

Expand Down Expand Up @@ -43,7 +46,7 @@ export default function useNoticeTimer(
} else {
onEventUpdate(0);
}
}, [durationMs]);
}, [durationMs, onEventUpdate]);

// Reset when durationMs changed.
React.useEffect(() => {
Expand All @@ -52,6 +55,13 @@ export default function useNoticeTimer(
setWalking(durationMs > 0);
}, [durationMs]);

// Restart the timer when an existing notice is updated with the same key.
React.useEffect(() => {
passTimeRef.current = 0;
lastRafTimeRef.current = walkingRef.current ? Date.now() : null;
onEventUpdate(0);
}, [times, onEventUpdate]);

// Trigger update when walking changed.
React.useEffect(() => {
if (!walking || durationMs <= 0) {
Expand All @@ -77,7 +87,7 @@ export default function useNoticeTimer(
return () => {
raf.cancel(rafId!);
};
}, [durationMs, walking]);
}, [durationMs, times, walking, onEventClose, onEventUpdate]);

return [onResume, onPause] as const;
}
36 changes: 36 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ describe('Notification.Basic', () => {
unmount();
});

it('resets duration when updating a notification with the same key', () => {
const { instance } = renderDemo();

act(() => {
instance.open({
key: 'update',
description: <p className="test">first</p>,
duration: 1,
});
});

act(() => {
step(800);
});

act(() => {
instance.open({
key: 'update',
description: <p className="test">second</p>,
duration: 1,
});
});

act(() => {
step(300);
});

expect(document.querySelector('.test')).toHaveTextContent('second');

act(() => {
step(700);
});

expect(document.querySelector('.test')).toBeFalsy();
});

it('works with custom close icon', () => {
const { instance } = renderDemo();

Expand Down
6 changes: 3 additions & 3 deletions tests/useNoticeTimer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('useNoticeTimer', () => {
it('preserves the remaining duration when paused repeatedly', () => {
const onClose = vi.fn();
const onUpdate = vi.fn();
const { result, unmount } = renderHook(() => useNoticeTimer(1, onClose, onUpdate));
const { result, unmount } = renderHook(() => useNoticeTimer(1, undefined, onClose, onUpdate));

act(() => vi.advanceTimersByTime(200));
act(() => result.current[1]());
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('useNoticeTimer', () => {
const onClose = vi.fn();
const onUpdate = vi.fn();
const { rerender, unmount } = renderHook(
({ duration }) => useNoticeTimer(duration, onClose, onUpdate),
({ duration }) => useNoticeTimer(duration, undefined, onClose, onUpdate),
{
initialProps: { duration: 1 as number | false | null },
},
Expand All @@ -69,7 +69,7 @@ describe('useNoticeTimer', () => {
const onClose = vi.fn();
const onUpdate = vi.fn();
const { rerender, unmount } = renderHook(
({ duration }) => useNoticeTimer(duration, onClose, onUpdate),
({ duration }) => useNoticeTimer(duration, undefined, onClose, onUpdate),
{
initialProps: { duration: 1 as number | false | null },
},
Expand Down
Loading