-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreatePortalContext.tsx
More file actions
28 lines (23 loc) · 1014 Bytes
/
CreatePortalContext.tsx
File metadata and controls
28 lines (23 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { createContext, PropsWithChildren, ReactNode, ReactPortal, useContext, useRef } from "react";
import { createPortal } from "react-dom";
import { useOptions } from "./OptionsContext";
type CreatePortalContext = (children: ReactNode) => ReactPortal | null
const Context = createContext<CreatePortalContext | null>(null)
const CreatePortalContext = ({ children }: PropsWithChildren<{}>) => {
const overlay = useRef<HTMLDivElement>(null)
const { darkMode } = useOptions()
return (
<Context.Provider value={node => overlay.current === null ? null : createPortal(node, overlay.current)}>
<div ref={overlay} className={"absolute z-30 pointer-events-none overflow-hidden h-full w-full " + (darkMode ? "dark" : "")}></div>
{children}
</Context.Provider>
)
}
export const useCreatePortal = () => {
const ctx = useContext(Context)
if (ctx === null) {
throw new Error("Must use useCreatePortal between CreatePortalContext")
}
return ctx
}
export default CreatePortalContext