mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
create portal root for toast on demand
This commit is contained in:
@@ -1,2 +0,0 @@
|
|||||||
<div id="modalRoot"></div>
|
|
||||||
<div id="toastRoot"></div>
|
|
||||||
@@ -2,14 +2,13 @@ import React, { FC } from "react";
|
|||||||
import { createPortal } from "react-dom";
|
import { createPortal } from "react-dom";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import { getTheme, Themeable, ToastThemeContext, Type } from "./themes";
|
import { getTheme, Themeable, ToastThemeContext, Type } from "./themes";
|
||||||
|
import usePortalRootElement from "../usePortalRootElement";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
type: Type;
|
type: Type;
|
||||||
title: string;
|
title: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const rootElement = document.getElementById("toastRoot");
|
|
||||||
|
|
||||||
const Container = styled.div<Themeable>`
|
const Container = styled.div<Themeable>`
|
||||||
z-index: 99999;
|
z-index: 99999;
|
||||||
position: fixed;
|
position: fixed;
|
||||||
@@ -43,8 +42,10 @@ const Title = styled.h1<Themeable>`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const Toast: FC<Props> = ({ children, title, type }) => {
|
const Toast: FC<Props> = ({ children, title, type }) => {
|
||||||
|
const rootElement = usePortalRootElement("toastRoot");
|
||||||
if (!rootElement) {
|
if (!rootElement) {
|
||||||
throw new Error("could not find toast container #toastRoot");
|
// portal not yet ready
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const theme = getTheme(type);
|
const theme = getTheme(type);
|
||||||
|
|||||||
33
scm-ui/ui-components/src/usePortalRootElement.ts
Normal file
33
scm-ui/ui-components/src/usePortalRootElement.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
const createElement = (id: string) => {
|
||||||
|
const element = document.createElement("div");
|
||||||
|
element.setAttribute("id", id);
|
||||||
|
return element;
|
||||||
|
};
|
||||||
|
|
||||||
|
const appendRootElement = (rootElement: HTMLElement) => {
|
||||||
|
document.body.appendChild(rootElement);
|
||||||
|
};
|
||||||
|
|
||||||
|
const usePortalRootElement = (id: string) => {
|
||||||
|
const [rootElement, setRootElement] = useState<HTMLElement>();
|
||||||
|
useEffect(() => {
|
||||||
|
let element = document.getElementById(id);
|
||||||
|
if (!element) {
|
||||||
|
element = createElement(id);
|
||||||
|
appendRootElement(element);
|
||||||
|
}
|
||||||
|
setRootElement(element);
|
||||||
|
return () => {
|
||||||
|
if (element) {
|
||||||
|
element.remove();
|
||||||
|
}
|
||||||
|
setRootElement(undefined);
|
||||||
|
};
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
|
return rootElement;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default usePortalRootElement;
|
||||||
@@ -34,7 +34,6 @@
|
|||||||
</noscript>
|
</noscript>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
<div id="modalRoot"></div>
|
<div id="modalRoot"></div>
|
||||||
<div id="toastRoot"></div>
|
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
This HTML file is a template.
|
This HTML file is a template.
|
||||||
|
|||||||
Reference in New Issue
Block a user