-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0e6d64
commit 0a30a23
Showing
7 changed files
with
121 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
frontend/webapp/design.system/notification/portal.notification.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useState, useLayoutEffect } from "react"; | ||
import { createPortal } from "react-dom"; | ||
|
||
interface Props { | ||
children: JSX.Element; | ||
wrapperId: string; | ||
} | ||
|
||
const PortalNotification = ({ children, wrapperId }: Props) => { | ||
const [portalElement, setPortalElement] = useState<HTMLElement | null>(null); | ||
|
||
useLayoutEffect(() => { | ||
let element = document.getElementById(wrapperId) as HTMLElement; | ||
let portalCreated = false; | ||
// if element is not found with wrapperId or wrapperId is not provided, | ||
// create and append to body | ||
if (!element) { | ||
element = createWrapperAndAppendToBody(wrapperId); | ||
portalCreated = true; | ||
} | ||
|
||
setPortalElement(element); | ||
|
||
// cleaning up the portal element | ||
return () => { | ||
// delete the programatically created element | ||
if (portalCreated && element.parentNode) { | ||
element.parentNode.removeChild(element); | ||
} | ||
}; | ||
}, [wrapperId]); | ||
|
||
const createWrapperAndAppendToBody = (elementId: string) => { | ||
const element = document.createElement("div"); | ||
element.setAttribute("id", elementId); | ||
document.body.appendChild(element); | ||
return element; | ||
}; | ||
|
||
// portalElement state will be null on the very first render. | ||
if (!portalElement) return null; | ||
|
||
return createPortal(children, portalElement); | ||
}; | ||
|
||
export default PortalNotification; |