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-teleportal

v0.0.2

Published

Alternative React portal implementation, giving you control over portal rendering.

Downloads

14

Readme

React Teleportal

GitHub package.json version CI GitHub license

Alternative React Portal implementation, giving you control over portal rendering.

Primarily written to support uninterrupted exit animations when combined with components such as TransitionGroup and AnimatePresence.

Install

npm install react-teleportal

Examples

React Teleportal x React Transition Group

https://codesandbox.io/s/react-teleportal-x-react-transition-group-k31d8p

React Teleportal x Framer Motion

https://codesandbox.io/s/react-teleportal-x-framer-motion-766nu7

Features

| Features | React Teleportal | ReactDOM.createPortal | | --------------------------- | ---------------- | --------------------- | | Custom Rendering | ✅ | ❌ | | Context | ✅* | ✅ | | Server Side Rendering (SSR) | ⚠️† | ❌ | | Multiple Portal Outlets | ❌‡ | ✅ | | React Tree Event Bubbling | ❌ | ✅ |

* Although <Portal />s in React Teleportal don't receive context from their own call site, they do receive context from the <PortalOutlet /> call site which means context from root providers will be available.

† Unlike ReactDOM.createPortal, React Teleportal doesn't depend on DOM APIs so the intention is to support SSR once a concurrent-safe solution has been found.

‡ React Teleportal doesn't currently support multiple portal outlets, but it would be trivial to add. For now it's been omitted because it would effectively become a "slot" library which, as a pattern, doesn't play nicely with streaming SSR.

API

Basic

import React, { useState } from 'react';
import { PortalProvider, PortalOutlet, Portal } from 'react-teleportal';

const App = () => {
  const [show, setShow] = useState(true);
  return (
    <PortalProvider>
      <button onClick={() => setShow(!show)}>Toggle</button>
      {show ? (
        <Portal>
          <>I render in the PortalOutlet</>
        </Portal>
      ) : null}
      <PortalOutlet />
    </PortalProvider>
  );
};

Animations with react-transition-group

import React, { useState, useRef } from 'react';
import { PortalProvider, PortalOutlet, Portal } from 'react-teleportal';
import { TransitionGroup, CSSTransition } from 'react-transition-group';

const App = () => {
  const [show, setShow] = useState(true);
  const nodeRef = useRef(null);
  return (
    <PortalProvider>
      <button onClick={() => setShow(!show)}>Toggle</button>
      {show ? (
        <Portal>
          <CSSTransition
            // `key` ensures showing / hiding the portal will reverse an in-flight animation rather than create a new instance.
            key="5f337061-5476-40a0-898e-e9f9827043b1"
            nodeRef={nodeRef}
            timeout={200}
            classNames="my-node"
          >
            <div ref={nodeRef}>I render in the PortalOutlet</div>
          </CSSTransition>
        </Portal>
      ) : null}
      <PortalOutlet>
        {(children) => <TransitionGroup>{children}</TransitionGroup>}
      </PortalOutlet>
    </PortalProvider>
  );
};

Animations with framer-motion

import React, { useState } from 'react';
import { PortalProvider, PortalOutlet, Portal } from 'react-teleportal';
import { AnimatePresence, motion } from 'framer-motion';

const App = () => {
  const [show, setShow] = useState(true);
  return (
    <PortalProvider>
      <button onClick={() => setShow(!show)}>Toggle</button>
      {show ? (
        <Portal>
          <motion.div
            // `key` ensures showing / hiding the portal will reverse an in-flight animation rather than create a new instance.
            key="02fe2dd1-e9d8-46e4-898b-4c1966c9a68b"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
          >
            I render in the PortalOutlet
          </motion.div>
        </Portal>
      ) : null}
      <PortalOutlet>
        {(children) => <AnimatePresence>{children}</AnimatePresence>}
      </PortalOutlet>
    </PortalProvider>
  );
};

FAQ

Does React Teleportal support SSR?

React Teleportal won't blow up on the server, but <Portal />s won't be rendered to HTML server side and instead will be rendered once on the client.

The intention is to eventually find a concurrent-safe SSR solution.

Can I have multiple named <PortalOutlet />s?

No not currently. React Teleportal intends to eventually support SSR & treating this as a "slot" library makes SSR less viable.

React Gateway is a good example of the "slot" pattern and how it can easily fail if misused.

import { GatewayProvider, GatewayDest, Gateway } from 'react-gateway';

const App = () => {
  return (
    <GatewayProvider>
      <header>
        <GatewayDest name="header-slot" />
      </header>
      <section>
        <Gateway into="header-slot">
          SSR will fail to render this as the "header-slot" has already rendered
          (and if streaming, the html has potentially already been flushed to
          the client).
        </Gateway>
      </section>
    </GatewayProvider>
  );
};

React Teleportal is therefore stricter and only allows a single <GatewayDest /> (or <PortalOutlet /> in React Teleportal terminology) which should be rendered at the bottom of the root component.

How do I manage stacking order?

It's recommended to avoid z-index and treat your <PortalOutlet /> similar to the DOM's Top Layer whereby the most recently ~~opened~~ mounted <Portal /> is rendered last and therefore naturally stacked on top.

Why do I need to add a key to the <Portal /> child when animating?

The collective <Portal /> children are ultimately rendered as children of the <PortalOutlet /> which means React is rendering a variable length array of elements which requires a key.

It's recommended to just statically include a uuid or similar at the call site of each distinct <Portal /> child to ensure it remains unique as your app grows.

<Portal>
  <div key="6db2c89c-dbb4-4c9e-96fa-8ad1d3dec463">Hello World</div>
</Portal>

NOTE: If you're not animating (i.e. if the <PortalOutlet /> unmounts the child immediately), then you can omit the key as React Teleportal is able to assign a key on your behalf.

License

MIT