Last active
February 18, 2020 02:05
-
-
Save tonkotsuboy/858f6f5082210c1ad93c8ae118e3dc78 to your computer and use it in GitHub Desktop.
waitTransitionendEvent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Elementのtransitionendイベントが発火すると解決するPromiseです。 | |
| * propertyNameを指定した場合は、該当propertyのtransitionendのみを待ちます。 | |
| * @param element | |
| * @param propertyName | |
| */ | |
| export const waitTransitionendEvent = async ( | |
| element: Element | null, | |
| propertyName?: keyof CSSStyleDeclaration | |
| ): Promise<void> => { | |
| if (element == null) { | |
| return; | |
| } | |
| new Promise(resolve => { | |
| const transitionEndListener = (event: Event | TransitionEvent) => { | |
| if ( | |
| propertyName != null && | |
| "propertyName" in event && | |
| propertyName !== event.propertyName | |
| ) { | |
| return; | |
| } | |
| element.removeEventListener("transitionend", transitionEndListener); | |
| resolve(); | |
| }; | |
| element.addEventListener("transitionend", transitionEndListener, { | |
| once: true | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment