import clsx from "clsx"; import oc from "open-color"; import React, { 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 { LibraryItem } from "../types"; import "./LibraryUnit.scss"; // fa-plus const PLUS_ICON = ( ); export const LibraryUnit = ({ elements, pendingElements, onRemoveFromLibrary, onClick, }: { elements?: LibraryItem; pendingElements?: LibraryItem; onRemoveFromLibrary: () => void; onClick: () => void; }) => { const ref = useRef(null); useEffect(() => { const elementsToRender = elements || pendingElements; if (!elementsToRender) { return; } const svg = exportToSvg(elementsToRender, { exportBackground: false, viewBackgroundColor: oc.white, }); 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( MIME_TYPES.excalidrawlib, JSON.stringify(elements), ); }} /> {adder} {elements && (isHovered || isMobile) && ( )}
); };