From d3fa9c233c7b75f9628bd8ef8aa5489922b50a4f Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:56:45 +1000 Subject: [PATCH 01/15] Use Dropdown for Block Styles control --- .../block-styles/block-styles-dropdown.js | 136 ++++++++++++++++++ .../src/components/block-styles/index.js | 52 ++----- .../src/components/block-styles/style.scss | 49 +------ .../block-styles/use-styles-for-block.js | 20 ++- 4 files changed, 168 insertions(+), 89 deletions(-) create mode 100644 packages/block-editor/src/components/block-styles/block-styles-dropdown.js diff --git a/packages/block-editor/src/components/block-styles/block-styles-dropdown.js b/packages/block-editor/src/components/block-styles/block-styles-dropdown.js new file mode 100644 index 00000000000000..90b4038ee8a7d2 --- /dev/null +++ b/packages/block-editor/src/components/block-styles/block-styles-dropdown.js @@ -0,0 +1,136 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + +/** + * WordPress dependencies + */ +import { + Button, + ColorIndicator, + Dropdown, + Flex, + FlexItem, + Icon, + MenuGroup, + MenuItem, + __experimentalHStack as HStack, + __experimentalZStack as ZStack, +} from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { check } from '@wordpress/icons'; + +const checkIcon = ; +const noop = () => undefined; + +function BlockStyleColorIndicator( { blockStyle } ) { + const { background, gradient, text } = blockStyle?.styles?.color || {}; + const label = blockStyle?.label || blockStyle?.name; + + return ( + + + + + + + + + + { label } + + ); +} + +function BlockStylesDropdownToggle( { onToggle, isOpen, blockStyle } ) { + const toggleProps = { + onClick: onToggle, + className: classnames( 'block-editor-block-styles__dropdown-toggle', { + 'is-open': isOpen, + } ), + 'aria-expanded': isOpen, + 'aria-label': __( 'Block style options' ), + }; + + return ( + + ); +} + +function BlockStylesDropdownContent( { + activeStyle, + handlePreview, + onSelect, + styles, +} ) { + return ( + + { styles.map( ( style ) => { + const isSelected = activeStyle?.name === style.name; + + return ( + handlePreview( null ) } + onClick={ () => onSelect( style ) } + onFocus={ () => handlePreview( style ) } + onMouseEnter={ () => handlePreview( style ) } + onMouseLeave={ () => handlePreview( null ) } + role="menuitemradio" + suffix={ isSelected ? checkIcon : undefined } + > + + + ); + } ) } + + ); +} + +export default function BlockStylesDropdown( { + className, + handlePreview = noop, + onSelect = noop, + styles, + value, + ...props +} ) { + if ( ! styles?.length ) { + return null; + } + + const classes = classnames( + className, + 'block-editor-block-styles__dropdown' + ); + + return ( + ( + + ) } + renderContent={ ( contentProps ) => ( + + ) } + /> + ); +} diff --git a/packages/block-editor/src/components/block-styles/index.js b/packages/block-editor/src/components/block-styles/index.js index a9fdbe94356ef6..0a986091e742a6 100644 --- a/packages/block-editor/src/components/block-styles/index.js +++ b/packages/block-editor/src/components/block-styles/index.js @@ -1,22 +1,14 @@ -/** - * External dependencies - */ -import classnames from 'classnames'; - /** * WordPress dependencies */ import { useState } from '@wordpress/element'; import { debounce, useViewportMatch } from '@wordpress/compose'; -import { - Button, - __experimentalTruncate as Truncate, - Popover, -} from '@wordpress/components'; +import { Popover } from '@wordpress/components'; /** * Internal dependencies */ +import BlockStylesDropdown from './block-styles-dropdown'; import BlockStylesPreviewPanel from './preview-panel'; import useStylesForBlocks from './use-styles-for-block'; @@ -61,40 +53,12 @@ function BlockStyles( { clientId, onSwitch = noop, onHoverClassName = noop } ) { return (
-
- { stylesToRender.map( ( style ) => { - const buttonText = style.label || style.name; - - return ( - - ); - } ) } -
+ { hoveredStyle && ! isMobileViewport && (