Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@gorhom/portal

gorhom2.4mMIT1.0.14TypeScript support: included

A simplified portal implementation for ⭕️ React Native ⭕️

expo, react-native-web, react-native, ios, android, portal

readme

React Native Portal

React Native Portal npm npm runs with expo

A simplified portal implementation for ⭕️ React Native ⭕️.

React Native Portal


Features

  • Multi portals handling.
  • Multi portal hosts handling.
  • Allow override functionality.
  • Compatible with React Native Web.
  • Compatible with Expo, check out the project Expo Snack.
  • Written in TypeScript.

Installation

yarn add @gorhom/portal

Usage

Simple Portal

This is the very simple usage of this library, where you will teleport your content to the PortalProvider layer of the app.

First, you will need to add PortalProvider to your root component - this usually be the App.tsx.

export const App = () => (
  <PortalProvider>
  {... your app goes here}
  </PortalProvider>
);

Last, you wrap the content that you want to teleport with Portal.

const BasicScreen = () => {
  return (
    { ... }
    <Portal>
      <Text>
        Text to be teleported to the root host
      </Text>
    </Portal>
    { ... }
  );
};

Custom Portal Host

This is when you need to teleport your content to a custom portal host PortalHost at any layer in the app.

First, you will need to add PortalProvider to your root component - this usually be the App.tsx.

export const App = () => (
  <PortalProvider>
  {... your app goes here ...}
  </PortalProvider>
);

Second, you will need to add PortalHost at any layer in your app, with a custom name.

const CustomView = () => {
  return (
    { ... }
    <PortalHost name="CustomPortalHost" />
    { ... }
  );
};

Last, you wrap the content that you want to teleport with Portal and the custom portal host name.

const BasicScreen = () => {
  return (
    { ... }
    <Portal hostName="CustomPortalHost">
      <Text>
        Text to be teleported to the CustomView component
      </Text>
    </Portal>
    { ... }
  );
};

React Native Screens integration

In order to get your teleported content on top of all native views, you will need to wrap your content with FullWindowOverlay from react-native-screens.

import { FullWindowOverlay } from 'react-native-screens';

const BasicScreen = () => {
  return (
    { ... }
    <Portal>
      <FullWindowOverlay style={StyleSheet.absoluteFill}>
        <Text>
          Text to be teleported to the CustomView component
        </Text>
      </FullWindowOverlay>
    </Portal>
    { ... }
  );
};

React Native Gesture Handler

To avoid issues when using the React Native Portal with React Native Gesture Handler, you must place the PortalProvider under the GestureHandlerRootView, otherwise it might freeze your app.

export const App = () => (
  <GestureHandlerRootView>
    <PortalProvider>
    {... your app goes here}
    </PortalProvider>
  </GestureHandlerRootView>
);

Read more about the app freezing issue.

Props

Portal Props

name

Portal's key or name to be used as an identifer.

required: NO | type: string | default: auto generated unique key

hostName

Host's key or name to teleport the portal content to.

required: NO | type: string | default: 'root'

handleOnMount

Override internal mounting functionality, this is useful if you want to trigger any action before mounting the portal content.

type handleOnMount = (mount?: () => void) => void;

required: NO | type: function | default: undefined

handleOnUnmount

Override internal un-mounting functionality, this is useful if you want to trigger any action before un-mounting the portal content.

type handleOnUnmount = (unmount?: () => void) => void;

required: NO | type: function | default: undefined

children

Portal's content.

required: NO | type: ReactNode | ReactNode[] | default: undefined

PortalHost Props

name

Host's key or name to be used as an identifier.

required: YES | type: string

Hooks

usePortal

To access internal functionalities of all portals.

/**
 * @param hostName host name or key.
 */
type usePortal = (hostName: string = 'root') => {
  /**
   * Register a new host.
   */
  registerHost: () => void;
  /**
   * Deregister a host.
   */
  deregisterHost: () => void;
  /**
   * Add portal to the host container.
   * @param name portal name or key
   * @param node portal content
   */
  addPortal: (name: string, node: ReactNode) => void;
  /**
   * Update portal content.
   * @param name portal name or key
   * @param node portal content
   */
  updatePortal: (name: string, node: ReactNode) => void;
  /**
   * Remove portal from host container.
   * @param name portal name or key
   */
  removePortal: (name: string) => void;
};

Built With ❤️

Author

To keep this library maintained and up-to-date please consider sponsoring it on GitHub. Or if you are looking for a private support or help in customizing the experience, then reach out to me on Twitter @gorhom.

License

MIT


Mo Gorhom Mo Gorhom

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Generated by auto-changelog.

v1.0.14

Commits

  • docs: added usage example with gesture handler 098ccdf
  • fix: avoid non-defined global error on web (#30)(by @AlanSl) d74fd20
  • chore: fix linting in logger.ts 74a1290

v1.0.13 - 2022-04-10

Commits

  • chore: upgrade nanoid (#22)(by @jeremyadavis) 1570974

v1.0.12 - 2022-01-06

Commits

  • chore: updated examples d9c3ea6
  • docs: updated readme file 9b0fffd
  • refactor: removed index files 83c1b74

v1.0.11 - 2021-10-18

Commits

  • fix: updated Portal handling types 8eba3ea

v1.0.10 - 2021-10-17

Commits

  • refactor: added handleOnUpdate to Portal b000935
  • refactor: removed duplicated method "update" c312e4c

v1.0.9 - 2021-09-21

Commits

  • refactor: removing immer to fix max call stack error with production … (#20)(by @egadstar) 809c3bc

v1.0.8 - 2021-08-23

Commits

  • fix: allow PortalHost to mount/unmount safely #18 05042e5

v1.0.7 - 2021-06-10

Commits

  • fix: error messages on development dae78b5

v1.0.6 - 2021-06-10

Commits

  • refactor: removed returned draft from immer produce 4547623

v1.0.5 - 2021-06-10

Commits

v1.0.4 - 2021-03-04

Merged

  • fix: disable immer auto freeze #13

v1.0.3 - 2021-02-27

Commits

v1.0.1 - 2021-02-24

Commits

  • fix: enable es5 for immer 74bd72a

v1.0.0 - 2021-02-21

Merged

  • feat: added multi hosts #8

v0.2.0 - 2021-01-28

Merged

  • feat: added mounting handlers #7
  • chore: added popover example #6
  • chore: updated examples #5
  • chore: updated dependencies #4

Commits

  • docs: updated readme file 708bca3

v0.1.4 - 2020-12-15

Commits

  • chore: added update node logic 8ee8292

v0.1.3 - 2020-12-14

Commits

  • fix: prevent unwanted re-mounting whenever children updates e4f1eb9

v0.1.2 - 2020-12-12

Commits

  • chore: renamed Portal prop key to name, to avoid react warnings bf6cf1e

v0.1.1 - 2020-12-08

Commits

  • chore: init project d7980b6
  • chore: added initial implementaion 044c266
  • docs: updated readme file 488a977