试试这个 DialogAnimation.jsx:
import * as React from 'react';
import Transition from 'react-transition-group/Transition';
// default css props
const defaultStyle = {
transform: 'translate3d(0, -20px, 0)',
opacity: 0,
};
// custom css props in 4 state: entering, entered, exiting, exited
const styles = {
entered: {
opacity: 1,
transform: 'translate3d(0, 0, 0)',
},
};
class DialogAnimation extends React.Component {
static defaultProps = {
in: false,
duration: 300
};
render() {
const {
children,
style: styleProp,
duration,
...other
} = this.props;
const style = {
...styleProp,
...(React.isValidElement(children)
? (children.props).style
: {})
};
return (
<Transition timeout={duration} appear={true} {...other}>
{(state, childProps) => {
return React.cloneElement(children, {
style: {
...defaultStyle,
transition: 'all ' + duration + 'ms ease',
transitionProperty: 'transform, opacity',
willChange: 'transform, opacity',
...styles[state],
...style
},
...childProps
});
}}
</Transition>
);
}
}
export default DialogAnimation;
您可以在 Dialog TransitionComponent 属性中使用它:
function Transition(props) {
return <DialogAnimation {...props} />;
}
<Dialog
TransitionComponent={Transition}
>