Giter Site home page Giter Site logo

worklyapp / react-native-portal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gorhom/react-native-portal

0.0 0.0 0.0 1.54 MB

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

License: MIT License

JavaScript 12.90% TypeScript 85.56% Handlebars 1.54%

react-native-portal's Introduction

React Native Portal

Base UI 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

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Portal, PortalProvider, PortalHost } from '@gorhom/portal';

const BasicScreen = () => {
  return (
    <View style={styles.container}>
      <View style={styles.box}>
        <Text style={styles.text}>
          Text won't be teleported!
          <Portal>
            <Text style={styles.text}>
              Text to be teleported to the root host
            </Text>
          </Portal>
          <Portal hostName="custom_host">
            <Text style={styles.text}>
              Text to be teleported to the custom host
            </Text>
          </Portal>
        </Text>
      </View>

      {/* Custom host */}
      <PortalHost name="custom_host" />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1 },
  box: {
    padding: 24,
    backgroundColor: '#333',
  },
  text: {
    alignSelf: 'center',
    textAlign: 'center',
    margin: 24,
    backgroundColor: '#eee',
  },
});

export default () => (
  <PortalProvider>
    <BasicScreen />
    {/* Text will be teleported to here */}
  </PortalProvider>
);

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 identifer.

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

License

MIT

Liked the library? 😇

Buy Me A Coffee


Mo Gorhom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.