游水上岸 發表於 2023-10-11 11:00:00

React跨路由组件动画

<blockquote>
<p>我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品。我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值。</p>
</blockquote>
<blockquote>
<p>本文作者:佳岚</p>
</blockquote>
<h3 id="回顾传统react动画">回顾传统React动画</h3>
<p>对于普通的 React 动画,我们大多使用官方推荐的 <strong>react-transition-group</strong>,其提供了四个基本组件 <strong>Transition、CSSTransition、SwitchTransition、TransitionGroup</strong></p>
<h4 id="transition"><strong>Transition</strong></h4>
<p>Transition 组件允许您使用简单的声明式 API 来描述组件的状态变化,默认情况下,Transition 组件不会改变它呈现的组件的行为,它只跟踪组件的“进入”和“退出”状态,我们需要做的是赋予这些状态意义。</p>
<p>其一共提供了四种状态,当组件感知到 <strong>in</strong> prop 变化时就会进行相应的状态过渡</p>
<ul>
<li><code>'entering'</code></li>
<li><code>'entered'</code></li>
<li><code>'exiting'</code></li>
<li><code>'exited'</code></li>
</ul>
<pre><code class="language-jsx">const defaultStyle = {
transition: `opacity ${duration}ms ease-in-out`,
opacity: 0,
}

const transitionStyles = {
entering: { opacity: 1 },
entered:{ opacity: 1 },
exiting:{ opacity: 0 },
exited:{ opacity: 0 },
};

const Fade = ({ in: inProp }) =&gt; (
&lt;Transition in={inProp} timeout={duration}&gt;
    {state =&gt; (
      &lt;div style={{
      ...defaultStyle,
      ...transitionStyles
      }}&gt;
      I'm a fade Transition!
      &lt;/div&gt;
    )}
&lt;/Transition&gt;
);
</code></pre>
<h4 id="csstransition">CSSTransition</h4>
<p>此组件主要用来做 CSS 样式过渡,它能够在组件各个状态变化的时候给我们要过渡的标签添加上不同的类名。所以参数和平时的 className 不同,参数为:<code>classNames</code></p>
<pre><code class="language-jsx">&lt;CSSTransition
in={inProp}
timeout={300}
classNames="fade"
unmountOnExit
&gt;
&lt;div className="star"&gt;⭐&lt;/div&gt;
&lt;/CSSTransition&gt;

