/** * WordPress dependencies */ import { useState, useRef, useEffect } from '@wordpress/element'; import isShallowEqual from '@wordpress/is-shallow-equal'; /** * Internal dependencies */ import BlockPopoverCover from '../components/block-popover/cover'; import { __unstableUseBlockElement as useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; function getComputedCSS( element, property ) { return element.ownerDocument.defaultView .getComputedStyle( element ) .getPropertyValue( property ); } export function MarginVisualizer( { clientId, attributes, forceShow } ) { const blockElement = useBlockElement( clientId ); const [ style, setStyle ] = useState(); const margin = attributes?.style?.spacing?.margin; useEffect( () => { if ( ! blockElement || null === blockElement.ownerDocument.defaultView ) { return; } const top = getComputedCSS( blockElement, 'margin-top' ); const right = getComputedCSS( blockElement, 'margin-right' ); const bottom = getComputedCSS( blockElement, 'margin-bottom' ); const left = getComputedCSS( blockElement, 'margin-left' ); setStyle( { borderTopWidth: top, borderRightWidth: right, borderBottomWidth: bottom, borderLeftWidth: left, top: top ? `-${ top }` : 0, right: right ? `-${ right }` : 0, bottom: bottom ? `-${ bottom }` : 0, left: left ? `-${ left }` : 0, } ); }, [ blockElement, margin ] ); const [ isActive, setIsActive ] = useState( false ); const valueRef = useRef( margin ); const timeoutRef = useRef(); const clearTimer = () => { if ( timeoutRef.current ) { window.clearTimeout( timeoutRef.current ); } }; useEffect( () => { if ( ! isShallowEqual( margin, valueRef.current ) && ! forceShow ) { setIsActive( true ); valueRef.current = margin; timeoutRef.current = setTimeout( () => { setIsActive( false ); }, 400 ); } return () => { setIsActive( false ); clearTimer(); }; }, [ margin, forceShow ] ); if ( ! isActive && ! forceShow ) { return null; } return (
); }