-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Grid Visualizer - improve styles #64321
base: trunk
Are you sure you want to change the base?
Changes from 19 commits
b74481f
995d04d
c260c5f
e3ad0c3
210b233
5f98335
ebcd385
9e3b9ae
6c3b96a
ca816ec
6d078c6
8ff3a22
03c068e
628ffb1
1e4c9e7
4386508
37c4203
497eb0f
3b4e9ae
98cb5de
ed7e1ed
6139488
e1343e7
4620f5e
f73ca9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,6 @@ const GridVisualizerGrid = forwardRef( | |
const [ gridInfo, setGridInfo ] = useState( () => | ||
getGridInfo( gridElement ) | ||
); | ||
const [ isDroppingAllowed, setIsDroppingAllowed ] = useState( false ); | ||
|
||
useEffect( () => { | ||
const observers = []; | ||
|
@@ -68,60 +67,59 @@ const GridVisualizerGrid = forwardRef( | |
}; | ||
}, [ gridElement ] ); | ||
|
||
useEffect( () => { | ||
function onGlobalDrag() { | ||
setIsDroppingAllowed( true ); | ||
} | ||
function onGlobalDragEnd() { | ||
setIsDroppingAllowed( false ); | ||
} | ||
document.addEventListener( 'drag', onGlobalDrag ); | ||
document.addEventListener( 'dragend', onGlobalDragEnd ); | ||
return () => { | ||
document.removeEventListener( 'drag', onGlobalDrag ); | ||
document.removeEventListener( 'dragend', onGlobalDragEnd ); | ||
}; | ||
}, [] ); | ||
|
||
return ( | ||
<BlockPopoverCover | ||
className={ clsx( 'block-editor-grid-visualizer', { | ||
'is-dropping-allowed': isDroppingAllowed, | ||
} ) } | ||
clientId={ gridClientId } | ||
__unstablePopoverSlot="__unstable-block-tools-after" | ||
> | ||
<div | ||
ref={ ref } | ||
className="block-editor-grid-visualizer__grid" | ||
style={ gridInfo.style } | ||
> | ||
{ isManualGrid ? ( | ||
<ManualGridVisualizer | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
/> | ||
) : ( | ||
Array.from( { length: gridInfo.numItems }, ( _, i ) => ( | ||
<GridVisualizerCell | ||
key={ i } | ||
color={ gridInfo.currentColor } | ||
/> | ||
) ) | ||
) } | ||
</div> | ||
</BlockPopoverCover> | ||
<> | ||
<GridPopunder | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
isManualGrid={ isManualGrid } | ||
/> | ||
{ isManualGrid && ( | ||
<GridPopover | ||
ref={ ref } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a bug here caused by Kapture.2024-08-12.at.15.42.13.mp4There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed in f73ca9f. The annoying aspect is that it means some parts of To properly finish #64162, the auto grid will also need a single appender in the next available cell, so something like this will be needed anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we can ignore that tbh. It's better imo to have it consistent across container blocks. |
||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
/> | ||
) } | ||
</> | ||
); | ||
} | ||
); | ||
|
||
function ManualGridVisualizer( { gridClientId, gridInfo } ) { | ||
/** | ||
* A popover component that renders in a slot over the grid block. | ||
* | ||
* This provides interactive elements of the grid visualization — | ||
* block inserters and drop zones. | ||
* | ||
* @param {Object} props | ||
* @param {string} props.gridClientId | ||
* @param {Object} props.gridInfo | ||
*/ | ||
const GridPopover = forwardRef( ( { gridClientId, gridInfo }, ref ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const [ isDroppingAllowed, setIsDroppingAllowed ] = useState( false ); | ||
const [ highlightedRect, setHighlightedRect ] = useState( null ); | ||
|
||
const gridItems = useSelect( | ||
( select ) => select( blockEditorStore ).getBlocks( gridClientId ), | ||
[ gridClientId ] | ||
); | ||
|
||
useEffect( () => { | ||
function onGlobalDrag() { | ||
setIsDroppingAllowed( true ); | ||
} | ||
function onGlobalDragEnd() { | ||
setIsDroppingAllowed( false ); | ||
} | ||
document.addEventListener( 'drag', onGlobalDrag ); | ||
document.addEventListener( 'dragend', onGlobalDragEnd ); | ||
return () => { | ||
document.removeEventListener( 'drag', onGlobalDrag ); | ||
document.removeEventListener( 'dragend', onGlobalDragEnd ); | ||
}; | ||
}, [] ); | ||
|
||
const occupiedRects = useMemo( () => { | ||
const rects = []; | ||
for ( const block of gridItems ) { | ||
|
@@ -146,56 +144,117 @@ function ManualGridVisualizer( { gridClientId, gridInfo } ) { | |
return rects; | ||
}, [ gridItems ] ); | ||
|
||
return range( 1, gridInfo.numRows ).map( ( row ) => | ||
range( 1, gridInfo.numColumns ).map( ( column ) => { | ||
const isCellOccupied = occupiedRects.some( ( rect ) => | ||
rect.contains( column, row ) | ||
); | ||
const isHighlighted = | ||
highlightedRect?.contains( column, row ) ?? false; | ||
return ( | ||
<GridVisualizerCell | ||
key={ `${ row }-${ column }` } | ||
color={ gridInfo.currentColor } | ||
className={ isHighlighted && 'is-highlighted' } | ||
> | ||
{ isCellOccupied ? ( | ||
<GridVisualizerDropZone | ||
column={ column } | ||
row={ row } | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
setHighlightedRect={ setHighlightedRect } | ||
/> | ||
) : ( | ||
<GridVisualizerAppender | ||
column={ column } | ||
row={ row } | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
setHighlightedRect={ setHighlightedRect } | ||
/> | ||
) } | ||
</GridVisualizerCell> | ||
); | ||
} ) | ||
return ( | ||
<BlockPopoverCover | ||
__unstablePopoverSlot="__unstable-block-tools-after" | ||
className={ clsx( 'block-editor-grid-visualizer', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class names in this file have come out of sync with the component names. It used to be that Also, is it bad that both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I've pushed a commit that updates these. Also removed another unused classname. It's not perfect, but I think good enough right now. |
||
'is-dropping-allowed': isDroppingAllowed, | ||
} ) } | ||
clientId={ gridClientId } | ||
> | ||
<div | ||
ref={ ref } | ||
className="block-editor-grid-visualizer__grid" | ||
style={ gridInfo.style } | ||
> | ||
{ range( 1, gridInfo.numRows ).map( ( row ) => | ||
range( 1, gridInfo.numColumns ).map( ( column ) => { | ||
const isCellOccupied = occupiedRects.some( ( rect ) => | ||
rect.contains( column, row ) | ||
); | ||
const isHighlighted = | ||
highlightedRect?.contains( column, row ) ?? false; | ||
|
||
return ( | ||
<div | ||
key={ `${ row }-${ column }` } | ||
className={ clsx( | ||
'block-editor-grid-visualizer__cell', | ||
{ | ||
'is-highlighted': isHighlighted, | ||
} | ||
) } | ||
> | ||
{ isCellOccupied ? ( | ||
<GridVisualizerDropZone | ||
column={ column } | ||
row={ row } | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
setHighlightedRect={ | ||
setHighlightedRect | ||
} | ||
/> | ||
) : ( | ||
<GridVisualizerAppender | ||
column={ column } | ||
row={ row } | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
setHighlightedRect={ | ||
setHighlightedRect | ||
} | ||
/> | ||
) } | ||
</div> | ||
); | ||
} ) | ||
) } | ||
</div> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
} ); | ||
|
||
function GridVisualizerCell( { color, children, className } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to get rid of In |
||
return ( | ||
<div | ||
className={ clsx( | ||
'block-editor-grid-visualizer__cell', | ||
className | ||
) } | ||
style={ { | ||
boxShadow: `inset 0 0 0 1px color-mix(in srgb, ${ color } 20%, #0000)`, | ||
/** | ||
* A popover component that renders inline under the grid block. | ||
* | ||
* This provides non-interactive elements of the grid visualization and | ||
* renders under the block so that the background colors are not atop | ||
* the block content. | ||
* | ||
* @param {Object} props | ||
* @param {string} props.gridClientId | ||
* @param {Object} props.gridInfo | ||
* @param {boolean} props.isManualGrid | ||
*/ | ||
function GridPopunder( { gridClientId, gridInfo, isManualGrid } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const color = gridInfo.currentColor; | ||
const cellStyle = isManualGrid | ||
? { | ||
backgroundColor: `rgba(var(--wp-admin-theme-color--rgb), 0.2)`, | ||
border: `1px dashed rgb(var(--wp-admin-theme-color--rgb))`, | ||
borderRadius: '2px', | ||
color, | ||
} } | ||
opacity: 0.2, | ||
} | ||
: { | ||
border: `1px dashed ${ color }`, | ||
borderRadius: '2px', | ||
color, | ||
opacity: 0.2, | ||
}; | ||
|
||
return ( | ||
<BlockPopoverCover | ||
inline | ||
className="block-editor-grid-visualizer" | ||
clientId={ gridClientId } | ||
// Override layout margin and popover's zIndex. | ||
contentStyle={ { margin: 0, zIndex: 0 } } | ||
> | ||
{ children } | ||
</div> | ||
<div | ||
className="block-editor-grid-visualizer__grid" | ||
style={ gridInfo.style } | ||
> | ||
{ Array.from( { length: gridInfo.numItems }, ( _, i ) => ( | ||
<div | ||
key={ i } | ||
className="block-editor-grid-visualizer__cell" | ||
style={ cellStyle } | ||
></div> | ||
) ) } | ||
</div> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,6 @@ | |
} | ||
} | ||
|
||
.block-editor-grid-visualizer__grid { | ||
display: grid; | ||
} | ||
|
||
.block-editor-grid-visualizer__cell { | ||
display: grid; | ||
position: relative; | ||
|
@@ -37,7 +33,7 @@ | |
overflow: hidden; | ||
|
||
.block-editor-grid-visualizer__appender { | ||
box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 20%, #0000); | ||
box-shadow: none; | ||
color: inherit; | ||
overflow: hidden; | ||
height: 100%; | ||
|
@@ -48,17 +44,26 @@ | |
|
||
} | ||
|
||
.block-editor-grid-visualizer__drop-zone { | ||
opacity: 0; | ||
} | ||
|
||
&.is-highlighted { | ||
.block-editor-inserter, | ||
.block-editor-grid-visualizer__drop-zone { | ||
background: var(--wp-admin-theme-color); | ||
background: rgba(var(--wp-admin-theme-color--rgb), 0.25); | ||
border: 1px solid var(--wp-admin-theme-color); | ||
} | ||
} | ||
|
||
&:hover .block-editor-grid-visualizer__appender, | ||
.block-editor-grid-visualizer__appender:focus { | ||
opacity: 1; | ||
background-color: color-mix(in srgb, currentColor 20%, #0000); | ||
box-shadow: none; | ||
background-color: rgba(var(--wp-admin-theme-color--rgb), 0.05); | ||
border: 1px dashed; | ||
border-radius: 2px; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yep, that was lazy. Now updated. |
||
color: var(--wp-admin-theme-color); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has all moved into
GridPopover