2021-05-21 21:53:53 +00:00
|
|
|
interface ElementAnimation {
|
2021-05-26 11:32:31 +00:00
|
|
|
keyframes: Keyframe[];
|
2021-05-21 21:53:53 +00:00
|
|
|
options?: KeyframeAnimationOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ElementAnimationMap {
|
|
|
|
[animationName: string]: ElementAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultAnimationRegistry = new Map<String, ElementAnimation>();
|
|
|
|
const customAnimationRegistry = new WeakMap<Element, ElementAnimationMap>();
|
|
|
|
|
2021-06-10 13:50:30 +00:00
|
|
|
function ensureAnimation(animation: ElementAnimation | null) {
|
|
|
|
return animation ?? { keyframes: [], options: { duration: 0 } };
|
|
|
|
}
|
|
|
|
|
2021-05-21 21:53:53 +00:00
|
|
|
//
|
|
|
|
// Sets a default animation. Components should use the `name.animation` for primary animations and `name.part.animation`
|
2021-05-26 11:32:31 +00:00
|
|
|
// for secondary animations, e.g. `dialog.show` and `dialog.overlay.show`. For modifiers, use `drawer.showTop`.
|
2021-05-21 21:53:53 +00:00
|
|
|
//
|
2021-06-10 13:50:30 +00:00
|
|
|
export function setDefaultAnimation(animationName: string, animation: ElementAnimation | null) {
|
|
|
|
defaultAnimationRegistry.set(animationName, ensureAnimation(animation));
|
2021-05-21 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sets a custom animation for the specified element.
|
|
|
|
//
|
2021-06-10 13:50:30 +00:00
|
|
|
export function setAnimation(el: Element, animationName: string, animation: ElementAnimation | null) {
|
2021-05-21 21:53:53 +00:00
|
|
|
customAnimationRegistry.set(
|
|
|
|
el,
|
|
|
|
Object.assign({}, customAnimationRegistry.get(el), {
|
2021-06-10 13:50:30 +00:00
|
|
|
[animationName]: ensureAnimation(animation)
|
2021-05-21 21:53:53 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-05-26 11:32:31 +00:00
|
|
|
// Gets an element's animation. Falls back to the default if no animation is found.
|
2021-05-21 21:53:53 +00:00
|
|
|
//
|
|
|
|
export function getAnimation(el: Element, animationName: string) {
|
|
|
|
const customAnimation = customAnimationRegistry.get(el);
|
|
|
|
|
|
|
|
// Check for a custom animation
|
|
|
|
if (customAnimation && customAnimation[animationName]) {
|
|
|
|
return customAnimation[animationName];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a default animation
|
|
|
|
const defaultAnimation = defaultAnimationRegistry.get(animationName);
|
|
|
|
if (defaultAnimation) {
|
|
|
|
return defaultAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to an empty animation
|
|
|
|
return {
|
|
|
|
keyframes: [],
|
|
|
|
options: { duration: 0 }
|
|
|
|
};
|
|
|
|
}
|