-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Open
talldan
wants to merge
25
commits into
trunk
Choose a base branch
from
try/inline-grid-visualizer
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b74481f
Add support to root block list in `useBlockRefs`
talldan 995d04d
Add basic underlay component
talldan c260c5f
Only render GridVisualizer above GridBlock and no longer on LayoutChi…
talldan e3ad0c3
Remove outdated comment
talldan 210b233
Move padding to underlay to ensure it refreshes
talldan 5f98335
Handle block moving
talldan ebcd385
Add a background for cells
talldan 9e3b9ae
Use BlockPopoverCover with inline prop
talldan 6c3b96a
Allow margin reset on inline popover via inline styles
talldan ca816ec
Override popover default z index
talldan 6d078c6
Render two "grid visualizers", one under the block and one over-the o…
talldan 8ff3a22
Revert change to root block list
talldan 03c068e
Update grid styles
talldan 628ffb1
Update appender styles
talldan 1e4c9e7
Hide drop zone until active
talldan 4386508
Improve highlight style
talldan 37c4203
Refactor into separate GridPopunder / GridPopover components
talldan 497eb0f
Fix resizer bounds
talldan 3b4e9ae
Try for a smaller diff
talldan 98cb5de
Rename GridVisualizerGrid, move occupiedRects
noisysocks ed7e1ed
Update grid css
talldan 6139488
Move GridResizerBoundsContext.Provider rendering to `GridTools` and m…
talldan e1343e7
Use scss variables
talldan 4620f5e
Rename popover prop to `style` instead of `contentStyle`
talldan f73ca9f
Fix: Resizer bounds not working for auto grids
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ export function GridVisualizer( { clientId, contentRef, parentLayout } ) { | |
parentLayout?.isManualPlacement && | ||
window.__experimentalEnableGridInteractivity; | ||
return ( | ||
<GridVisualizerGrid | ||
<GridVisualizerUI | ||
gridClientId={ clientId } | ||
gridElement={ gridElement } | ||
isManualGrid={ isManualGrid } | ||
|
@@ -45,12 +45,11 @@ export function GridVisualizer( { clientId, contentRef, parentLayout } ) { | |
); | ||
} | ||
|
||
const GridVisualizerGrid = forwardRef( | ||
const GridVisualizerUI = forwardRef( | ||
( { gridClientId, gridElement, isManualGrid }, ref ) => { | ||
const [ gridInfo, setGridInfo ] = useState( () => | ||
getGridInfo( gridElement ) | ||
); | ||
const [ isDroppingAllowed, setIsDroppingAllowed ] = useState( false ); | ||
|
||
useEffect( () => { | ||
const observers = []; | ||
|
@@ -68,6 +67,38 @@ const GridVisualizerGrid = forwardRef( | |
}; | ||
}, [ gridElement ] ); | ||
|
||
return ( | ||
<> | ||
<GridPopunder | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
isManualGrid={ isManualGrid } | ||
/> | ||
<GridPopover | ||
ref={ ref } | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
isManualGrid={ isManualGrid } | ||
/> | ||
</> | ||
); | ||
} | ||
); | ||
|
||
/** | ||
* 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, isManualGrid }, ref ) => { | ||
const [ isDroppingAllowed, setIsDroppingAllowed ] = useState( false ); | ||
|
||
useEffect( () => { | ||
function onGlobalDrag() { | ||
setIsDroppingAllowed( true ); | ||
|
@@ -85,43 +116,32 @@ const GridVisualizerGrid = forwardRef( | |
|
||
return ( | ||
<BlockPopoverCover | ||
className={ clsx( 'block-editor-grid-visualizer', { | ||
__unstablePopoverSlot="__unstable-block-tools-after" | ||
className={ clsx( 'block-editor-grid-visualizer-popover', { | ||
'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 | ||
<div ref={ ref } style={ gridInfo.style }> | ||
{ isManualGrid && ( | ||
<ManualGridPopoverItems | ||
gridClientId={ gridClientId } | ||
gridInfo={ gridInfo } | ||
/> | ||
) : ( | ||
Array.from( { length: gridInfo.numItems }, ( _, i ) => ( | ||
<GridVisualizerCell | ||
key={ i } | ||
color={ gridInfo.currentColor } | ||
/> | ||
) ) | ||
) } | ||
</div> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
); | ||
|
||
function ManualGridVisualizer( { gridClientId, gridInfo } ) { | ||
function ManualGridPopoverItems( { gridClientId, gridInfo } ) { | ||
const [ highlightedRect, setHighlightedRect ] = useState( null ); | ||
|
||
const gridItems = useSelect( | ||
( select ) => select( blockEditorStore ).getBlocks( gridClientId ), | ||
[ gridClientId ] | ||
); | ||
|
||
const occupiedRects = useMemo( () => { | ||
const rects = []; | ||
for ( const block of gridItems ) { | ||
|
@@ -153,11 +173,16 @@ function ManualGridVisualizer( { gridClientId, gridInfo } ) { | |
); | ||
const isHighlighted = | ||
highlightedRect?.contains( column, row ) ?? false; | ||
|
||
return ( | ||
<GridVisualizerCell | ||
<div | ||
key={ `${ row }-${ column }` } | ||
color={ gridInfo.currentColor } | ||
className={ isHighlighted && 'is-highlighted' } | ||
className={ clsx( | ||
'block-editor-grid-visualizer-popover__cell', | ||
{ | ||
'is-highlighted': isHighlighted, | ||
} | ||
) } | ||
> | ||
{ isCellOccupied ? ( | ||
<GridVisualizerDropZone | ||
|
@@ -176,26 +201,54 @@ function ManualGridVisualizer( { gridClientId, gridInfo } ) { | |
setHighlightedRect={ setHighlightedRect } | ||
/> | ||
) } | ||
</GridVisualizerCell> | ||
</div> | ||
); | ||
} ) | ||
); | ||
} | ||
|
||
function GridVisualizerCell( { color, children, className } ) { | ||
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 | ||
clientId={ gridClientId } | ||
// Override layout margin and popover's zIndex. | ||
style={ { margin: 0, zIndex: 0 } } | ||
> | ||
{ children } | ||
</div> | ||
<div style={ gridInfo.style }> | ||
{ Array.from( { length: gridInfo.numItems }, ( _, i ) => ( | ||
<div key={ i } style={ cellStyle } /> | ||
) ) } | ||
</div> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I decided to get rid of
GridVisualizerCell
as it wasn't doing very much, especially inGridPopover
where it's just a<div>
.In
GridPopunder
it's a<div>
with a some styles, so I think it still doesn't need its own component.