Create a dynamic react component using portal and promise

Call a modal or drawer from anywhere in my react app using simple a function.
I have made this work by using the root.render, but the problem with root.render they cannot found any of my previous providers, makeing me duplicate and import all providers again in this component. Is there a way to do this even if it’s not with the portal?

The code from the function:

  const handleDelete = async () => {
    try {
      const res = await SheetManager(".KeyDrawerGlobal", "KeyEdit");

      console.log(res);
    } catch (error) {
      console.error(error);
    }
  };

And this is the SheetManager code:

import React from "react";
import _ from "lodash";
import SheetComponent from "./SheetRegister";
import { SwipeableDrawer } from "@mui/material";
import { createPortal } from "react-dom";

const SheetManager = (container = "body", component = null, options = {}) => {
  return new Promise((resolve, reject) => {
    const parent = document.querySelector(container);
    if (!parent) return reject();
    const handleClose = (result) => {
      if (result) resolve({ result });

      reject();
    };

    createPortal(
      <SwipeableDrawer
        container={parent}
        anchor="bottom"
        open={true}
        onClose={handleClose}
        onOpen={() => {}}
        disableSwipeToOpen={false}
        ModalProps={{
          keepMounted: true,
          sx: {
            position: "absolute !important",
          },
          componentsProps: {
            backdrop: {
              sx: {
                position: "absolute !important",
              },
            },
          },
        }}
        PaperProps={{
          sx: {
            position: "absolute !important",
            borderTopLeftRadius: "16px",
            borderTopRightRadius: "16px",
          },
        }}
      >
        <SheetComponent type={component} {...options} />
      </SwipeableDrawer>,
      document.body
    );
  });
};

export default SheetManager;

New contributor

Andre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

I created a solution by using it like a Context, here is the result:

import {
  SwipeableDrawer,
} from "@mui/material";
import React, { createContext, useState, useContext } from "react";
import { createPortal } from "react-dom";
import SheetComponent from "../components/core/sheet/SheetRegister";

const SheetContext = createContext();

export const useSheet = () => useContext(SheetContext);

let showSheet;

export const SheetProvider = ({ children }) => {
  const [modal, setSheet] = useState(null);

  showSheet = (container = "body", component, props) => {
    const parent = document.querySelector(container);

    return new Promise((resolve, reject) => {
      const handleClose = (result) => {
        setSheet(null);
        if (result) resolve(result);
        else reject();
      };

      setSheet({ container: parent, component, props, handleClose });
    });
  };

  return (
    <SheetContext.Provider value={showSheet}>
      {children}
      {modal &&
        createPortal(
          <SwipeableDrawer
            container={modal.container}
            anchor="bottom"
            open={true}
            onClose={() => modal.handleClose()}
            onOpen={() => {}}
            disableSwipeToOpen={false}
            ModalProps={{
              keepMounted: true,
              sx: {
                position: "absolute !important",
              },
              componentsProps: {
                backdrop: {
                  sx: {
                    position: "absolute !important",
                  },
                },
              },
            }}
            PaperProps={{
              sx: {
                position: "absolute !important",
                borderTopLeftRadius: "16px",
                borderTopRightRadius: "16px",
              },
            }}
          >
            <SheetComponent
              type={modal.component}
              {...modal.props}
              handleClose={modal.handleClose}
            />
          </SwipeableDrawer>,
          modal.container
        )}
    </SheetContext.Provider>
  );
};

export { showSheet };

New contributor

Andre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật