Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfabris committed Jul 18, 2024
1 parent 76d8ea2 commit da52ca4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
29 changes: 9 additions & 20 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, useState } from 'react';
import { type ReactElement } from 'react';
import { useCountdown } from 'react-timing-hooks';

import type { ToastBarPayload } from './ToastBarContext';
import { useToastBarDismiss } from './ToastBarContext';
Expand All @@ -11,34 +11,23 @@ const ToastBarTimed = ({
id,
message,
}: ToastBarPayload): ReactElement => {
const timeInSeconds = time * 1000;
const [shouldPause, setShouldPause] = useState(false);
const dismissToastMessage = useToastBarDismiss();

useEffect(() => {
const initialTime = Date.now();
const interval = setInterval(() => {
if (shouldPause) {
return;
}

if (Date.now() - initialTime >= timeInSeconds) {
dismissToastMessage(id);
}
}, timeInSeconds);

return () => clearInterval(interval);
}, [shouldPause]);
const [, { isPaused, pause, resume }] = useCountdown(time, 0, {
onEnd: () => dismissToastMessage(id),
startOnMount: true,
});

return (
<ToastBar
variant={type}
onMouseEnter={() => setShouldPause(true)}
onMouseLeave={() => setShouldPause(false)}
onPointerEnter={() => pause()}
onPointerLeave={() => resume()}
children={message}
onClose={dismissToastMessage}
id={id}
time={time}
isPaused={isPaused}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,4 @@ $toastbar-progressbar-background-color: theme(
background-color: $toastbar-progressbar-background-color;
}
}

&:hover &_progressbar::after {
animation-play-state: paused;
}
}
3 changes: 3 additions & 0 deletions packages/fuselage/src/components/ToastBar/ToastBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ToastBarProps = {
className?: string;
children?: ReactNode;
time?: number;
isPaused?: boolean;
id?: string;
onClose?: (id: string) => void;
buttonLabel?: string;
Expand All @@ -22,6 +23,7 @@ export function ToastBar({
className = '',
variant = 'info',
time = 5,
isPaused,
id,
onClose,
buttonLabel = 'Dismiss alert',
Expand Down Expand Up @@ -60,6 +62,7 @@ export function ToastBar({
width: 0%;
animation: ${progressBar} ${time}s;
animation-fill-mode: forwards;
animation-play-state: ${isPaused ? 'paused' : 'running'};
}
`;

Expand Down

0 comments on commit da52ca4

Please sign in to comment.