简体   繁体   English

使用ReactDOM createPortal为react-transition-group设置动画

[英]Animating react-transition-group with ReactDOM createPortal

I have been breaking my head trying to figure out how to accomplish something that I figured would be quite simple (I know...) 我一直在试图弄清楚如何完成我认为很简单的事情(我知道......)

Goal is for a very minimal , and reusable modal component that I can animate in some sort of HOC with a trigger button or w/e. 目标是一个非常小的可重复使用的模态组件,我可以使用触发按钮或w / e在某种HOC中制作动画。

I am creating it using createPortal, and the goal is to have some simple animations be added to on enter/ exit of said portal. 我正在使用createPortal创建它,目标是在进入/ 退出所述门户时添加一些简单的动画。

I have gotten this to work using GSAP, but ideally I would love for this to go along with SC instead so that I don't have to pull in another animation library. 我已经使用GSAP来实现这一点,但理想情况下我会喜欢这样与SC一起使用,这样我就不必再引入另一个动画库了。

For the life of me I just cannot get this to work with SC and would love if someone could point me in the right direction. 对于我的生活,我只是无法与SC一起工作,如果有人能指出我正确的方向,我会很高兴。

I have made a sandbox here: https://codesandbox.io/s/r44w9m4o5p using GSAP to run the animations, and it's a bit hacky but it is in the proper direction for what I am going for. 我在这里制作了一个沙盒: https//codesandbox.io/s/r44w9m4o5p使用GSAP来运行动画,它有点hacky但是它正朝着我想要的方向前进。

Also, is there a benefit to using react-transition-group over something like https://github.com/react-tools/react-move ? 另外,使用react-transition-group比https://github.com/react-tools/react-move更有用吗?

You can use the CSSTransition component instead of Transition . 您可以使用CSSTransition组件而不是Transition CSSTransition will just toggle approriate class names using given animation timings: .*-enter , .*-enter-active for in-transition and .*-exit , .*-exit-active for out-transition. CSSTransition将使用给定的动画时序切换适当的类名: .*-enter.*-enter-active用于转换的.*-enter-active.*-exit.*-exit-active用于输出转换的.*-exit-active So you can define all transitions using CSS3 properties with styled-components . 因此,您可以使用带有styled-components CSS3属性定义所有过渡。

Take a look at my fork: https://codesandbox.io/s/zz95v5103 看看我的前叉: https//codesandbox.io/s/zz95v5103

I've just extended your Modal with transition styles. 我刚刚使用过渡样式扩展了你的Modal Note that extending styles requires className property for the component to be extended , so I've added that property to your Modal component. 请注意,扩展样式需要扩展组件的className属性 ,因此我已将该属性添加到Modal组件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM