Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MarginVisualizer and PaddingVisualizer #59227

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
DimensionsPanel as StylesDimensionsPanel,
useHasDimensionsPanel,
} from '../components/global-styles';
import { MarginVisualizer } from './margin';
import { PaddingVisualizer } from './padding';
import { MarginVisualizer, PaddingVisualizer } from './spacing-visualizer';
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';
import { cleanEmptyObject, shouldSkipSerialization } from './utils';
Expand Down
88 changes: 0 additions & 88 deletions packages/block-editor/src/hooks/margin.js

This file was deleted.

79 changes: 0 additions & 79 deletions packages/block-editor/src/hooks/padding.js

This file was deleted.

126 changes: 126 additions & 0 deletions packages/block-editor/src/hooks/spacing-visualizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* WordPress dependencies
*/
import {
useState,
useRef,
useLayoutEffect,
useEffect,
useReducer,
} 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 SpacingVisualizer( { clientId, value, computeStyle, forceShow } ) {
const blockElement = useBlockElement( clientId );
const [ style, updateStyle ] = useReducer( () =>
computeStyle( blockElement )
);

useLayoutEffect( () => {
if ( ! blockElement ) {
return;
}
// It's not sufficient to read the computed spacing value when value.spacing changes as
// useEffect may run before the browser recomputes CSS. We therefore combine
// useLayoutEffect and two rAF calls to ensure that we read the spacing after the current
// paint but before the next paint.
// See https://github.com/WordPress/gutenberg/pull/59227.
window.requestAnimationFrame( () =>
window.requestAnimationFrame( updateStyle )
);
}, [ blockElement, value ] );

const previousValue = useRef( value );
const [ isActive, setIsActive ] = useState( false );

useEffect( () => {
if ( isShallowEqual( value, previousValue.current ) || forceShow ) {
return;
}

setIsActive( true );
previousValue.current = value;

const timeout = setTimeout( () => {
setIsActive( false );
}, 400 );

return () => {
setIsActive( false );
clearTimeout( timeout );
};
}, [ value, forceShow ] );

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

return (
<BlockPopoverCover
clientId={ clientId }
__unstablePopoverSlot="block-toolbar"
>
<div className="block-editor__spacing-visualizer" style={ style } />
</BlockPopoverCover>
);
}

function getComputedCSS( element, property ) {
return element.ownerDocument.defaultView
.getComputedStyle( element )
.getPropertyValue( property );
}

export function MarginVisualizer( { clientId, value, forceShow } ) {
return (
<SpacingVisualizer
clientId={ clientId }
value={ value?.spacing?.margin }
computeStyle={ ( blockElement ) => {
const top = getComputedCSS( blockElement, 'margin-top' );
const right = getComputedCSS( blockElement, 'margin-right' );
const bottom = getComputedCSS( blockElement, 'margin-bottom' );
const left = getComputedCSS( blockElement, 'margin-left' );
return {
borderTopWidth: top,
borderRightWidth: right,
borderBottomWidth: bottom,
borderLeftWidth: left,
top: top ? `-${ top }` : 0,
right: right ? `-${ right }` : 0,
bottom: bottom ? `-${ bottom }` : 0,
left: left ? `-${ left }` : 0,
};
} }
forceShow={ forceShow }
/>
);
}

export function PaddingVisualizer( { clientId, value, forceShow } ) {
return (
<SpacingVisualizer
clientId={ clientId }
value={ value?.spacing?.padding }
computeStyle={ ( blockElement ) => ( {
borderTopWidth: getComputedCSS( blockElement, 'padding-top' ),
borderRightWidth: getComputedCSS(
blockElement,
'padding-right'
),
borderBottomWidth: getComputedCSS(
blockElement,
'padding-bottom'
),
borderLeftWidth: getComputedCSS( blockElement, 'padding-left' ),
} ) }
forceShow={ forceShow }
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.block-editor__padding-visualizer {
.block-editor__spacing-visualizer {
position: absolute;
top: 0;
bottom: 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
@import "./hooks/color.scss";
@import "./hooks/dimensions.scss";
@import "./hooks/layout.scss";
@import "./hooks/padding.scss";
@import "./hooks/position.scss";
@import "./hooks/spacing.scss";
@import "./hooks/typography.scss";

@import "./components/block-toolbar/style.scss";
Expand Down
Loading