import clsx from "clsx"; import oc from "open-color"; import { useEffect, useRef, useState } from "react"; import { close } from "../components/icons"; import { MIME_TYPES } from "../constants"; import { t } from "../i18n"; import { useIsMobile } from "../components/App"; import { exportToSvg } from "../scene/export"; import { BinaryFiles, LibraryItem } from "../types"; import "./LibraryUnit.scss"; // fa-plus const PLUS_ICON = ( ); export const LibraryUnit = ({ elements, files, pendingElements, onRemoveFromLibrary, onClick, }: { elements?: LibraryItem; files: BinaryFiles; pendingElements?: LibraryItem; onRemoveFromLibrary: () => void; onClick: () => void; }) => { const ref = useRef(null); useEffect(() => { (async () => { const elementsToRender = elements || pendingElements; if (!elementsToRender) { return; } const svg = await exportToSvg( elementsToRender, { exportBackground: false, viewBackgroundColor: oc.white, }, files, ); if (ref.current) { ref.current.innerHTML = svg.outerHTML; } })(); }, [elements, pendingElements, files]); 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( MIME_TYPES.excalidrawlib, JSON.stringify(elements), ); }} /> {adder} {elements && (isHovered || isMobile) && ( )}
); };