Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Last active February 18, 2020 02:05
Show Gist options
  • Select an option

  • Save tonkotsuboy/858f6f5082210c1ad93c8ae118e3dc78 to your computer and use it in GitHub Desktop.

Select an option

Save tonkotsuboy/858f6f5082210c1ad93c8ae118e3dc78 to your computer and use it in GitHub Desktop.
waitTransitionendEvent
/**
* 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