import clsx from "clsx"; import oc from "open-color"; import { useEffect, useRef, useState } from "react"; import { MIME_TYPES } from "../constants"; import { useIsMobile } from "../components/App"; import { exportToSvg } from "../scene/export"; import { BinaryFiles, LibraryItem } from "../types"; import "./LibraryUnit.scss"; import { CheckboxItem } from "./CheckboxItem"; const PLUS_ICON = ( ); export const LibraryUnit = ({ id, elements, files, isPending, onClick, selected, onToggle, }: { id: LibraryItem["id"] | /** for pending item */ null; elements?: LibraryItem["elements"]; files: BinaryFiles; isPending?: boolean; onClick: () => void; selected: boolean; onToggle: (id: string, event: React.MouseEvent) => void; }) => { const ref = useRef(null); useEffect(() => { const node = ref.current; if (!node) { return; } (async () => { if (!elements) { return; } const svg = await exportToSvg( elements, { exportBackground: false, viewBackgroundColor: oc.white, }, files, ); node.innerHTML = svg.outerHTML; })(); return () => { node.innerHTML = ""; }; }, [elements, files]); const [isHovered, setIsHovered] = useState(false); const isMobile = useIsMobile(); const adder = isPending && (
{PLUS_ICON}
); return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} >
{ if (id && event.shiftKey) { onToggle(id, event); } else { onClick(); } } : undefined } onDragStart={(event) => { setIsHovered(false); event.dataTransfer.setData( MIME_TYPES.excalidrawlib, JSON.stringify(elements), ); }} /> {adder} {id && elements && (isHovered || isMobile || selected) && ( onToggle(id, event)} className="library-unit__checkbox" /> )}
); };