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 abb2d8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
29 changes: 12 additions & 17 deletions packages/fuselage-toastbar/src/ToastBarTimed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,30 @@ const ToastBarTimed = ({

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

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

useEffect(() => {
const timeOut = setTimeout(() => {
if (hovering) {
setTimedOut(true);
let elapsed = 0;

const interval = setInterval(() => {
if (elapsed >= visibleFor) {
dismissToastMessage(id);
return;
}

dismissToastMessage(id);
}, time * 1000);
if (!hovering) {
elapsed += 10;
}
}, 10);

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 abb2d8e

Please sign in to comment.