|
1 | | -import React, { useEffect, useState } from 'react'; |
2 | | -import ReactDOM from 'react-dom'; |
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + createContext, |
| 4 | + useCallback, |
| 5 | + useContext, |
| 6 | + useLayoutEffect, |
| 7 | + useMemo, |
| 8 | +} from 'react'; |
| 9 | +import { createPortal } from 'react-dom'; |
| 10 | +import { useIsMounted } from '../../hooks/useIsMounted'; |
3 | 11 |
|
4 | 12 | interface PortalProps { |
5 | | - id?: string; |
6 | 13 | children: React.ReactNode; |
| 14 | + className?: string; |
| 15 | + containerRef?: React.RefObject<HTMLElement | null>; |
7 | 16 | } |
8 | 17 |
|
9 | | -const Portal = ({ id, children }: PortalProps) => { |
10 | | - const [portalElement, setPortalElement] = useState<Element | null>(null); |
| 18 | +const portalContext = createContext<{ parentPortal: HTMLElement | null }>({ |
| 19 | + parentPortal: null, |
| 20 | +}); |
11 | 21 |
|
12 | | - useEffect(() => { |
13 | | - setPortalElement(document.querySelector(`#${id}`)); |
14 | | - }, [id]); |
| 22 | +const PORTAL_DEFAULT_CLASS = 'thumbnail-generator'; |
15 | 23 |
|
16 | | - if (!portalElement) return <>{children}</>; |
17 | | - return ReactDOM.createPortal(children, portalElement); |
18 | | -}; |
| 24 | +function RenderPortal({ children, className, containerRef }: PortalProps) { |
| 25 | + const { parentPortal } = useContext(portalContext); |
19 | 26 |
|
20 | | -export default Portal; |
| 27 | + const getPortalNode = useCallback( |
| 28 | + (mountNode: HTMLElement) => { |
| 29 | + const portalNode = mountNode.ownerDocument.createElement('div'); |
| 30 | + portalNode.classList.add(className || PORTAL_DEFAULT_CLASS); |
| 31 | + |
| 32 | + return portalNode; |
| 33 | + }, |
| 34 | + [className] |
| 35 | + ); |
| 36 | + |
| 37 | + /** |
| 38 | + * This is the mount node to render portal nodes. |
| 39 | + * The mountNode has the value "containerRef.current" if it has a "containerRef", or the parent portal node if it is a nested portal. |
| 40 | + * By default, it has "document.body". |
| 41 | + */ |
| 42 | + const mountNode = useMemo(() => { |
| 43 | + if (parentPortal) { |
| 44 | + return parentPortal; |
| 45 | + } |
| 46 | + |
| 47 | + if (containerRef?.current) { |
| 48 | + return containerRef.current; |
| 49 | + } |
| 50 | + |
| 51 | + return document.body; |
| 52 | + }, [parentPortal, containerRef]); |
| 53 | + |
| 54 | + const portalNode = useMemo( |
| 55 | + () => getPortalNode(mountNode), |
| 56 | + [getPortalNode, mountNode] |
| 57 | + ); |
| 58 | + |
| 59 | + useLayoutEffect(() => { |
| 60 | + mountNode.appendChild(portalNode); |
| 61 | + |
| 62 | + /** |
| 63 | + * "portalNode" is removed from "mountNode" on unmount. |
| 64 | + */ |
| 65 | + return () => { |
| 66 | + if (mountNode.contains(portalNode)) { |
| 67 | + mountNode.removeChild(portalNode); |
| 68 | + } |
| 69 | + }; |
| 70 | + }, [portalNode, mountNode]); |
| 71 | + |
| 72 | + return createPortal( |
| 73 | + <portalContext.Provider value={{ parentPortal: portalNode }}> |
| 74 | + {children} |
| 75 | + </portalContext.Provider>, |
| 76 | + portalNode |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** @tossdocs-ignore */ |
| 81 | +export default function Portal({ children, ...restProps }: PortalProps) { |
| 82 | + const isMounted = useIsMounted(); |
| 83 | + |
| 84 | + /** |
| 85 | + * With this code, it is possible to solve the "window is not defined" and "Hydration Error" that can occur in SSR. |
| 86 | + */ |
| 87 | + if (!isMounted) { |
| 88 | + return <></>; |
| 89 | + } |
| 90 | + |
| 91 | + return <RenderPortal {...restProps}>{children}</RenderPortal>; |
| 92 | +} |
0 commit comments