npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-routerfly

v0.1.1

Published

Shared React component across routes with animations

Downloads

15

Readme

Why

We usually use the same React compoennt in different pages(routes) with different sizes and positions. Sometime you may want to have smooth animation of the component when switching between routes.

The component structure of React is presented in the form of a tree, and even the same component will have different instances under different routes. This means that when users switch between routes, the same component will not be shared across routes.

The existing solution for cross-route components is FLIP, which can simulate transition animations between components. However, it still creates two component instances and the internal state of the component is lost.

Routerfly is designed to address this need, and you can think of it as the React version of Vue Starport.

How

Since we cannot share components between different branches of the component tree, we can actually lift the components up to the root node so that they exist independently of the routes.

Using a proxy component to obtain the position, size, and props of the component, and pass the information to the actual component, allowing it to “fly” to the position of another page through animation when switching routes.

However, there is a problem with this approach. The node position of the component in the DOM tree is different from its original position because it is floating at the root node.

When the animation is finished, we can use the createPortal function to teleport it to the actual node in the DOM tree. Through this "landing" mechanism, the structure of the DOM tree can be maintained.

Install

npm i react-routerfly

Usage

export <RouterFlyCarrier> from react-routerfly and add it to the root component(app.ts). All useage of <RouterFly> should be inside <RouterFlyCarrier> component.

import { RouterFlyCarrier } from 'react-routerfly'

const App = () => {
  return (
    <RouterFlyCarrier>
      <>
        <Layout>
          <Outlet />
        <Layout/>
      </>
    </RouterFlyCarrier>
  )
}

export default App

In a certain page, wrap the component with <RouterFly> and pass the port prop (any string)

import {RouterFly} from 'react-routerfly'

const PageA = () => {
  return (
    <div>
    <!-- ... -->
    <RouterFly port={id} style={{height:600px}}>
      <MyComponent prop={value} />
    </RouterFly>
  </div>
  );
};

export default App;

On another page, use the same port to match it:

import {RouterFly} from 'react-routerfly'

const PageB = () => {
  return (
    <div>
      <!-- ... -->
      <RouterFly port={id} style={{height:400px}}>
        <MyComponent prop={value} />
      </RouterFly>
    </div>
  );
};

export default App;

Please note that you may need to add some styles to <RouterFly> so that it has a size even when there is no content. Also, place layout-related styles on <RouterFly>.

options

keepAlive

default value is true

you can config it on <RouterFlyCarrier> or <RouterFly>

config globally

  <RouterFlyCarrier keepAlive={?}>
        ...
    </RouterFlyCarrier>

also you can control each component's keepAlive config

  <RouterFly port={id} style={{height:400px}} keepAlive={?}>
        <MyComponent prop={value} />
      </RouterFly>

the priority of configuration of keepAlive on <RouterFly> is higher than <RouterFlyCarrier>

Todo

  • [x] keepalive configurable(by default, <RouterFly> has keepalive enabled. This means that when you navigate to a page that does not have a corresponding <RouterFly> proxy, the component will not be unmounted and will remain in memory.)
  • [ ] animation duration configurable

License

MIT License © 2023 frankcao