Skip to content

Commit

Permalink
Spacing visualizer: add option to trigger with mousover as well as va…
Browse files Browse the repository at this point in the history
…lue change (#44955)
  • Loading branch information
glendaviesnz committed Oct 19, 2022
1 parent 5a82cf5 commit a61f7d6
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default function AllInputControl( {
spacingSizes,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
} ) {
const allValue = getAllRawValue( values );
const hasValues = isValuesDefined( values );
Expand All @@ -35,6 +37,8 @@ export default function AllInputControl( {
isMixed={ isMixed }
type={ type }
minimumCustomValue={ minimumCustomValue }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default function AxialInputControls( {
spacingSizes,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
} ) {
const createHandleOnChange = ( side ) => ( next ) => {
if ( ! onChange ) {
Expand Down Expand Up @@ -54,6 +56,8 @@ export default function AxialInputControls( {
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default function SpacingSizesControl( {
splitOnAxis = false,
useSelect,
minimumCustomValue = 0,
onMouseOver,
onMouseOut,
} ) {
const spacingSizes = [
{ name: 0, slug: '0', size: 0 },
Expand Down Expand Up @@ -70,6 +72,8 @@ export default function SpacingSizesControl( {
useSelect,
type: label,
minimumCustomValue,
onMouseOver,
onMouseOut,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function BoxInputControls( {
spacingSizes,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
} ) {
// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
Expand Down Expand Up @@ -38,6 +40,8 @@ export default function BoxInputControls( {
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default function SpacingInputControl( {
isMixed = false,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
} ) {
// Treat value as a preset value if the passed in value matches the value of one of the spacingSizes.
value = getPresetValueFromCustomValue( value, spacingSizes );
Expand Down Expand Up @@ -218,6 +220,8 @@ export default function SpacingInputControl( {
{ showCustomValueControl && (
<>
<UnitControl
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
onChange={ ( newSize ) =>
onChange( getNewCustomValue( newSize ) )
}
Expand All @@ -234,6 +238,8 @@ export default function SpacingInputControl( {
/>

<RangeControl
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
value={ customRangeValue }
min={ 0 }
max={ CUSTOM_VALUE_SETTINGS[ selectedUnit ]?.max ?? 10 }
Expand All @@ -248,6 +254,8 @@ export default function SpacingInputControl( {
) }
{ showRangeControl && ! showCustomValueControl && (
<RangeControl
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
className="components-spacing-sizes-control__range-control"
value={ currentValue }
onChange={ ( newSize ) =>
Expand Down Expand Up @@ -293,6 +301,8 @@ export default function SpacingInputControl( {
hideLabelFromVision={ true }
__nextUnconstrainedWidth={ true }
size={ '__unstable-large' }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
) }
</>
Expand Down
37 changes: 32 additions & 5 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __experimentalToolsPanelItem as ToolsPanelItem } from '@wordpress/components';
import { Platform } from '@wordpress/element';
import { Platform, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getBlockSupport } from '@wordpress/blocks';

Expand Down Expand Up @@ -44,6 +44,13 @@ 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 );
const onMouseOver = () => setIsMouseOver( true );
const onMouseOut = () => setIsMouseOver( false );
return { isMouseOver, onMouseOver, onMouseOut };
}

/**
* Inspector controls for dimensions support.
*
Expand All @@ -58,6 +65,8 @@ export function DimensionsPanel( props ) {
const isDisabled = useIsDimensionsDisabled( props );
const isSupported = hasDimensionsSupport( props.name );
const spacingSizes = useSetting( 'spacing.spacingSizes' );
const paddingMouseOver = useVisualizerMouseOver();
const marginMouseOver = useVisualizerMouseOver();

if ( isDisabled || ! isSupported ) {
return null;
Expand Down Expand Up @@ -96,7 +105,11 @@ export function DimensionsPanel( props ) {
isShownByDefault={ defaultSpacingControls?.padding }
panelId={ props.clientId }
>
<PaddingEdit { ...props } />
<PaddingEdit
onMouseOver={ paddingMouseOver.onMouseOver }
onMouseOut={ paddingMouseOver.onMouseOut }
{ ...props }
/>
</ToolsPanelItem>
) }
{ ! isMarginDisabled && (
Expand All @@ -109,7 +122,11 @@ export function DimensionsPanel( props ) {
isShownByDefault={ defaultSpacingControls?.margin }
panelId={ props.clientId }
>
<MarginEdit { ...props } />
<MarginEdit
onMouseOver={ marginMouseOver.onMouseOver }
onMouseOut={ marginMouseOver.onMouseOut }
{ ...props }
/>
</ToolsPanelItem>
) }
{ ! isGapDisabled && (
Expand All @@ -126,8 +143,18 @@ export function DimensionsPanel( props ) {
</ToolsPanelItem>
) }
</InspectorControls>
{ ! isPaddingDisabled && <PaddingVisualizer { ...props } /> }
{ ! isMarginDisabled && <MarginVisualizer { ...props } /> }
{ ! isPaddingDisabled && (
<PaddingVisualizer
forceShow={ paddingMouseOver.isMouseOver }
{ ...props }
/>
) }
{ ! isMarginDisabled && (
<MarginVisualizer
forceShow={ marginMouseOver.isMouseOver }
{ ...props }
/>
) }
</>
);
}
Expand Down
33 changes: 19 additions & 14 deletions packages/block-editor/src/hooks/margin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { cleanEmptyObject } from './utils';
import BlockPopover from '../components/block-popover';
import SpacingSizesControl from '../components/spacing-sizes-control';
import { getCustomValueFromPreset } from '../components/spacing-sizes-control/utils';
import { getSpacingPresetCssVar } from '../components/spacing-sizes-control/utils';

/**
* Determines if there is margin support.
Expand Down Expand Up @@ -101,6 +101,8 @@ export function MarginEdit( props ) {
name: blockName,
attributes: { style },
setAttributes,
onMouseOver,
onMouseOut,
} = props;

const spacingSizes = useSetting( 'spacing.spacingSizes' );
Expand Down Expand Up @@ -148,6 +150,8 @@ export function MarginEdit( props ) {
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
) }
{ spacingSizes?.length > 0 && (
Expand All @@ -159,6 +163,8 @@ export function MarginEdit( props ) {
units={ units }
allowReset={ false }
splitOnAxis={ false }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
) }
</>
Expand All @@ -167,33 +173,32 @@ export function MarginEdit( props ) {
} );
}

export function MarginVisualizer( { clientId, attributes } ) {
export function MarginVisualizer( { clientId, attributes, forceShow } ) {
const margin = attributes?.style?.spacing?.margin;
const spacingSizes = useSetting( 'spacing.spacingSizes' );

const style = useMemo( () => {
const marginTop = margin?.top
? getCustomValueFromPreset( margin?.top, spacingSizes )
? getSpacingPresetCssVar( margin?.top )
: 0;
const marginRight = margin?.right
? getCustomValueFromPreset( margin?.right, spacingSizes )
? getSpacingPresetCssVar( margin?.right )
: 0;
const marginBottom = margin?.bottom
? getCustomValueFromPreset( margin?.bottom, spacingSizes )
? getSpacingPresetCssVar( margin?.bottom )
: 0;
const marginLeft = margin?.left
? getCustomValueFromPreset( margin?.left, spacingSizes )
? getSpacingPresetCssVar( margin?.left )
: 0;

return {
borderTopWidth: marginTop,
borderRightWidth: marginRight,
borderBottomWidth: marginBottom,
borderLeftWidth: marginLeft,
top: marginTop !== 0 ? `calc(${ marginTop } * -1)` : 0,
right: marginRight !== 0 ? `calc(${ marginRight } * -1)` : 0,
bottom: marginBottom !== 0 ? `calc(${ marginBottom } * -1)` : 0,
left: marginLeft !== 0 ? `calc(${ marginLeft } * -1)` : 0,
top: marginTop ? `calc(${ marginTop } * -1)` : 0,
right: marginRight ? `calc(${ marginRight } * -1)` : 0,
bottom: marginBottom ? `calc(${ marginBottom } * -1)` : 0,
left: marginLeft ? `calc(${ marginLeft } * -1)` : 0,
};
}, [ margin ] );

Expand All @@ -208,7 +213,7 @@ export function MarginVisualizer( { clientId, attributes } ) {
};

useEffect( () => {
if ( ! isShallowEqual( margin, valueRef.current ) ) {
if ( ! isShallowEqual( margin, valueRef.current ) && ! forceShow ) {
setIsActive( true );
valueRef.current = margin;

Expand All @@ -220,9 +225,9 @@ export function MarginVisualizer( { clientId, attributes } ) {
}

return () => clearTimer();
}, [ margin ] );
}, [ margin, forceShow ] );

if ( ! isActive ) {
if ( ! isActive && ! forceShow ) {
return null;
}

Expand Down
35 changes: 19 additions & 16 deletions packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ import {
import { cleanEmptyObject } from './utils';
import BlockPopover from '../components/block-popover';
import SpacingSizesControl from '../components/spacing-sizes-control';
import {
getSpacingPresetCssVar,
isValueSpacingPreset,
} from '../components/spacing-sizes-control/utils';
import { getSpacingPresetCssVar } from '../components/spacing-sizes-control/utils';
/**
* Determines if there is padding support.
*
Expand Down Expand Up @@ -103,6 +100,8 @@ export function PaddingEdit( props ) {
name: blockName,
attributes: { style },
setAttributes,
onMouseOver,
onMouseOut,
} = props;

const spacingSizes = useSetting( 'spacing.spacingSizes' );
Expand Down Expand Up @@ -150,6 +149,8 @@ export function PaddingEdit( props ) {
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
) }
{ spacingSizes?.length > 0 && (
Expand All @@ -161,6 +162,8 @@ export function PaddingEdit( props ) {
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
/>
) }
</>
Expand All @@ -169,22 +172,22 @@ export function PaddingEdit( props ) {
} );
}

export function PaddingVisualizer( { clientId, attributes } ) {
export function PaddingVisualizer( { clientId, attributes, forceShow } ) {
const padding = attributes?.style?.spacing?.padding;
const style = useMemo( () => {
return {
borderTopWidth: isValueSpacingPreset( padding?.top )
borderTopWidth: padding?.top
? getSpacingPresetCssVar( padding?.top )
: padding?.top,
borderRightWidth: isValueSpacingPreset( padding?.right )
: 0,
borderRightWidth: padding?.right
? getSpacingPresetCssVar( padding?.right )
: padding?.right,
borderBottomWidth: isValueSpacingPreset( padding?.bottom )
: 0,
borderBottomWidth: padding?.bottom
? getSpacingPresetCssVar( padding?.bottom )
: padding?.bottom,
borderLeftWidth: isValueSpacingPreset( padding?.left )
: 0,
borderLeftWidth: padding?.left
? getSpacingPresetCssVar( padding?.left )
: padding?.left,
: 0,
};
}, [ padding ] );

Expand All @@ -199,7 +202,7 @@ export function PaddingVisualizer( { clientId, attributes } ) {
};

useEffect( () => {
if ( ! isShallowEqual( padding, valueRef.current ) ) {
if ( ! isShallowEqual( padding, valueRef.current ) && ! forceShow ) {
setIsActive( true );
valueRef.current = padding;

Expand All @@ -211,9 +214,9 @@ export function PaddingVisualizer( { clientId, attributes } ) {
}

return () => clearTimer();
}, [ padding ] );
}, [ padding, forceShow ] );

if ( ! isActive ) {
if ( ! isActive && ! forceShow ) {
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- `BoxControl` & `CustomSelectControl`: Add `onMouseOver` and `onMouseOut` callback props to allow handling of these events by parent components ([#44955](https://github.com/WordPress/gutenberg/pull/44955))

## 21.3.0 (2022-10-19)

### Bug Fix
Expand Down
Loading

0 comments on commit a61f7d6

Please sign in to comment.