2022-06-22 14:57:09 +08:00
|
|
|
import type { CSSMotionProps, MotionEndEventHandler, MotionEventHandler } from 'rc-motion';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { MotionEvent } from 'rc-motion/lib/interface';
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
// ================== Collapse Motion ==================
|
2020-08-05 21:23:15 +08:00
|
|
|
const getCollapsedHeight: MotionEventHandler = () => ({ height: 0, opacity: 0 });
|
2022-11-19 13:47:33 +08:00
|
|
|
const getRealHeight: MotionEventHandler = (node) => {
|
2021-06-04 14:44:41 +08:00
|
|
|
const { scrollHeight } = node;
|
|
|
|
return { height: scrollHeight, opacity: 1 };
|
|
|
|
};
|
2022-11-19 13:47:33 +08:00
|
|
|
const getCurrentHeight: MotionEventHandler = (node) => ({ height: node ? node.offsetHeight : 0 });
|
2021-05-18 13:02:17 +08:00
|
|
|
const skipOpacityTransition: MotionEndEventHandler = (_, event: MotionEvent) =>
|
|
|
|
event?.deadline === true || (event as TransitionEvent).propertyName === 'height';
|
2020-07-01 15:41:55 +08:00
|
|
|
|
2022-07-19 22:53:38 +08:00
|
|
|
const initCollapseMotion = (rootCls: string = 'ant'): CSSMotionProps => ({
|
|
|
|
motionName: `${rootCls}-motion-collapse`,
|
2019-05-06 12:04:39 +08:00
|
|
|
onAppearStart: getCollapsedHeight,
|
|
|
|
onEnterStart: getCollapsedHeight,
|
|
|
|
onAppearActive: getRealHeight,
|
|
|
|
onEnterActive: getRealHeight,
|
|
|
|
onLeaveStart: getCurrentHeight,
|
|
|
|
onLeaveActive: getCollapsedHeight,
|
2020-07-01 15:41:55 +08:00
|
|
|
onAppearEnd: skipOpacityTransition,
|
|
|
|
onEnterEnd: skipOpacityTransition,
|
|
|
|
onLeaveEnd: skipOpacityTransition,
|
2020-04-25 11:13:14 +08:00
|
|
|
motionDeadline: 500,
|
2022-07-19 22:53:38 +08:00
|
|
|
});
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-12-13 14:57:40 +08:00
|
|
|
const SelectPlacements = ['bottomLeft', 'bottomRight', 'topLeft', 'topRight'] as const;
|
|
|
|
|
2022-01-20 16:54:47 +08:00
|
|
|
export type SelectCommonPlacement = typeof SelectPlacements[number];
|
|
|
|
|
2023-01-16 09:55:52 +08:00
|
|
|
const getTransitionDirection = (placement?: SelectCommonPlacement) => {
|
2022-01-20 16:54:47 +08:00
|
|
|
if (placement !== undefined && (placement === 'topLeft' || placement === 'topRight')) {
|
|
|
|
return `slide-down`;
|
|
|
|
}
|
|
|
|
return `slide-up`;
|
|
|
|
};
|
|
|
|
|
2021-02-08 17:09:13 +08:00
|
|
|
const getTransitionName = (rootPrefixCls: string, motion: string, transitionName?: string) => {
|
|
|
|
if (transitionName !== undefined) {
|
|
|
|
return transitionName;
|
|
|
|
}
|
|
|
|
return `${rootPrefixCls}-${motion}`;
|
|
|
|
};
|
2022-03-08 10:29:00 +08:00
|
|
|
|
2022-01-20 16:54:47 +08:00
|
|
|
export { getTransitionName, getTransitionDirection };
|
2022-07-19 22:53:38 +08:00
|
|
|
export default initCollapseMotion;
|