({
autoEndAnimation = true,
outAnimationDuration = OUT_ANIMATION_DURATION,
onAnimationEnd,
}: UseTimedAnimationProps)
| 22 | const MANUAL_DISMISS_ANIMATION_ID = 1; |
| 23 | |
| 24 | export const useTimedAnimation = ({ |
| 25 | autoEndAnimation = true, |
| 26 | outAnimationDuration = OUT_ANIMATION_DURATION, |
| 27 | onAnimationEnd, |
| 28 | }: UseTimedAnimationProps): UseTimedAnimation => { |
| 29 | const [timer, setTimer] = useState(0); |
| 30 | const interval = useRef<number>(); |
| 31 | const hasStartedAnimation = useRef(false); |
| 32 | const [animationEnd] = useDebounceFn( |
| 33 | onAnimationEnd ?? (() => undefined), |
| 34 | outAnimationDuration, |
| 35 | ); |
| 36 | |
| 37 | const clearInterval = () => { |
| 38 | if (!interval?.current) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | window.clearInterval(interval.current); |
| 43 | interval.current = undefined; |
| 44 | }; |
| 45 | |
| 46 | const endAnimation = useCallback(() => { |
| 47 | // Guard on hasStartedAnimation (not interval.current): hover-pause clears |
| 48 | // the interval, so an interval check would make dismiss/undo a no-op while |
| 49 | // the pointer is over the toast. A started-but-paused animation must still |
| 50 | // be endable. |
| 51 | if (!hasStartedAnimation.current) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | setTimer(0); |
| 56 | }, []); |
| 57 | |
| 58 | const runTick = useCallback(() => { |
| 59 | interval.current = window.setInterval( |
| 60 | () => |
| 61 | setTimer((current) => |
| 62 | PROGRESS_INTERVAL >= current ? 0 : current - PROGRESS_INTERVAL, |
| 63 | ), |
| 64 | PROGRESS_INTERVAL, |
| 65 | ); |
| 66 | }, []); |
| 67 | |
| 68 | const startAnimation = useCallback( |
| 69 | (duration: number) => { |
| 70 | if (interval.current) { |
| 71 | clearInterval(); |
| 72 | } |
| 73 | |
| 74 | setTimer(duration); |
| 75 | |
| 76 | if (isNullOrUndefined(duration) || duration <= 0) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | hasStartedAnimation.current = true; |
| 81 |
no test coverage detected