Skip to content

Commit

Permalink
Update GridVisualizer when grid or its children resize
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Feb 20, 2024
1 parent e6567ab commit a036614
Showing 1 changed file with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand All @@ -10,6 +15,51 @@ export function GridVisualizer( { clientId } ) {
if ( ! blockElement ) {
return null;
}
return (
<BlockPopover
className="block-editor-grid-visualizer"
clientId={ clientId }
__unstableCoverTarget
__unstablePopoverSlot="block-toolbar"
shift={ false }
>
<GridVisualizerGrid blockElement={ blockElement } />
</BlockPopover>
);
}

function GridVisualizerGrid( { blockElement } ) {
const [ gridInfo, setGridInfo ] = useState( () =>
getGridInfo( blockElement )
);
useEffect( () => {
const observers = [];
for ( const element of [ blockElement, ...blockElement.children ] ) {
const observer = new window.ResizeObserver( () => {
setGridInfo( getGridInfo( blockElement ) );
} );
observer.observe( element );
observers.push( observer );
}
return () => {
for ( const observer of observers ) {
observer.disconnect();
}
};
}, [ blockElement ] );
return (
<div
className="block-editor-grid-visualizer__grid"
style={ gridInfo.style }
>
{ Array.from( { length: gridInfo.numItems }, ( _, i ) => (
<div key={ i } className="block-editor-grid-visualizer__item" />
) ) }
</div>
);
}

function getGridInfo( blockElement ) {
const gridTemplateColumns = getComputedCSS(
blockElement,
'grid-template-columns'
Expand All @@ -21,30 +71,13 @@ export function GridVisualizer( { clientId } ) {
const numColumns = gridTemplateColumns.split( ' ' ).length;
const numRows = gridTemplateRows.split( ' ' ).length;
const numItems = numColumns * numRows;
return (
<BlockPopover
className="block-editor-grid-visualizer"
clientId={ clientId }
__unstableCoverTarget
__unstablePopoverSlot="block-toolbar"
shift={ false }
>
<div
className="block-editor-grid-visualizer__grid"
style={ {
gridTemplateColumns,
gridTemplateRows,
gap: getComputedCSS( blockElement, 'gap' ),
padding: getComputedCSS( blockElement, 'padding' ),
} }
>
{ Array.from( { length: numItems }, ( _, i ) => (
<div
key={ i }
className="block-editor-grid-visualizer__item"
/>
) ) }
</div>
</BlockPopover>
);
return {
numItems,
style: {
gridTemplateColumns,
gridTemplateRows,
gap: getComputedCSS( blockElement, 'gap' ),
padding: getComputedCSS( blockElement, 'padding' ),
},
};
}

0 comments on commit a036614

Please sign in to comment.