import * as React from 'react'; import { CloneDebugButton, LevelUpDebugButton, TrashDebugButton, MoveDebugButton } from './DebugButtons'; export type DebugFrameProps = { target; selector; componentName?; onClone?; onDelete?; onMove?; onGoToParent?; }; // FIXME: temporary hacky implementation! reuse DebugRect export const DebugFrame: React.FunctionComponent = ({ target, selector, componentName, onClone, onDelete, onMove, onGoToParent, }) => { const frameRef = React.useRef(); const animationFrameId = React.useRef(); const setFramePosition = React.useCallback((frameEl, controlEl) => { const rect = controlEl.getBoundingClientRect(); frameEl.style.top = `${rect.top}px`; frameEl.style.left = `${rect.left}px`; frameEl.style.width = `${rect.width}px`; frameEl.style.height = `${rect.height}px`; frameEl.style.display = 'block'; animationFrameId.current = requestAnimationFrame(() => setFramePosition(frameEl, controlEl)); }, []); const hideFrame = frameEl => { frameEl.style.display = 'none'; }; const handleMove = React.useCallback( e => { onMove?.(e); }, [onMove], ); const handleClone = React.useCallback( e => { onClone?.(e); }, [onClone], ); const handleDelete = React.useCallback(() => { onDelete?.(); }, [onDelete]); const handleGoToParent = React.useCallback(() => { onGoToParent?.(); }, [onGoToParent]); React.useEffect(() => { if (animationFrameId.current) { cancelAnimationFrame(animationFrameId.current); } if (!frameRef.current) { return undefined; } const el = target.querySelectorAll(selector); animationFrameId.current = el.length === 1 ? requestAnimationFrame(() => setFramePosition(frameRef.current, el[0])) : requestAnimationFrame(() => hideFrame(frameRef.current)); return () => cancelAnimationFrame(animationFrameId.current); }, [target, selector, setFramePosition]); return (
      
{componentName}
); };