( ref: RefObject<HTMLElement | null>, isActive: boolean, onEnd: () => void )
| 81 | } |
| 82 | |
| 83 | function useAnimation( |
| 84 | ref: RefObject<HTMLElement | null>, |
| 85 | isActive: boolean, |
| 86 | onEnd: () => void |
| 87 | ): void { |
| 88 | useLayoutEffect(() => { |
| 89 | if (isActive && ref.current) { |
| 90 | if (!('getAnimations' in ref.current)) { |
| 91 | // JSDOM |
| 92 | onEnd(); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | let animations = ref.current.getAnimations(); |
| 97 | if (animations.length === 0) { |
| 98 | onEnd(); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | let canceled = false; |
| 103 | Promise.allSettled(animations.map(a => a.finished)).then(() => { |
| 104 | if (!canceled) { |
| 105 | flushSync(() => { |
| 106 | onEnd(); |
| 107 | }); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | return () => { |
| 112 | canceled = true; |
| 113 | }; |
| 114 | } |
| 115 | }, [ref, isActive, onEnd]); |
| 116 | } |
no outgoing calls
no test coverage detected