Skip to content

Commit

Permalink
fix: moved portal to toaster file
Browse files Browse the repository at this point in the history
  • Loading branch information
mvriu5 committed Aug 12, 2024
1 parent 40b1662 commit 66a4fb5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "griller",
"license": "MIT",
"version": "1.0.17",
"version": "1.0.18",
"private": false,
"repository": {
"url": "https://github.com/mvriu5/griller"
Expand Down
30 changes: 28 additions & 2 deletions src/component/toaster.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
"use client";

import React, {createContext, ReactNode, useCallback, useContext, useMemo, useRef, useState} from 'react';
import React, {createContext, ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {Position, positionClasses, Toast, ToastProps} from './toast';
import {AnimatePresence, motion} from "framer-motion";
import { ToastPortal } from './toastportal';
import {createPortal} from "react-dom";

const ToastPortal: React.FC<{children: ReactNode}> = ({ children }) => {
const [portalElement, setPortalElement] = useState<HTMLElement | null>(null);

useEffect(() => {
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);
}

return () => {
if (portalElement && portalElement.parentNode) {
portalElement.parentNode.removeChild(portalElement);
}
};
}, []);

if (!portalElement) return null;

return createPortal(children, portalElement);
};

interface ToastContextType {
addToast: (props: Omit<ToastProps, 'id'>) => string;
Expand Down
31 changes: 0 additions & 31 deletions src/component/toastportal.tsx

This file was deleted.

0 comments on commit 66a4fb5

Please sign in to comment.