You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
1 year ago
|
import { useState, useLayoutEffect } from "react";
|
||
2 years ago
|
import { useDevice, useExcalidrawContainer } from "../components/App";
|
||
|
import { useUIAppState } from "../context/ui-appState";
|
||
|
|
||
|
export const useCreatePortalContainer = (opts?: {
|
||
|
className?: string;
|
||
|
parentSelector?: string;
|
||
|
}) => {
|
||
|
const [div, setDiv] = useState<HTMLDivElement | null>(null);
|
||
|
|
||
|
const device = useDevice();
|
||
|
const { theme } = useUIAppState();
|
||
|
|
||
|
const { container: excalidrawContainer } = useExcalidrawContainer();
|
||
|
|
||
|
useLayoutEffect(() => {
|
||
|
if (div) {
|
||
1 year ago
|
div.className = "";
|
||
|
div.classList.add("excalidraw", ...(opts?.className?.split(/\s+/) || []));
|
||
1 year ago
|
div.classList.toggle("excalidraw--mobile", device.editor.isMobile);
|
||
1 year ago
|
div.classList.toggle("theme--dark", theme === "dark");
|
||
2 years ago
|
}
|
||
1 year ago
|
}, [div, theme, device.editor.isMobile, opts?.className]);
|
||
2 years ago
|
|
||
|
useLayoutEffect(() => {
|
||
|
const container = opts?.parentSelector
|
||
|
? excalidrawContainer?.querySelector(opts.parentSelector)
|
||
|
: document.body;
|
||
|
|
||
|
if (!container) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const div = document.createElement("div");
|
||
|
|
||
|
container.appendChild(div);
|
||
|
|
||
|
setDiv(div);
|
||
|
|
||
|
return () => {
|
||
|
container.removeChild(div);
|
||
|
};
|
||
1 year ago
|
}, [excalidrawContainer, opts?.parentSelector]);
|
||
2 years ago
|
|
||
|
return div;
|
||
|
};
|