import * as React from 'react'; import { FiberNavigator } from './FiberNavigator'; interface DebugRectProps { showBackground?: boolean; showClassName?: boolean; showCropMarks?: boolean; showElement?: boolean; fiberNav: FiberNavigator; renderLabel?: (fiberNav: FiberNavigator) => string; } const cropMarkStyle: React.CSSProperties = { position: 'absolute', background: '#6495ed88', }; export class DebugRect extends React.Component { selectorRef = React.createRef(); static defaultProps = { showBackground: true, showClassName: true, showElement: true, renderLabel: fiberNav => `<${fiberNav.name} />`, }; componentDidMount() { this.setDebugSelectorPosition(); } componentDidUpdate(prevProps, prevState, snapshot) { this.setDebugSelectorPosition(); } setDebugSelectorPosition = () => { const { fiberNav } = this.props; if ( fiberNav && fiberNav.domNode && fiberNav.domNode.getBoundingClientRect && typeof fiberNav.domNode.getBoundingClientRect === 'function' && this.selectorRef.current ) { const rect = fiberNav.domNode.getBoundingClientRect(); this.selectorRef.current.style.top = `${rect.top}px`; this.selectorRef.current.style.left = `${rect.left}px`; this.selectorRef.current.style.width = `${rect.width}px`; this.selectorRef.current.style.height = `${rect.height}px`; requestAnimationFrame(this.setDebugSelectorPosition); } }; render() { const { fiberNav, showBackground, showClassName, showCropMarks, showElement, renderLabel } = this.props; if (!fiberNav) { return null; } const label = renderLabel(fiberNav); return (
        {label && (
          
{renderLabel(fiberNav)}
)} {showCropMarks && ( <> {/* Top Left */}
{/* Top Right */}
{/* Bottom Left */}
{/* Bottom Right */}
)} {fiberNav.domNode && (showElement || showClassName) && (
{showElement && ( {fiberNav.domNode.tagName && fiberNav.domNode.tagName.toLowerCase()} )} {showClassName && fiberNav.domNode.hasAttribute && typeof fiberNav.domNode.hasAttribute === 'function' && fiberNav.domNode.hasAttribute('class') && ( .{(fiberNav.domNode.getAttribute('class') || '').replace(/ +/g, '.')} )}
)}
); } }