ant-design/components/_util/openAnimation.tsx

55 lines
1.4 KiB
TypeScript
Raw Normal View History

/**
* Deprecated. We should replace the animation with pure react motion instead of modify style directly.
* If you are creating new component with animation, please use `./motion`.
*/
2016-03-30 16:27:14 +08:00
import cssAnimation from 'css-animation';
import raf from 'raf';
2015-08-25 11:11:21 +08:00
2017-11-22 12:06:49 +08:00
function animate(node: HTMLElement, show: boolean, done: () => void) {
let height: number;
let requestAnimationFrameId: number;
return cssAnimation(node, 'ant-motion-collapse-legacy', {
2016-03-30 16:27:14 +08:00
start() {
if (!show) {
node.style.height = `${node.offsetHeight}px`;
2017-11-22 12:06:49 +08:00
node.style.opacity = '1';
2016-03-30 16:27:14 +08:00
} else {
height = node.offsetHeight;
node.style.height = '0px';
2017-11-22 12:06:49 +08:00
node.style.opacity = '0';
2016-03-30 16:27:14 +08:00
}
},
active() {
2017-02-26 19:08:36 +08:00
if (requestAnimationFrameId) {
raf.cancel(requestAnimationFrameId);
2017-02-26 19:08:36 +08:00
}
requestAnimationFrameId = raf(() => {
node.style.height = `${show ? height : 0}px`;
2017-11-22 12:06:49 +08:00
node.style.opacity = show ? '1' : '0';
});
2016-03-30 16:27:14 +08:00
},
end() {
2017-02-26 19:08:36 +08:00
if (requestAnimationFrameId) {
raf.cancel(requestAnimationFrameId);
2017-02-26 19:08:36 +08:00
}
2016-03-30 16:27:14 +08:00
node.style.height = '';
node.style.opacity = '';
2015-08-25 11:11:21 +08:00
done();
2016-03-30 16:27:14 +08:00
},
2015-08-25 11:11:21 +08:00
});
}
const animation = {
2017-11-22 12:06:49 +08:00
enter(node: HTMLElement, done: () => void) {
2016-03-30 16:27:14 +08:00
return animate(node, true, done);
2015-08-25 11:11:21 +08:00
},
2017-11-22 12:06:49 +08:00
leave(node: HTMLElement, done: () => void) {
2016-03-30 16:27:14 +08:00
return animate(node, false, done);
2015-08-25 11:11:21 +08:00
},
2017-11-22 12:06:49 +08:00
appear(node: HTMLElement, done: () => void) {
2016-03-30 16:27:14 +08:00
return animate(node, true, done);
2015-08-25 11:11:21 +08:00
},
};
export default animation;