我正在尝试创建一个模式,用于使用当您单击登录按钮时淡入并在您关闭它时淡出的掩码登录。我决定让我们使用“react-transition-group”库来实现它。淡入效果很好,但它会立即淡出,而没有应用动画。我怎样才能让它工作?
连接组件:
<div>
<ModalRoot {...this.props}>
<Grid>
<Menu {...this.props}/>
<div className='wrapper'>
<div className='content'>
<Form postTodo={this.props.postTodo}/>
<ul className='todo-list'>
<CSSTransitionGroup
transitionName='todo'
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
{todoItems}
</CSSTransitionGroup>
</ul>
</div>
</div>
</Grid>
</ModalRoot>
</div>
模态根:
const MODAL_COMPONENTS = {
LOGIN_MODAL: LoginModal,
/* other modals */
};
const ModalRoot = (props: any) => {
const {isVisible, isMaskShown, window} = props.modal;
const Modal = MODAL_COMPONENTS[window];
if(!isVisible) {
return (
<div>
{props.children}
</div>
);
}
if(isMaskShown) {
return (
<CSSTransitionGroup
transitionName='mask'
transitionAppear={true}
transitionAppearTimeout={500}
transitionEnterTimeout={0}
transitionLeaveTimeout={300}>
<Mask>
<Modal key={100} hideModal={props.hideModal} />
{props.children}
</Mask>
</CSSTransitionGroup>
);
}
if(!isMaskShown) {
return (
<div>
<Modal hideModal={props.hideModal}/>
{props.children}
</div>
);
}
};
登录模式:
const LoginModal = ({hideModal,presets}) => {
return (
<div className='login-modal'>
<button onClick={hideModal} className='close'>X</button>
</div>
);
};
CSS:
.mask {
position: absolute;
width: 100%;
height: 100%;
background: black;
opacity: 0.75;
}
.mask-leave {
opacity: 0.75;
}
.mask-leave.mask-leave-active {
opacity: 0;
transition: opacity 300ms ease-in;
}
.mask-appear {
opacity: 0;
}
.mask-appear.mask-appear-active {
opacity: 0.75;
transition: opacity .5s ease-in;
}