From fe399d792a72898543bc31de1b7ff95f71571721 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Wed, 18 May 2022 13:36:26 +1000 Subject: [PATCH 1/9] Move ResizableBox to BlockPopover for Cover block borders --- packages/base-styles/_z-index.scss | 4 +- packages/block-editor/src/components/index.js | 1 + .../components/resizable-box-popover/index.js | 25 ++++++ packages/block-library/src/cover/block.json | 12 +++ .../block-library/src/cover/edit/index.js | 81 ++++++++++--------- ...le-cover.js => resizable-cover-popover.js} | 18 +++-- packages/block-library/src/cover/editor.scss | 24 +++--- packages/block-library/src/cover/style.scss | 1 + 8 files changed, 107 insertions(+), 59 deletions(-) create mode 100644 packages/block-editor/src/components/resizable-box-popover/index.js rename packages/block-library/src/cover/edit/{resizable-cover.js => resizable-cover-popover.js} (73%) diff --git a/packages/base-styles/_z-index.scss b/packages/base-styles/_z-index.scss index d61403d702707..a85814b4b838a 100644 --- a/packages/base-styles/_z-index.scss +++ b/packages/base-styles/_z-index.scss @@ -33,8 +33,8 @@ $z-layers: ( ".interface-interface-skeleton__content": 20, ".edit-widgets-header": 30, ".block-library-button__inline-link .block-editor-url-input__suggestions": 6, // URL suggestions for button block above sibling inserter - ".wp-block-cover__inner-container": 1, // InnerBlocks area inside cover image block - ".wp-block-cover.is-placeholder .components-placeholder.is-large": 1, // Cover block resizer component inside a large placeholder. + ".wp-block-cover__inner-container": 32, // InnerBlocks area inside cover image block. Must be higher than block popover and less than drop zone. + ".wp-block-cover.is-placeholder .components-placeholder.is-large": 32, // Cover block resizer component inside a large placeholder. ".wp-block-cover.has-background-dim::before": 1, // Overlay area inside block cover need to be higher than the video background. ".block-library-cover__padding-visualizer": 2, // BoxControl visualizer needs to be +1 higher than .wp-block-cover.has-background-dim::before ".wp-block-cover__image-background": 0, // Image background inside cover block. diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index f5b2c17e87740..bb3f36c948e45 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -77,6 +77,7 @@ export { default as MediaUpload } from './media-upload'; export { default as MediaUploadCheck } from './media-upload/check'; export { default as PanelColorSettings } from './panel-color-settings'; export { default as PlainText } from './plain-text'; +export { default as __experimentalResizableBoxPopover } from './resizable-box-popover'; export { default as __experimentalResponsiveBlockControl } from './responsive-block-control'; export { default as RichText, diff --git a/packages/block-editor/src/components/resizable-box-popover/index.js b/packages/block-editor/src/components/resizable-box-popover/index.js new file mode 100644 index 0000000000000..d93f45faeec9d --- /dev/null +++ b/packages/block-editor/src/components/resizable-box-popover/index.js @@ -0,0 +1,25 @@ +/** + * WordPress dependencies + */ +import { ResizableBox } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import BlockPopover from '../block-popover'; + +export default function ResizableCoverPopover( { + clientId, + __unstableRefreshSize, + ...props +} ) { + return ( + + + + ); +} diff --git a/packages/block-library/src/cover/block.json b/packages/block-library/src/cover/block.json index 0d272ab4e4cff..8c7cb0f9143c6 100644 --- a/packages/block-library/src/cover/block.json +++ b/packages/block-library/src/cover/block.json @@ -94,6 +94,18 @@ "blockGap": true } }, + "__experimentalBorder": { + "color": true, + "radius": true, + "style": true, + "width": true, + "__experimentalDefaultControls": { + "color": true, + "radius": true, + "style": true, + "width": true + } + }, "color": { "__experimentalDuotone": "> .wp-block-cover__image-background, > .wp-block-cover__video-background", "text": true, diff --git a/packages/block-library/src/cover/edit/index.js b/packages/block-library/src/cover/edit/index.js index a74ebe8fe18eb..930d0a71890c5 100644 --- a/packages/block-library/src/cover/edit/index.js +++ b/packages/block-library/src/cover/edit/index.js @@ -9,9 +9,9 @@ import namesPlugin from 'colord/plugins/names'; * WordPress dependencies */ import { useEntityProp, store as coreStore } from '@wordpress/core-data'; -import { useEffect, useRef } from '@wordpress/element'; +import { useEffect, useMemo, useRef } from '@wordpress/element'; import { Placeholder, Spinner } from '@wordpress/components'; -import { compose } from '@wordpress/compose'; +import { compose, useResizeObserver } from '@wordpress/compose'; import { withColors, ColorPalette, @@ -42,7 +42,7 @@ import useCoverIsDark from './use-cover-is-dark'; import CoverInspectorControls from './inspector-controls'; import CoverBlockControls from './block-controls'; import CoverPlaceholder from './cover-placeholder'; -import ResizableCover from './resizable-cover'; +import ResizableCoverPopover from './resizable-cover-popover'; extend( [ namesPlugin ] ); @@ -146,6 +146,14 @@ function CoverEdit( { const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType; const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType; + const [ resizeListener, { height, width } ] = useResizeObserver(); + const resizableBoxDimensions = useMemo( () => { + return { + height: minHeight ? parseFloat( minHeight ) : 'auto', + width: 'auto', + }; + }, [ minHeight ] ); + const minHeightWithUnit = minHeight && minHeightUnit ? `${ minHeight }${ minHeightUnit }` @@ -246,24 +254,50 @@ function CoverEdit( { /> ); + const resizableCoverProps = { + className: 'block-library-cover__resize-container', + clientId, + height, + minHeight: parseFloat( minHeight ), + onResizeStart: () => { + setAttributes( { minHeightUnit: 'px' } ); + toggleSelection( false ); + }, + onResize: ( value ) => { + setAttributes( { minHeight: value } ); + }, + onResizeStop: ( newMinHeight ) => { + toggleSelection( true ); + setAttributes( { minHeight: newMinHeight } ); + }, + showHandle: true, + size: resizableBoxDimensions, + width, + }; + if ( ! useFeaturedImage && ! hasInnerBlocks && ! hasBackground ) { return ( <> { blockControls } { inspectorControls } + { isSelected && ( + + ) } + { resizeListener } @@ -275,21 +309,6 @@ function CoverEdit( { /> - { - setAttributes( { minHeightUnit: 'px' } ); - toggleSelection( false ); - } } - onResize={ ( value ) => { - setAttributes( { minHeight: value } ); - } } - onResizeStop={ ( newMinHeight ) => { - toggleSelection( true ); - setAttributes( { minHeight: newMinHeight } ); - } } - showHandle={ isSelected } - /> > ); @@ -318,22 +337,7 @@ function CoverEdit( { style={ { ...style, ...blockProps.style } } data-url={ url } > - { - setAttributes( { minHeightUnit: 'px' } ); - toggleSelection( false ); - } } - onResize={ ( value ) => { - setAttributes( { minHeight: value } ); - } } - onResizeStop={ ( newMinHeight ) => { - toggleSelection( true ); - setAttributes( { minHeight: newMinHeight } ); - } } - showHandle={ isSelected } - /> - + { resizeListener } { ( ! useFeaturedImage || url ) && ( + { isSelected && ( + + ) } > ); } diff --git a/packages/block-library/src/cover/edit/resizable-cover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js similarity index 73% rename from packages/block-library/src/cover/edit/resizable-cover.js rename to packages/block-library/src/cover/edit/resizable-cover-popover.js index 7029e5c997e46..3f5950912f5d7 100644 --- a/packages/block-library/src/cover/edit/resizable-cover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -6,8 +6,8 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { useState } from '@wordpress/element'; -import { ResizableBox } from '@wordpress/components'; +import { useMemo, useState } from '@wordpress/element'; +import { __experimentalResizableBoxPopover as ResizableBoxPopover } from '@wordpress/block-editor'; const RESIZABLE_BOX_ENABLE_OPTION = { top: false, @@ -20,17 +20,25 @@ const RESIZABLE_BOX_ENABLE_OPTION = { topLeft: false, }; -export default function ResizableCover( { +export default function ResizableCoverPopover( { className, - onResizeStart, + height, + minHeight, onResize, + onResizeStart, onResizeStop, + width, ...props } ) { const [ isResizing, setIsResizing ] = useState( false ); + const dimensions = useMemo( + () => ( { height, minHeight, width } ), + [ minHeight, height, width ] + ); return ( - .components-drop-zone .components-drop-zone__content { opacity: 0.8 !important; diff --git a/packages/block-library/src/cover/style.scss b/packages/block-library/src/cover/style.scss index cbd2c0d247093..e562ee87ad782 100644 --- a/packages/block-library/src/cover/style.scss +++ b/packages/block-library/src/cover/style.scss @@ -11,6 +11,7 @@ box-sizing: border-box; // Keep the flex layout direction to the physical direction (LTR) in RTL languages. /*rtl:raw: direction: ltr; */ + overflow: hidden; /** * Set a default background color for has-background-dim _unless_ it includes another From c3dac562559ef7c893b9ea7ab3fb0512a88fd1a2 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 9 Mar 2023 16:03:32 +1000 Subject: [PATCH 2/9] Fix resizable box popover --- .../block-editor/src/components/resizable-box-popover/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-editor/src/components/resizable-box-popover/index.js b/packages/block-editor/src/components/resizable-box-popover/index.js index d93f45faeec9d..23043535cb16b 100644 --- a/packages/block-editor/src/components/resizable-box-popover/index.js +++ b/packages/block-editor/src/components/resizable-box-popover/index.js @@ -18,6 +18,8 @@ export default function ResizableCoverPopover( { clientId={ clientId } __unstableCoverTarget __unstableRefreshSize={ __unstableRefreshSize } + __unstablePopoverSlot="block-toolbar" + shift={ false } > From fae68e65fa7d90c16eb7f4bd637f6b7dac62241d Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 9 Mar 2023 17:14:43 +1000 Subject: [PATCH 3/9] Fix issue preventing inner block selection --- .../components/resizable-box-popover/index.js | 8 +-- .../src/cover/edit/resizable-cover-popover.js | 51 ++++++++++--------- packages/block-library/src/cover/editor.scss | 13 +++++ 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/packages/block-editor/src/components/resizable-box-popover/index.js b/packages/block-editor/src/components/resizable-box-popover/index.js index 23043535cb16b..c2400dbe07f91 100644 --- a/packages/block-editor/src/components/resizable-box-popover/index.js +++ b/packages/block-editor/src/components/resizable-box-popover/index.js @@ -8,20 +8,20 @@ import { ResizableBox } from '@wordpress/components'; */ import BlockPopover from '../block-popover'; -export default function ResizableCoverPopover( { +export default function ResizableBoxPopover( { clientId, - __unstableRefreshSize, + resizableBoxProps, ...props } ) { return ( - + ); } diff --git a/packages/block-library/src/cover/edit/resizable-cover-popover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js index 3f5950912f5d7..57d846ed7007b 100644 --- a/packages/block-library/src/cover/edit/resizable-cover-popover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -36,33 +36,36 @@ export default function ResizableCoverPopover( { [ minHeight, height, width ] ); + const resizableBoxProps = { + className: classnames( className, { 'is-resizing': isResizing } ), + enable: RESIZABLE_BOX_ENABLE_OPTION, + onResizeStart: ( _event, _direction, elt ) => { + onResizeStart( elt.clientHeight ); + onResize( elt.clientHeight ); + }, + onResize: ( _event, _direction, elt ) => { + onResize( elt.clientHeight ); + if ( ! isResizing ) { + setIsResizing( true ); + } + }, + onResizeStop: ( _event, _direction, elt ) => { + onResizeStop( elt.clientHeight ); + setIsResizing( false ); + }, + __experimentalShowTooltip: true, + __experimentalTooltipProps: { + axis: 'y', + position: 'bottom', + isVisible: isResizing, + }, + }; + return ( { - onResizeStart( elt.clientHeight ); - onResize( elt.clientHeight ); - } } - onResize={ ( _event, _direction, elt ) => { - onResize( elt.clientHeight ); - if ( ! isResizing ) { - setIsResizing( true ); - } - } } - onResizeStop={ ( _event, _direction, elt ) => { - onResizeStop( elt.clientHeight ); - setIsResizing( false ); - } } - __experimentalShowTooltip - __experimentalTooltipProps={ { - axis: 'y', - position: 'bottom', - isVisible: isResizing, - } } + resizableBoxProps={ resizableBoxProps } { ...props } /> ); diff --git a/packages/block-library/src/cover/editor.scss b/packages/block-library/src/cover/editor.scss index bc0af932f1d1a..dad9ba9fd1e73 100644 --- a/packages/block-library/src/cover/editor.scss +++ b/packages/block-library/src/cover/editor.scss @@ -87,6 +87,19 @@ min-height: 50px; } +// Prevent resizable box popover form preventing inner block selection. +.components-popover.block-editor-block-popover.block-library-cover__resizable-box-popover { + // Additional specificity is required to overcome default block popover + // pointer events only for the intended wrappers. The default pointer events + // are still needed for the inner resize handles of the resizable box. + .components-popover__content > div, + .block-library-cover__resize-container { + // The inner drag handle will still have `pointer-events: all` allowing + // it to continue to be interacted with. + pointer-events: none; + } +} + // When uploading background images, show a transparent overlay. .wp-block-cover > .components-drop-zone .components-drop-zone__content { opacity: 0.8 !important; From e16a401fde23e96eab0fce09cdeb7c6c60a4ba48 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 10 Mar 2023 18:14:37 +1000 Subject: [PATCH 4/9] Fix overflow hidden style being omitted --- packages/block-library/src/cover/style.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/cover/style.scss b/packages/block-library/src/cover/style.scss index e562ee87ad782..a67e2712b8d1f 100644 --- a/packages/block-library/src/cover/style.scss +++ b/packages/block-library/src/cover/style.scss @@ -7,11 +7,11 @@ justify-content: center; align-items: center; padding: 1em; + overflow: hidden; // This block has customizable padding, border-box makes that more predictable. box-sizing: border-box; // Keep the flex layout direction to the physical direction (LTR) in RTL languages. /*rtl:raw: direction: ltr; */ - overflow: hidden; /** * Set a default background color for has-background-dim _unless_ it includes another From c0709c8417d37120056df79e64687fcf9b772133 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 10 Mar 2023 18:26:52 +1000 Subject: [PATCH 5/9] Use lock/unlock for experimental ResizableBoxPopover --- packages/block-editor/src/components/index.js | 1 - packages/block-editor/src/private-apis.js | 2 ++ .../src/cover/edit/resizable-cover-popover.js | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index bb3f36c948e45..f5b2c17e87740 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -77,7 +77,6 @@ export { default as MediaUpload } from './media-upload'; export { default as MediaUploadCheck } from './media-upload/check'; export { default as PanelColorSettings } from './panel-color-settings'; export { default as PlainText } from './plain-text'; -export { default as __experimentalResizableBoxPopover } from './resizable-box-popover'; export { default as __experimentalResponsiveBlockControl } from './responsive-block-control'; export { default as RichText, diff --git a/packages/block-editor/src/private-apis.js b/packages/block-editor/src/private-apis.js index 567849f66f019..ceb6bcd0e31c0 100644 --- a/packages/block-editor/src/private-apis.js +++ b/packages/block-editor/src/private-apis.js @@ -6,6 +6,7 @@ import { ExperimentalBlockEditorProvider } from './components/provider'; import { lock } from './lock-unlock'; import OffCanvasEditor from './components/off-canvas-editor'; import LeafMoreMenu from './components/off-canvas-editor/leaf-more-menu'; +import ResizableBoxPopover from './components/resizable-box-popover'; import { ComposedPrivateInserter as PrivateInserter } from './components/inserter'; import { PrivateListView } from './components/list-view'; @@ -20,4 +21,5 @@ lock( privateApis, { OffCanvasEditor, PrivateInserter, PrivateListView, + ResizableBoxPopover, } ); diff --git a/packages/block-library/src/cover/edit/resizable-cover-popover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js index 57d846ed7007b..3d42369db5a77 100644 --- a/packages/block-library/src/cover/edit/resizable-cover-popover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -7,7 +7,12 @@ import classnames from 'classnames'; * WordPress dependencies */ import { useMemo, useState } from '@wordpress/element'; -import { __experimentalResizableBoxPopover as ResizableBoxPopover } from '@wordpress/block-editor'; +import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { unlock } from '../../private-apis'; const RESIZABLE_BOX_ENABLE_OPTION = { top: false, @@ -30,6 +35,7 @@ export default function ResizableCoverPopover( { width, ...props } ) { + const { ResizableBoxPopover } = unlock( blockEditorPrivateApis ); const [ isResizing, setIsResizing ] = useState( false ); const dimensions = useMemo( () => ( { height, minHeight, width } ), From 306c7d48747644c8152cc99bbabe1bc59b644c27 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 16 Mar 2023 10:17:07 +1000 Subject: [PATCH 6/9] Pass showHandle to ResizableBox only --- .../block-library/src/cover/edit/resizable-cover-popover.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-library/src/cover/edit/resizable-cover-popover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js index 3d42369db5a77..50484a1138f39 100644 --- a/packages/block-library/src/cover/edit/resizable-cover-popover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -32,6 +32,7 @@ export default function ResizableCoverPopover( { onResize, onResizeStart, onResizeStop, + showHandle, width, ...props } ) { @@ -59,6 +60,7 @@ export default function ResizableCoverPopover( { onResizeStop( elt.clientHeight ); setIsResizing( false ); }, + showHandle, __experimentalShowTooltip: true, __experimentalTooltipProps: { axis: 'y', From ac5f302dbd69deb3efea136335365f044c6c1a52 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 16 Mar 2023 10:54:53 +1000 Subject: [PATCH 7/9] Ensure size gets passed through to ResizableBox --- .../block-library/src/cover/edit/resizable-cover-popover.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-library/src/cover/edit/resizable-cover-popover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js index 50484a1138f39..7567da82e8436 100644 --- a/packages/block-library/src/cover/edit/resizable-cover-popover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -33,6 +33,7 @@ export default function ResizableCoverPopover( { onResizeStart, onResizeStop, showHandle, + size, width, ...props } ) { @@ -61,6 +62,7 @@ export default function ResizableCoverPopover( { setIsResizing( false ); }, showHandle, + size, __experimentalShowTooltip: true, __experimentalTooltipProps: { axis: 'y', From f95be25872f0b0584e4ab1feb77459454b9215bb Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 16 Mar 2023 11:36:16 +1000 Subject: [PATCH 8/9] Improve non-pixel suppport --- packages/block-library/src/cover/edit/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/cover/edit/index.js b/packages/block-library/src/cover/edit/index.js index 930d0a71890c5..cbb8ac4e4891b 100644 --- a/packages/block-library/src/cover/edit/index.js +++ b/packages/block-library/src/cover/edit/index.js @@ -149,10 +149,10 @@ function CoverEdit( { const [ resizeListener, { height, width } ] = useResizeObserver(); const resizableBoxDimensions = useMemo( () => { return { - height: minHeight ? parseFloat( minHeight ) : 'auto', + height: minHeightUnit === 'px' ? minHeight : 'auto', width: 'auto', }; - }, [ minHeight ] ); + }, [ minHeight, minHeightUnit ] ); const minHeightWithUnit = minHeight && minHeightUnit @@ -258,7 +258,7 @@ function CoverEdit( { className: 'block-library-cover__resize-container', clientId, height, - minHeight: parseFloat( minHeight ), + minHeight: minHeightWithUnit, onResizeStart: () => { setAttributes( { minHeightUnit: 'px' } ); toggleSelection( false ); From 492e099d0be2030a9d24727b6882e2f516d23e2a Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 22 Mar 2023 13:33:23 +0800 Subject: [PATCH 9/9] Fix quick dragging when resizing the cover block --- packages/block-editor/src/components/iframe/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index b7c25fe7dc84b..c6639f9e057e1 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -71,7 +71,7 @@ function bubbleEvents( doc ) { } } - const eventTypes = [ 'dragover' ]; + const eventTypes = [ 'dragover', 'mousemove' ]; for ( const name of eventTypes ) { doc.addEventListener( name, bubbleEvent );