Skip to content

Commit

Permalink
try using setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
debdutdeb committed Jun 25, 2024
1 parent 9b393b0 commit 4329652
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
26 changes: 10 additions & 16 deletions packages/fuselage-toastbar/src/ToastBarTimed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,29 @@ const ToastBarTimed = ({

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

const [timedOut, setTimedOut] = useState(false);
const visibleFor = time * 1000;

useEffect(() => {
const timeOut = setTimeout(() => {
const start = Date.now();

const interval = setInterval(() => {
if (hovering) {
setTimedOut(true);
return;
}

dismissToastMessage(id);
}, time * 1000);
if (Date.now() - start >= visibleFor) {
dismissToastMessage(id);
}
}, 100);

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

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

setHovering(false);
};

return (
<ToastBar
variant={type}
onMouseEnter={() => setHovering(true)}
onMouseLeave={onMouseLeave}
onMouseLeave={() => setHovering(false)}
children={message}
onClose={dismissToastMessage}
id={id}
Expand Down
1 change: 1 addition & 0 deletions packages/fuselage/src/components/ToastBar/ToastBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function ToastBar({
&::after {
width: 0%;
animation: ${progressBar} ${time}s;
animation-fill-mode: forwards;
}
`;

Expand Down

0 comments on commit 4329652

Please sign in to comment.