// 定义过渡样式类
.fade-enter {
opacity: 0;
}
.fade-enter-active {
opacity: 1;
transition: opacity 200ms;
}
.fade-exit {
opacity: 1;
}
.fade-exit-active {
opacity: 0;
transition: opacity 200ms;
}
</code></pre>
<h4 id="switchtransition">SwitchTransition</h4>
<p>SwitchTransition 用来做组件切换时的过渡,其会缓存传入的 children,并在过渡结束后渲染新的 children</p>
<pre><code class="language-tsx">function App() {
const = useState(false);
return (
   &lt;SwitchTransition&gt;
   &lt;CSSTransition
       key={state ? "Goodbye, world!" : "Hello, world!"}
       classNames='fade'
   &gt;
       &lt;button onClick={() =&gt; setState(state =&gt; !state)}&gt;
         {state ? "Goodbye, world!" : "Hello, world!"}
       &lt;/button&gt;
   &lt;/CSSTransition&gt;
   &lt;/SwitchTransition&gt;
);
}
</code></pre>
<h4 id="transitiongroup">TransitionGroup</h4>
<p>如果有一组 CSSTransition 需要我们去过渡,那么我们需要管理每一个 CSSTransition 的 in 状态,这样会很麻烦。</p>
<p>TransitionGroup 可以帮我们管理一组 Transition 或 CSSTransition 组件,为此我们不再需要给 Transition 组件传入 in 属性来标识过渡状态,转用 key 属性来代替 in</p>
<pre><code class="language-jsx">&lt;TransitionGroup&gt;
{
      this.state.list.map((item, index) =&gt; {
          return (
            &lt;CSSTransition
                key = {item.id}
                timeout= {1000}
                classNames = 'fade'
                unmountOnExit
            &gt;
                &lt;TodoItem /&gt;
            &lt;/CSSTransition&gt;
          )
      }
}
&lt;/TransitionGroup&gt;
</code></pre>
<p>TransitionGroup 会监测其 children 的变化,将新的 children 与原有的 children 使用 key 进行比较,就能得出哪些 children 是新增的与删除的,从而为他们注入进场动画或离场动画。</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105955384-818332873.png"></p>
<h3 id="flip-动画">FLIP 动画</h3>
<p><strong>FLIP</strong> 是什么?</p>
<p><strong>FLIP</strong> 是 <code>First</code>、<code>Last</code>、<code>Invert</code>和 <code>Play</code>四个单词首字母的缩写</p>
<p><code>First</code>, 元素过渡前开始位置信息</p>
<p><code>Last</code>:执行一段代码,使元素位置发生变化,记录最后状态的位置等信息.</p>
<p><code>Invert</code>:根据 First 和 Last 的位置信息,计算出位置差值,使用 transform: translate(x,y) 将元素移动到First的位置。</p>
<p><code>Play</code>: &nbsp;给元素加上 transition 过渡属性,再讲 transform 置为 none,这时候因为 transition 的存在,开始播放丝滑的动画。</p>
<blockquote>
<p>Flip 动画可以看成是一种编写动画的范式,方法论,对于开始或结束状态未知的复杂动画,可以使用 Flip 快速实现</p>
</blockquote>
<p>位置过渡效果</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105955666-332532584.gif"></p>
<p>代码实现:</p>
<pre><code class="language-javascript">const container = document.querySelector('.flip-container');
const btnAdd = document.querySelector('#add-btn')
const btnDelete = document.querySelector('#delete-btn')
let rectList = []

function addItem() {
    const el = document.createElement('div')
    el.className = 'flip-item'
    el.innerText = rectList.length + 1;
    el.style.width = (Math.random() * 300 + 100) + 'px'

    // 加入新元素前重新记录起始位置信息
    recordFirst();

    // 加入新元素
    container.prepend(el)
    rectList.unshift({
      top: undefined,
      left: undefined
    })
   
    // 触发FLIP
    update()
}

function removeItem() {
    const children = container.children;
    if (children.length &gt; 0) {
      recordFirst();
      container.removeChild(children)
      rectList.shift()
      update()
    }
}

// 记录位置
function recordFirst() {
    const items = container.children;
    for (let i = 0; i &lt; items.length; i++) {
      const rect = items.getBoundingClientRect();
      rectList = {
      left: rect.left,
      top: rect.top
      }
    }
}

function update() {
    const items = container.children;
    for (let i = 0; i &lt; items.length; i++) {
      // Last
      const rect = items.getBoundingClientRect();
      if (rectList.left !== undefined) {

       // Invert
      const transformX = rectList.left - rect.left;
      const transformY = rectList.top - rect.top;

      items.style.transform = `translate(${transformX}px, ${transformY}px)`
      items.style.transition = "none"

      // Play
      requestAnimationFrame(() =&gt; {
          items.style.transform = `none`
          items.style.transition = "all .5s"
      })
      }
    }
}

btnAdd.addEventListener('click', () =&gt; {
    addItem()
})
btnDelete.addEventListener('click', () =&gt; {
    removeItem()
})
</code></pre>
<p>使用 flip 实现的动画 demo</p>
<p>乱序动画:</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105955973-1990733514.gif"></p>
<p>缩放动画:</p>
<p><img src="https://img2023.cnblogs.com/blog/2332333/202310/2332333-20231011110717467-1589571221.webp"></p>
<h3 id="react跨路由组件动画">React跨路由组件动画</h3>
<p>在 React 中路由之前的切换动画可以使用 react-transition-group 来实现,但对于不同路由上的组件如何做到动画过渡是个很大的难题,目前社区中也没有一个成熟的方案。</p>
<p><img src="https://img2023.cnblogs.com/blog/2332333/202310/2332333-20231011110730382-1455526644.gif"></p>
<h4 id="使用flip来实现">使用flip来实现</h4>
<p>在路由 A 中组件的大小与位置状态可以当成 <strong>First</strong>, 在路由 B 中组件的大小与位置状态可以当成 <strong>Last</strong>,</p>
<p>从路由 A 切换至路由B时,向 B 页面传递 First 状态,B 页面中需要过渡的组件再进行 Flip 动画。</p>
<p>为此我们可以抽象出一个组件来帮我们实现 Flip 动画,并且能够在切换路由时保存组件的状态。</p>
<p>对需要进行过渡的组件进行包裹, 使用相同的 <strong>flipId</strong> 来标识他们需要在不同的路由中过渡。</p>
<pre><code class="language-jsx">&lt;FlipRouteAnimate className="about-profile" flipId="avatar" animateStyle={{ borderRadius: "15px" }}&gt;
&lt;img src={require("./touxiang.jpg")} alt="" /&gt;
&lt;/FlipRouteAnimate&gt;
</code></pre>
<p>完整代码:</p>
<pre><code class="language-jsx">import React, { createRef } from "react";
import withRouter from "./utils/withRouter";
class FlipRouteAnimate extends React.Component {
constructor(props) {
    super(props);
    this.flipRef = createRef();
}
// 用来存放所有实例的rect
static flipRectMap = new Map();
componentDidMount() {
    const {
      flipId,
      location: { pathname },
      animateStyle: lastAnimateStyle,
    } = this.props;

    const lastEl = this.flipRef.current;

    // 没有上一个路由中组件的rect,说明不用进行动画过渡
    if (!FlipRouteAnimate.flipRectMap.has(flipId) || flipId === undefined) return;

    // 读取缓存的rect
    const first = FlipRouteAnimate.flipRectMap.get(flipId);
    if (first.route === pathname) return;

    // 开始FLIP动画
    const firstRect = first.rect;
    const lastRect = lastEl.getBoundingClientRect();

    const transformOffsetX = firstRect.left - lastRect.left;
    const transformOffsetY = firstRect.top - lastRect.top;

    const scaleRatioX = firstRect.width / lastRect.width;
    const scaleRatioY = firstRect.height / lastRect.height;

    lastEl.style.transform = `translate(${transformOffsetX}px, ${transformOffsetY}px) scale(${scaleRatioX}, ${scaleRatioY})`;
    lastEl.style.transformOrigin = "left top";

    for (const styleName in first.animateStyle) {
      lastEl.style = first.animateStyle;
    }

    setTimeout(() =&gt; {
      lastEl.style.transition = "all 2s";
      lastEl.style.transform = `translate(0, 0) scale(1)`;
      // 可能有其他属性也需要过渡
      for (const styleName in lastAnimateStyle) {
      lastEl.style = lastAnimateStyle;
      }
    }, 0);
}

componentWillUnmount() {
    const {
      flipId,
      location: { pathname },
      animateStyle = {},
    } = this.props;
    const el = this.flipRef.current;
    // 组件卸载时保存自己的位置等状态
    const rect = el.getBoundingClientRect();

    FlipRouteAnimate.flipRectMap.set(flipId, {
      // 当前路由路径
      route: pathname,
      // 组件的大小位置
      rect: rect,
      // 其他需要过渡的样式
      animateStyle,
    });
}
render() {
    return (
      &lt;div
      className={this.props.className}
      style={{ display: "inline-block", ...this.props.style, ...this.props.animateStyle }}
      ref={this.flipRef}
      &gt;
      {this.props.children}
      &lt;/div&gt;
    );
}
}
</code></pre>
<p>实现效果:</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105957168-2068351573.gif"></p>
<h4 id="共享组件的方式实现">共享组件的方式实现</h4>
<p>要想在不同的路由共用同一个组件实例,并不现实,树形的 Dom 树并不允许我们这么做。</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105957544-1012386823.png"></p>
<p>我们可以换个思路,把组件提取到路由容器的外部,然后通过某种方式将该组件与路由页面相关联。</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105957750-1241651191.png"></p>
<p>我们将 Float 组件提升至根组件,然后在每个路由中使用 Proxy 组件进行占位,当路由切换时,每个 Proxy 将其位置信息与其他 props 传递给 Float 组件,Float 组件再根据接收到的状态信息,将自己移动到对应位置。</p>
<p>我们先封装一个 Proxy 组件, &nbsp;使用 PubSub 发布元信息。</p>
<pre><code class="language-tsx">// FloatProxy.tsx
const FloatProxy: React.FC&lt;any&gt; = (props: any) =&gt; {
const el = useRef();

// 保存代理元素引用,方便获取元素的位置信息
useEffect(() =&gt; {
    PubSub.publish("proxyElChange", el);
    return () =&gt; {
      PubSub.publish("proxyElChange", null);
    }
}, []);

useEffect(() =&gt; {
    PubSub.publish("metadataChange", props);
}, );

const computedStyle = useMemo(() =&gt; {
    const propStyle = props.style || {};
    return {
      border: "dashed 1px #888",
      transition: "all .2s ease-in",
      ...propStyle,
    };
}, );

return &lt;div {...props} style={computedStyle} ref={el}&gt;&lt;/div&gt;;
};
</code></pre>
<p>在路由中使用, 将样式信息进行传递</p>
<pre><code class="language-tsx">class Bar extends React.Component {
render() {
    return (
      &lt;div className="container"&gt;
      &lt;p&gt;bar&lt;/p&gt;
      &lt;div style={{ marginTop: "140px" }}&gt;
          &lt;FloatProxy style={{ width: 120, height: 120, borderRadius: 15, overflow: "hidden" }} /&gt;
      &lt;/div&gt;
      &lt;/div&gt;
    );
}
}
</code></pre>
<p>创建全局变量用于保存代理信息</p>
<pre><code class="language-typescript">// floatData.ts
type ProxyElType = {
current: HTMLElement | null;
};
type MetaType = {
attrs: any;
props: any;
};

export const metadata: MetaType = {
attrs: {
    hideComponent: true,
    left: 0,
    top: 0
},
props: {},
};

export const proxyEl: ProxyElType = {
current: null,
};
</code></pre>
<p>创建一个FloatContainer容器组件,用于监听代理数据的变化, &nbsp;数据变动时驱动组件进行移动</p>
<pre><code class="language-tsx">import { metadata, proxyEl } from "./floatData";
class FloatContainer extends React.Component&lt;any, any&gt; {
componentDidMount() {
    // 将代理组件上的props绑定到Float组件上
    PubSub.subscribe("metadataChange", (msg, props) =&gt; {
      metadata.props = props;
      this.forceUpdate();
    });

    // 切换路由后代理元素改变,保存代理元素的位置信息
    PubSub.subscribe("proxyElChange", (msg, el) =&gt; {
      if (!el) {
      metadata.attrs.hideComponent = true;
      // 在下一次tick再更新dom
      setTimeout(() =&gt; {
          this.forceUpdate();
      }, 0);
      return;
      } else {
      metadata.attrs.hideComponent = false;
      }
      proxyEl.current = el.current;
      const rect = proxyEl.current?.getBoundingClientRect()!;
      metadata.attrs.left = rect.left;
      metadata.attrs.top = rect.top
      this.forceUpdate();
    });
}

render() {
    const { timeout = 500 } = this.props;
    const wrapperStyle: React.CSSProperties = {
      position: "fixed",
      left: metadata.attrs.left,
      top: metadata.attrs.top,
      transition: `all ${timeout}ms ease-in`,
                   // 当前路由未注册Proxy时进行隐藏
      display: metadata.attrs.hideComponent ? "none" : "block",
    };

    const propStyle = metadata.props.style || {};

    // 注入过渡样式属性
    const computedProps = {
      ...metadata.props,
      style: {
      transition: `all ${timeout}ms ease-in`,
      ...propStyle,
      },
    };
    console.log(metadata.attrs.hideComponent)

    return &lt;div className="float-element" style={wrapperStyle}&gt;{this.props.render(computedProps)} &lt;/div&gt;;
}
}
</code></pre>
<p>将组件提取到路由容器外部,并使用 FloatContainer 包裹</p>
<pre><code class="language-tsx">function App() {
return (
    &lt;BrowserRouter&gt;
      &lt;div className="App"&gt;
      &lt;NavLink to={"/"}&gt;/foo&lt;/NavLink&gt;
      &lt;NavLink to={"/bar"}&gt;/bar&lt;/NavLink&gt;
      &lt;NavLink to={"/baz"}&gt;/baz&lt;/NavLink&gt;
      &lt;FloatContainer render={(attrs: any) =&gt; &lt;MyImage {...attrs}/&gt;}&gt;&lt;/FloatContainer&gt;
      &lt;Routes&gt;
          &lt;Route path="/" element={&lt;Foo /&gt;}&gt;&lt;/Route&gt;
          &lt;Route path="/bar" element={&lt;Bar /&gt;}&gt;&lt;/Route&gt;
          &lt;Route path="/baz" element={&lt;Baz /&gt;}&gt;&lt;/Route&gt;
      &lt;/Routes&gt;
      &lt;/div&gt;
    &lt;/BrowserRouter&gt;
);
}
</code></pre>
<p>实现效果:</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011105958594-1966799567.gif"></p>
<p>目前我们实现了一个单例的组件,我们将组件改造一下,让其可以被复用</p>
<p>首先我们将元数据更改为一个元数据 map,以 layoutId 为键,元数据为值</p>
<pre><code class="language-tsx">// floatData.tsx
type ProxyElType = {
current: HTMLElement | null;
};
type MetaType = {
attrs: {
    hideComponent: boolean,
    left: number,
    top: number
};
props: any;
};

type floatType = {
metadata: MetaType,
proxyEl: ProxyElType
}

export const metadata: MetaType = {
attrs: {
    hideComponent: true,
    left: 0,
    top: 0
},
props: {},
};

export const proxyEl: ProxyElType = {
current: null,
};

export const floatMap = new Map&lt;string, floatType&gt;()
</code></pre>
<p>在代理组件中传递layoutId 来通知注册了相同layoutId的floatContainer做出相应变更</p>
<pre><code class="language-tsx">// FloatProxy.tsx

// 保存代理元素引用,方便获取元素的位置信息
useEffect(() =&gt; {
    const float = floatMap.get(props.layoutId);
    if (float) {
      float.proxyEl.current = el.current;
    } else {
      floatMap.set(props.layoutId, {
      metadata: {
          attrs: {
            hideComponent: true,
            left: 0,
            top: 0,
          },
          props: {},
      },
      proxyEl: {
          current: el.current,
      },
      });
    }
    PubSub.publish("proxyElChange", props.layoutId);
    return () =&gt; {
      if (float) {
      float.proxyEl.current = null
      PubSub.publish("proxyElChange", props.layoutId);
      }
    };
}, []);


// 在路由中使用
&lt;FloatProxy layoutId='layout1' style={{ width: 200, height: 200 }} /&gt;
</code></pre>
<p>在FloatContainer组件上也加上layoutId来标识同一组</p>
<pre><code class="language-tsx">// FloatContainer.tsx

// 监听到自己同组的Proxy发送消息时进行rerender
PubSub.subscribe("metadataChange", (msg, layoutId) =&gt; {
    if (layoutId === this.props.layoutId) {
      this.forceUpdate();
    }
});

// 页面中使用
&lt;FloatContainer layoutId='layout1' render={(attrs: any) =&gt; &lt;MyImage imgSrc={img} {...attrs} /&gt;}&gt;&lt;/FloatContainer&gt;
</code></pre>
<p>实现多组过渡的效果</p>
<p><img src="https://img2023.cnblogs.com/other/2332333/202310/2332333-20231011110001331-272208084.gif"></p>
<h2 id="最后">最后</h2>
<p>欢迎关注【袋鼠云数栈UED团队】~<br>
袋鼠云数栈UED团队持续为广大开发者分享技术成果,相继参与开源了欢迎star</p>
<ul>
<li><strong>大数据分布式任务调度系统——Taier</strong></li>
<li><strong>轻量级的 Web IDE UI 框架——Molecule</strong></li>
<li><strong>针对大数据领域的 SQL Parser 项目——dt-sql-parser</strong></li>
<li><strong>袋鼠云数栈前端团队代码评审工程实践文档——code-review-practices</strong></li>
<li><strong>一个速度更快、配置更灵活、使用更简单的模块打包器——ko</strong></li>
</ul><br><br>
来源:https://www.cnblogs.com/dtux/p/17756569.html
頁: [1]
查看完整版本: React跨路由组件动画