From 40b16629f090993de43c1cf38cdcc5af733eae3a Mon Sep 17 00:00:00 2001 From: Marius Ahsmus Date: Mon, 12 Aug 2024 15:54:09 +0200 Subject: [PATCH] fix: typescript interface error --- package.json | 2 +- src/component/toastportal.tsx | 31 ++++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 37dd915..38dee8f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "griller", "license": "MIT", - "version": "1.0.16", + "version": "1.0.17", "private": false, "repository": { "url": "https://github.com/mvriu5/griller" diff --git a/src/component/toastportal.tsx b/src/component/toastportal.tsx index 6dd0cf3..2ee19cc 100644 --- a/src/component/toastportal.tsx +++ b/src/component/toastportal.tsx @@ -1,30 +1,31 @@ -import ReactDOM from 'react-dom'; +import {createPortal} from 'react-dom'; import React, {ReactNode, useEffect, useState} from 'react'; -interface ToastPortalProps { - children: ReactNode; -} -export const ToastPortal: React.FC = ({ children }) => { +const ToastPortal: React.FC<{children: ReactNode}> = ({ children }) => { const [portalElement, setPortalElement] = useState(null); useEffect(() => { - let element = document.getElementById('toast-portal-root'); - if (!element) { - element = document.createElement('div'); - element.id = 'toast-portal-root'; - document.body.appendChild(element); + if (typeof window !== 'undefined') { + let element = document.getElementById('toast-portal-root'); + if (!element) { + element = document.createElement('div'); + element.id = 'toast-portal-root'; + document.body.appendChild(element); + } + setPortalElement(element); } - setPortalElement(element); return () => { - if (element && element.parentNode) { - element.parentNode.removeChild(element); + if (portalElement && portalElement.parentNode) { + portalElement.parentNode.removeChild(portalElement); } }; }, []); if (!portalElement) return null; - return ReactDOM.createPortal(children, portalElement); -}; \ No newline at end of file + return createPortal(children, portalElement); +}; + +export { ToastPortal }; \ No newline at end of file