将动画/过渡效果添加到从DOM中移除的元素

5
我有一个React组件用于错误消息元素ErrorMessage,它要么返回具有子元素的div元素,要么返回null,以便从DOM中删除。
我想知道是否有一种CSS/React的方法可以在该元素从DOM中删除时对其进行动画处理?当渲染正常时,我可以进行动画处理。
这是一个测试用的CodeSandBox
以下是我的SASS。
.ErrorMessages {
  background: #ee4900;
  color: #fff;
  border: 0;
  color: #fff;
  padding: 0.8em 1em;
  text-align: left;
  font-size: 12px;
  margin-top: 10px;
  margin-bottom: 10px;
  animation-duration: 0.5s;
  animation-fill-mode: both;
  animation-name: fadeInUp;
}

.ErrorMessages--Centered {
  padding-top: 30px;
  padding-right: 70px;
  padding-left: 70px;
}

.ErrorMessages--fadeOut {
  animation-name: fadeOutDown;
}

@keyframes fadeOutDown {
  from {
    opacity: 1;
    max-height: 72px;
  }

  to {
    opacity: 0;
    padding: 0;
    margin: 0;
    max-height: 0;
    transform: translate3d(0, 50px, 0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    padding: 0;
    margin: 0;
    max-height: 0;
    transform: translate3d(0, 50px, 0);
  }
  to {
    opacity: 1;
  }
}

以下是我的错误消息组件

import React from "react";

const ErrorMessage = props => {
  let err = props.show ? (
    <div className="ErrorMessages ErrorMessages--Centered">
      <h4>{props.errorHeader}</h4>
      <p>{props.errorMessage}</p>
    </div>
  ) : null;
  return err;
};

export default ErrorMessage;

这将在DOM中呈现为

<div class="ErrorMessages ErrorMessages--Centered"></div>

非常感谢您的帮助!

2个回答

3

props.show 发生变化时,您可以使用 setTimeout 来提高动画标志。例如:

const ErrorMessage = props => {
  const [show, setShow] = useState(props.show);
  const [hide, setHide] = useState(!props.show);
  const timeoutRef = useRef(null);

  useEffect(() => {
    if (props.show) {
      setShow(true);
      setHide(false);
      clearTimeout(timeoutRef.current);
    }
    else {
      setShow(false);
      timeoutRef.current = setTimeout(() => setHide(true), props.delay || 1000);
    }
  }, [props.show]);

  /* unmount cleanup */
  useEffect(() => () => clearTimeout(timeoutRef.current), []);

  let err = !hide ? (
    <div className={'ErrorMessages ErrorMessages--Centered' + (show ? '' : ' ErrorMessages--fadeOut')}>
      <h4>{props.errorHeader}</h4>
      <p>{props.errorMessage}</p>
    </div>
  ) : null;
  return err;
};

太完美了,谢谢!像魔法一样运行,我没想到使用超时! - mcclosa

2

如果不延迟组件卸载,您将无法对其进行动画处理 - 否则,该组件在技术上已经不存在,因此您无法在其离开时添加任何动画/过渡效果。

个人认为,react-transition-group 处理动画非常出色。

https://github.com/reactjs/react-transition-group

然后你可以使用类似以下的内容:

<TransitionGroup>
    {this.state.showError && 
    <CSSTransition className='animate-error' timeout={{enter: 500, exit: 500}}>
        <ErrorMesage ... />
    </CSSTransition>
    }
</TransitionGroup>

并按以下方式控制状态:

.animate-error-enter .ErrorMessages.ErrorMessages--Centered { 
    the styles for the initial state of your animation/transition before it enters
}

.animate-error-enter-done .ErrorMessages.ErrorMessages--Centered { 
    the styles you animate to on enter 
}

.animate-error-exit .ErrorMessages.ErrorMessage--Centered { 
    the styles you animate to on exit in order to hide the component 
}

超时输入和超时退出应该等于您的进入完成和退出的动画/转换持续时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接