admin管理员组文章数量:1287891
I have a React ponent for an Error Message element ErrorMessage
that either returns a div element with child elements or returns null
so it gets removed from the DOM.
I was curious if there was a way with CSS/React to animate this element as it gets removed from the DOM? I can animate when it renders fine.
Here is a CodeSandBox to test it.
Here is my 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;
}
}
Below is my ErrorMessage ponent
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;
Which renders in the DOM as
<div class="ErrorMessages ErrorMessages--Centered"></div>
Any help would be greatly appreciated!
I have a React ponent for an Error Message element ErrorMessage
that either returns a div element with child elements or returns null
so it gets removed from the DOM.
I was curious if there was a way with CSS/React to animate this element as it gets removed from the DOM? I can animate when it renders fine.
Here is a CodeSandBox to test it.
Here is my 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;
}
}
Below is my ErrorMessage ponent
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;
Which renders in the DOM as
<div class="ErrorMessages ErrorMessages--Centered"></div>
Any help would be greatly appreciated!
Share Improve this question asked Mar 28, 2019 at 11:36 mcclosamcclosa 1,4559 gold badges34 silver badges78 bronze badges2 Answers
Reset to default 4You can setTimeout
and raise an animation flag when props.show
changes. Something like this:
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;
};
You won't be able to animate it without delaying the ponent unmounting - otherwise the ponent technically isn't there anymore and therefore you can't add any animation/transition on its leave.
Personally I think that react-transition-group handles the animating very well.
https://github./reactjs/react-transition-group
Then you could use something along the lines of:
<TransitionGroup>
{this.state.showError &&
<CSSTransition className='animate-error' timeout={{enter: 500, exit: 500}}>
<ErrorMesage ... />
</CSSTransition>
}
</TransitionGroup>
and control the states as follow:
.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 ponent */
}
The timeout enter and timeout exit should be equal to your animation/transition duration for enter-done and exit.
本文标签: javascriptAdd AnimationTransition to Element When Removed From DOMStack Overflow
版权声明:本文标题:javascript - Add AnimationTransition to Element When Removed From DOM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329843a2372704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论