From cd209172a2d1b2038d043d221bd55bdd9e108fc0 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 15 Feb 2023 13:33:14 +0100 Subject: [PATCH] Add visualizers back --- .../global-styles/dimensions-panel.js | 12 +++++ packages/block-editor/src/hooks/dimensions.js | 48 +++++++++---------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/block-editor/src/components/global-styles/dimensions-panel.js b/packages/block-editor/src/components/global-styles/dimensions-panel.js index 6d189e6b83255..9f2d355a053a4 100644 --- a/packages/block-editor/src/components/global-styles/dimensions-panel.js +++ b/packages/block-editor/src/components/global-styles/dimensions-panel.js @@ -178,6 +178,7 @@ export default function DimensionsPanel( { settings, panelId, defaultControls = DEFAULT_CONTROLS, + onVisualize = () => {}, } ) { const decodeValue = ( rawValue ) => getValueFromVariable( { settings }, '', rawValue ); @@ -247,6 +248,7 @@ export default function DimensionsPanel( { !! value?.spacing?.padding && Object.keys( value?.spacing?.padding ).length; const resetPaddingValue = () => setPaddingValues( undefined ); + const onMouseOverPadding = () => onVisualize( 'padding' ); // Margin const showMarginControl = useHasMargin( settings ); @@ -272,6 +274,7 @@ export default function DimensionsPanel( { !! value?.spacing?.margin && Object.keys( value?.spacing?.margin ).length; const resetMarginValue = () => setMarginValues( undefined ); + const onMouseOverMargin = () => onVisualize( 'margin' ); // Block Gap const showGapControl = useHasGap( settings ); @@ -370,6 +373,7 @@ export default function DimensionsPanel( { }, } ); }; + const onMouseLeaveControls = () => onVisualize( false ); return ( @@ -450,6 +454,8 @@ export default function DimensionsPanel( { units={ units } allowReset={ false } splitOnAxis={ isAxialPadding } + onMouseOver={ onMouseOverPadding } + onMouseOut={ onMouseLeaveControls } /> ) } { showSpacingPresetsControl && ( @@ -461,6 +467,8 @@ export default function DimensionsPanel( { units={ units } allowReset={ false } splitOnAxis={ isAxialPadding } + onMouseOver={ onMouseOverPadding } + onMouseOut={ onMouseLeaveControls } /> ) } @@ -485,6 +493,8 @@ export default function DimensionsPanel( { units={ units } allowReset={ false } splitOnAxis={ isAxialMargin } + onMouseOver={ onMouseOverMargin } + onMouseOut={ onMouseLeaveControls } /> ) } { showSpacingPresetsControl && ( @@ -496,6 +506,8 @@ export default function DimensionsPanel( { units={ units } allowReset={ false } splitOnAxis={ isAxialMargin } + onMouseOver={ onMouseOverMargin } + onMouseOut={ onMouseLeaveControls } /> ) } diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index 5104c6437b31e..39189361549ed 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -1,8 +1,8 @@ /** * WordPress dependencies */ -/*import { useState } from '@wordpress/element'; -import { useDispatch } from '@wordpress/data';*/ +import { useState, useEffect } from '@wordpress/element'; +import { useDispatch } from '@wordpress/data'; import { getBlockSupport } from '@wordpress/blocks'; import deprecated from '@wordpress/deprecated'; @@ -14,11 +14,11 @@ import { DimensionsPanel as StylesDimensionsPanel, useHasDimensionsPanel, } from '../components/global-styles'; -/*import { MarginVisualizer } from './margin'; +import { MarginVisualizer } from './margin'; import { PaddingVisualizer } from './padding'; import { store as blockEditorStore } from '../store'; import { unlock } from '../lock-unlock'; -*/ + import { cleanEmptyObject, useBlockSettings } from './utils'; export const DIMENSIONS_SUPPORT_KEY = 'dimensions'; @@ -26,23 +26,21 @@ export const SPACING_SUPPORT_KEY = 'spacing'; export const ALL_SIDES = [ 'top', 'right', 'bottom', 'left' ]; export const AXIAL_SIDES = [ 'vertical', 'horizontal' ]; -/*function useVisualizerMouseOver() { - const [ isMouseOver, setIsMouseOver ] = useState( false ); +function useVisualizer() { + const [ property, setProperty ] = useState( false ); const { hideBlockInterface, showBlockInterface } = unlock( useDispatch( blockEditorStore ) ); - const onMouseOver = ( e ) => { - e.stopPropagation(); - hideBlockInterface(); - setIsMouseOver( true ); - }; - const onMouseOut = ( e ) => { - e.stopPropagation(); - showBlockInterface(); - setIsMouseOver( false ); - }; - return { isMouseOver, onMouseOver, onMouseOut }; -}*/ + useEffect( () => { + if ( ! property ) { + showBlockInterface(); + } else { + hideBlockInterface(); + } + }, [ property, showBlockInterface, hideBlockInterface ] ); + + return [ property, setProperty ]; +} function DimensionsInspectorControl( { children } ) { return ( @@ -61,8 +59,7 @@ export function DimensionsPanel( props ) { const settings = useBlockSettings( name, __unstableParentLayout ); const isEnabled = useHasDimensionsPanel( settings ); const value = attributes.style; - //const paddingMouseOver = useVisualizerMouseOver(); - //const marginMouseOver = useVisualizerMouseOver(); + const [ visualizedProperty, setVisualizedProperty ] = useVisualizer(); const onChange = ( newStyle ) => { setAttributes( { style: cleanEmptyObject( newStyle ), @@ -96,21 +93,20 @@ export function DimensionsPanel( props ) { value={ value } onChange={ onChange } defaultControls={ defaultControls } + onVisualize={ setVisualizedProperty } /> - { /* - { ! isPaddingDisabled && ( + { !! settings?.spacing?.padding && ( ) } - { ! isMarginDisabled && ( + { !! settings?.spacing?.margin && ( ) } - /*/ } ); }