import React, { useRef, useEffect, useState } from "react"; import { exportToSvg } from "../scene/export"; import { ExcalidrawElement, NonDeleted } from "../element/types"; import { close } from "../components/icons"; import "./LibraryUnit.scss"; import { t } from "../i18n"; import useIsMobile from "../is-mobile"; // fa-plus const PLUS_ICON = ( ); export const LibraryUnit = ({ elements, pendingElements, onRemoveFromLibrary, onClick, }: { elements?: NonDeleted[]; pendingElements?: NonDeleted[]; onRemoveFromLibrary: () => void; onClick: () => void; }) => { const ref = useRef(null); useEffect(() => { const elementsToRender = elements || pendingElements; if (!elementsToRender) { return; } const svg = exportToSvg(elementsToRender, { exportBackground: false, viewBackgroundColor: "#fff", shouldAddWatermark: false, }); for (const child of ref.current!.children) { if (child.tagName !== "svg") { continue; } ref.current!.removeChild(child); } ref.current!.appendChild(svg); const current = ref.current!; return () => { current.removeChild(svg); }; }, [elements, pendingElements]); const [isHovered, setIsHovered] = useState(false); const isMobile = useIsMobile(); const adder = (isHovered || isMobile) && pendingElements && (
{PLUS_ICON}
); return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} >
{ setIsHovered(false); event.dataTransfer.setData( "application/vnd.excalidraw.json", JSON.stringify(elements), ); }} /> {adder} {elements && (isHovered || isMobile) && ( )}
); };