Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fuselage-toastbar): Pause ToastBar timer while hovering #1406

Merged
merged 14 commits into from
Jul 18, 2024
24 changes: 22 additions & 2 deletions packages/fuselage-toastbar/src/ToastBarTimed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ToastBar } from '@rocket.chat/fuselage';
import type { ReactElement } from 'react';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

import type { ToastBarPayload } from './ToastBarContext';
import { useToastBarDismiss } from './ToastBarContext';
Expand All @@ -13,17 +13,37 @@ const ToastBarTimed = ({
}: ToastBarPayload): ReactElement => {
const dismissToastMessage = useToastBarDismiss();

const [hovering, setHovering] = useState(false);

const [timedOut, setTimedOut] = useState(false);

useEffect(() => {
const timeOut = setTimeout(() => {
if (hovering) {
setTimedOut(true);
return;
}

dismissToastMessage(id);
}, time * 1000);

return () => clearTimeout(timeOut);
}, []);
}, [hovering]);

const onMouseLeave = () => {
if (timedOut) {
dismissToastMessage(id);
return;
}

setHovering(false);
};

return (
<ToastBar
variant={type}
onMouseEnter={() => setHovering(true)}
onMouseLeave={onMouseLeave}
children={message}
onClose={dismissToastMessage}
id={id}
Expand Down
Loading