Repository navigation
Add Grid interactivity #59052
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
Add Grid interactivity #59052
Changes from all commits
956b676
c7f6986
185dc1a
3ce2c39
895b33d
088cf0f
a8deab4
0a22979
0bd1e2c
4db7ab0
e6567ab
a036614
df65b36
cbc5b50
e1c2c92
f0c111a
4e50848
e48fe99
fad46c9
fcdbc90
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { ResizableBox } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
| import BlockPopoverCover from '../block-popover/cover'; | ||
| import { getComputedCSS } from './utils'; | ||
|
|
||
| export function GridItemResizer( { clientId, onChange } ) { | ||
| const blockElement = useBlockElement( clientId ); | ||
| if ( ! blockElement ) { | ||
| return null; | ||
| } | ||
| return ( | ||
| <BlockPopoverCover | ||
| className="block-editor-grid-item-resizer" | ||
| clientId={ clientId } | ||
| __unstablePopoverSlot="block-toolbar" | ||
| > | ||
| <ResizableBox | ||
| className="block-editor-grid-item-resizer__box" | ||
| size={ { | ||
| width: '100%', | ||
| height: '100%', | ||
| } } | ||
| enable={ { | ||
| bottom: true, | ||
| bottomLeft: false, | ||
| bottomRight: false, | ||
| left: false, | ||
| right: true, | ||
| top: false, | ||
| topLeft: false, | ||
| topRight: false, | ||
|
Contributor
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. So what happens if we enable "left" and "top"? I'm guessing it breaks somehow? 😄
Member
Author
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 there's a bug in |
||
| } } | ||
| onResizeStop={ ( event, direction, boxElement ) => { | ||
| const gridElement = blockElement.parentElement; | ||
| const columnGap = parseFloat( | ||
| getComputedCSS( gridElement, 'column-gap' ) | ||
| ); | ||
| const rowGap = parseFloat( | ||
| getComputedCSS( gridElement, 'row-gap' ) | ||
| ); | ||
| const gridColumnLines = getGridLines( | ||
| getComputedCSS( gridElement, 'grid-template-columns' ), | ||
| columnGap | ||
| ); | ||
| const gridRowLines = getGridLines( | ||
| getComputedCSS( gridElement, 'grid-template-rows' ), | ||
| rowGap | ||
| ); | ||
| const columnStart = getClosestLine( | ||
| gridColumnLines, | ||
| blockElement.offsetLeft | ||
| ); | ||
| const rowStart = getClosestLine( | ||
| gridRowLines, | ||
| blockElement.offsetTop | ||
| ); | ||
| const columnEnd = getClosestLine( | ||
| gridColumnLines, | ||
| blockElement.offsetLeft + boxElement.offsetWidth | ||
| ); | ||
| const rowEnd = getClosestLine( | ||
| gridRowLines, | ||
| blockElement.offsetTop + boxElement.offsetHeight | ||
| ); | ||
|
noisysocks marked this conversation as resolved.
Resolved
|
||
| onChange( { | ||
| columnSpan: Math.max( columnEnd - columnStart, 1 ), | ||
| rowSpan: Math.max( rowEnd - rowStart, 1 ), | ||
| } ); | ||
| } } | ||
| /> | ||
| </BlockPopoverCover> | ||
| ); | ||
| } | ||
|
|
||
| function getGridLines( template, gap ) { | ||
| const lines = [ 0 ]; | ||
| for ( const size of template.split( ' ' ) ) { | ||
| const line = parseFloat( size ); | ||
| lines.push( lines[ lines.length - 1 ] + line + gap ); | ||
| } | ||
| return lines; | ||
| } | ||
|
|
||
| function getClosestLine( lines, position ) { | ||
| return lines.reduce( | ||
| ( closest, line, index ) => | ||
| Math.abs( line - position ) < | ||
| Math.abs( lines[ closest ] - position ) | ||
| ? index | ||
| : closest, | ||
| 0 | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useState, useEffect } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
| import BlockPopoverCover from '../block-popover/cover'; | ||
| import { getComputedCSS } from './utils'; | ||
|
|
||
| export function GridVisualizer( { clientId } ) { | ||
| const blockElement = useBlockElement( clientId ); | ||
| if ( ! blockElement ) { | ||
| return null; | ||
| } | ||
| return ( | ||
| <BlockPopoverCover | ||
| className="block-editor-grid-visualizer" | ||
| clientId={ clientId } | ||
| __unstablePopoverSlot="block-toolbar" | ||
| > | ||
| <GridVisualizerGrid blockElement={ blockElement } /> | ||
| </BlockPopoverCover> | ||
| ); | ||
| } | ||
|
|
||
| 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' | ||
| ); | ||
| const gridTemplateRows = getComputedCSS( | ||
| blockElement, | ||
| 'grid-template-rows' | ||
| ); | ||
| const numColumns = gridTemplateColumns.split( ' ' ).length; | ||
| const numRows = gridTemplateRows.split( ' ' ).length; | ||
| const numItems = numColumns * numRows; | ||
| return { | ||
| numItems, | ||
| style: { | ||
| gridTemplateColumns, | ||
| gridTemplateRows, | ||
| gap: getComputedCSS( blockElement, 'gap' ), | ||
| padding: getComputedCSS( blockElement, 'padding' ), | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { GridVisualizer } from './grid-visualizer'; | ||
| export { GridItemResizer } from './grid-item-resizer'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // TODO: Specificity hacks to get rid of all these darn !importants. | ||
|
|
||
| .block-editor-grid-visualizer { | ||
| z-index: z-index(".block-editor-grid-visualizer") !important; | ||
| } | ||
|
|
||
| .block-editor-grid-visualizer .components-popover__content * { | ||
| pointer-events: none !important; | ||
| } | ||
|
|
||
| .block-editor-grid-visualizer__grid { | ||
| display: grid; | ||
| } | ||
|
|
||
| .block-editor-grid-visualizer__item { | ||
| border: $border-width dashed $gray-300; | ||
| } | ||
|
|
||
| .block-editor-grid-item-resizer { | ||
| z-index: z-index(".block-editor-grid-visualizer") !important; | ||
| } | ||
|
|
||
| .block-editor-grid-item-resizer .components-popover__content * { | ||
| pointer-events: none !important; | ||
| } | ||
|
|
||
| .block-editor-grid-item-resizer__box { | ||
| border: $border-width solid var(--wp-admin-theme-color); | ||
|
|
||
| .components-resizable-box__handle { | ||
| pointer-events: all !important; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export function getComputedCSS( element, property ) { | ||
|
Contributor
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. This is the third instance of this function in the block-editor package, might be worth consolidating (as a separate task)
Member
Author
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 refactor in #59227 brings it down to 2! Getting close... 😂 |
||
| return element.ownerDocument.defaultView | ||
| .getComputedStyle( element ) | ||
| .getPropertyValue( property ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.