diff --git a/packages/block-editor/src/components/block-styles/index.native.js b/packages/block-editor/src/components/block-styles/index.native.js index 982274b6bd6212..858e8a2c7a5f9f 100644 --- a/packages/block-editor/src/components/block-styles/index.native.js +++ b/packages/block-editor/src/components/block-styles/index.native.js @@ -9,6 +9,7 @@ import { find } from 'lodash'; */ import { store as blocksStore } from '@wordpress/blocks'; import { useSelect, useDispatch } from '@wordpress/data'; +import { useMemo } from '@wordpress/element'; import { _x } from '@wordpress/i18n'; /** @@ -33,10 +34,6 @@ function BlockStyles( { clientId, url } ) { const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); - if ( ! styles || styles.length === 0 ) { - return null; - } - const renderedStyles = find( styles, 'isDefault' ) ? styles : [ @@ -48,37 +45,46 @@ function BlockStyles( { clientId, url } ) { ...styles, ]; - const activeStyle = getActiveStyle( renderedStyles, className ); + const mappedRenderedStyles = useMemo( () => { + const activeStyle = getActiveStyle( renderedStyles, className ); + + return renderedStyles.map( ( style ) => { + const styleClassName = replaceActiveStyle( + className, + activeStyle, + style + ); + const isActive = activeStyle === style; + + const onStylePress = () => { + updateBlockAttributes( clientId, { + className: styleClassName, + } ); + }; + + return ( + + ); + } ); + }, [ renderedStyles, className, clientId ] ); + + if ( ! styles || styles.length === 0 ) { + return null; + } + return ( - { renderedStyles.map( ( style ) => { - const styleClassName = replaceActiveStyle( - className, - activeStyle, - style - ); - const isActive = activeStyle === style; - - const onStylePress = () => { - updateBlockAttributes( clientId, { - className: styleClassName, - } ); - }; - - return ( - - ); - } ) } + { mappedRenderedStyles } ); } diff --git a/packages/block-editor/src/components/block-styles/preview.native.js b/packages/block-editor/src/components/block-styles/preview.native.js index c9f663dc7f7945..c5461bf330acb9 100644 --- a/packages/block-editor/src/components/block-styles/preview.native.js +++ b/packages/block-editor/src/components/block-styles/preview.native.js @@ -71,7 +71,7 @@ function StylePreview( { onPress, isActive, style, url } ) { return ( ); } ); diff --git a/packages/block-editor/src/components/colors-gradients/panel-color-gradient-settings.native.js b/packages/block-editor/src/components/colors-gradients/panel-color-gradient-settings.native.js index 126064cfd3865c..548137215fafc3 100644 --- a/packages/block-editor/src/components/colors-gradients/panel-color-gradient-settings.native.js +++ b/packages/block-editor/src/components/colors-gradients/panel-color-gradient-settings.native.js @@ -7,6 +7,7 @@ import { useNavigation } from '@react-navigation/native'; * WordPress dependencies */ import { ColorControl, PanelBody } from '@wordpress/components'; +import { useMemo } from '@wordpress/element'; /** * Internal dependencies @@ -16,32 +17,32 @@ import { blockSettingsScreens } from '../block-settings'; export default function PanelColorGradientSettings( { settings, title } ) { const navigation = useNavigation(); - return ( - - { settings.map( - ( { - onColorChange, - colorValue, - onGradientChange, - gradientValue, - label, - } ) => ( - { - navigation.navigate( blockSettingsScreens.color, { - onColorChange, - colorValue: gradientValue || colorValue, - gradientValue, - onGradientChange, - label, - } ); - } } - key={ `color-setting-${ label }` } - label={ label } - color={ gradientValue || colorValue } - /> - ) - ) } - - ); + const mappedSettings = useMemo( () => { + return settings.map( + ( { + onColorChange, + colorValue, + onGradientChange, + gradientValue, + label, + } ) => ( + { + navigation.navigate( blockSettingsScreens.color, { + onColorChange, + colorValue: gradientValue || colorValue, + gradientValue, + onGradientChange, + label, + } ); + } } + key={ `color-setting-${ label }` } + label={ label } + color={ gradientValue || colorValue } + /> + ) + ); + }, [ settings ] ); + + return { mappedSettings }; } diff --git a/packages/block-library/src/button/color-edit.js b/packages/block-library/src/button/color-edit.js index fbb1b2766ed396..e5611a97aee69d 100644 --- a/packages/block-library/src/button/color-edit.js +++ b/packages/block-library/src/button/color-edit.js @@ -7,7 +7,13 @@ import { pickBy, isEqual, isObject, identity, mapValues } from 'lodash'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useState, useEffect, useRef, Platform } from '@wordpress/element'; +import { + useState, + useEffect, + useRef, + useMemo, + Platform, +} from '@wordpress/element'; /** * Internal dependencies @@ -200,32 +206,43 @@ function ColorEdit( props ) { }; }; + const settings = useMemo( () => { + return [ + { + label: __( 'Text Color' ), + onColorChange: onChangeColor( 'text' ), + colorValue: getColorObjectByAttributeValues( + colors, + textColor, + style?.color?.text + ).color, + }, + { + label: __( 'Background Color' ), + onColorChange: onChangeColor( 'background' ), + colorValue: getColorObjectByAttributeValues( + colors, + backgroundColor, + style?.color?.background + ).color, + gradientValue, + onGradientChange: onChangeGradient, + }, + ]; + }, [ + colors, + textColor, + backgroundColor, + gradientValue, + style?.color?.text, + style?.color?.background, + ] ); + return ( ); } diff --git a/packages/block-library/src/button/edit.native.js b/packages/block-library/src/button/edit.native.js index 79e7f76363b409..b8a01e9d01174f 100644 --- a/packages/block-library/src/button/edit.native.js +++ b/packages/block-library/src/button/edit.native.js @@ -61,6 +61,35 @@ class ButtonEdit extends Component { isButtonFocused: true, placeholderTextWidth: 0, }; + + this.actions = [ + { + label: __( 'Remove link' ), + onPress: this.onClearSettings, + }, + ]; + + this.baseOptions = { + url: { + label: __( 'Button Link URL' ), + placeholder: __( 'Add URL' ), + autoFocus: true, + autoFill: true, + }, + openInNewTab: { + label: __( 'Open in new tab' ), + }, + linkRel: { + label: __( 'Link Rel' ), + placeholder: __( 'None' ), + }, + }; + + this.modOptions = { + ...this.baseOptions, + ...this.baseOptions.url, + autoFocus: false, + }; } componentDidMount() { @@ -218,39 +247,22 @@ class ButtonEdit extends Component { getLinkSettings( isCompatibleWithSettings ) { const { isLinkSheetVisible } = this.state; const { attributes, setAttributes } = this.props; - const actions = [ - { - label: __( 'Remove link' ), - onPress: this.onClearSettings, - }, - ]; - - const options = { - url: { - label: __( 'Button Link URL' ), - placeholder: __( 'Add URL' ), - autoFocus: ! isCompatibleWithSettings, - autoFill: true, - }, - openInNewTab: { - label: __( 'Open in new tab' ), - }, - linkRel: { - label: __( 'Link Rel' ), - placeholder: __( 'None' ), - }, - }; - return ( ); @@ -383,35 +395,35 @@ class ButtonEdit extends Component { { isSelected && ( - - - - - + <> + + + + + + { this.getLinkSettings( false ) } + + + + + + + { this.getLinkSettings( true ) } + + + ) } - - { this.getLinkSettings( false ) } - - - - - - - - { this.getLinkSettings( true ) } - - ); } diff --git a/packages/block-library/src/buttons/content-justification-dropdown.js b/packages/block-library/src/buttons/content-justification-dropdown.js index 7437a8670941ec..adce63ea234e0e 100644 --- a/packages/block-library/src/buttons/content-justification-dropdown.js +++ b/packages/block-library/src/buttons/content-justification-dropdown.js @@ -2,6 +2,7 @@ * WordPress dependencies */ import { DropdownMenu } from '@wordpress/components'; +import { useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; /** @@ -52,21 +53,25 @@ export default function ContentJustificationDropdown( { toggleProps, value, } ) { + const controls = useMemo( () => { + return allowedValues.map( ( allowedValue ) => { + return { + ...CONTROLS[ allowedValue ], + isActive: value === allowedValue, + role: 'menuitemradio', + onClick: () => + onChange( + value === allowedValue ? undefined : allowedValue + ), + }; + } ); + }, [ allowedValues, value ] ); + return ( { - return { - ...CONTROLS[ allowedValue ], - isActive: value === allowedValue, - role: 'menuitemradio', - onClick: () => - onChange( - value === allowedValue ? undefined : allowedValue - ), - }; - } ) } + controls={ controls } toggleProps={ toggleProps } /> ); diff --git a/packages/block-library/src/buttons/edit.native.js b/packages/block-library/src/buttons/edit.native.js index 1cbd6804df008c..459e9d05959d17 100644 --- a/packages/block-library/src/buttons/edit.native.js +++ b/packages/block-library/src/buttons/edit.native.js @@ -107,19 +107,21 @@ export default function ButtonsEdit( { return ( <> - - - - { ( toggleProps ) => ( - - ) } - - - + { isSelected && ( + + + + { ( toggleProps ) => ( + + ) } + + + + ) } { resizeObserver } - - - - - - + + + + + + + } + /> + + + - } - /> - - - - - + + + + ) } { + const getColumnsSliders = useMemo( () => { if ( ! editorSidebarOpened || ! isSelected ) { return null; } @@ -219,39 +219,48 @@ function ColumnsEditContainer( { /> ); } ); - }; + }, [ editorSidebarOpened, isSelected, innerColumns ] ); + + const onChangeColumnsNum = useCallback( + ( value ) => { + updateColumns( columnCount, value ); + }, + [ columnCount ] + ); return ( <> - - - - updateColumns( columnCount, value ) - } - min={ MIN_COLUMNS_NUM } - max={ columnCount + 1 } - type="stepper" - /> - { getColumnsSliders() } - - - - - - - - + { isSelected && ( + <> + + + + { getColumnsSliders } + + + + + + + + + + ) } { resizeListener } { width && ( diff --git a/packages/block-library/src/cover/edit.native.js b/packages/block-library/src/cover/edit.native.js index 79594d06c00bc1..bf58808ecee1da 100644 --- a/packages/block-library/src/cover/edit.native.js +++ b/packages/block-library/src/cover/edit.native.js @@ -22,7 +22,7 @@ import { PanelBody, RangeControl, UnitControl, - BottomSheet, + TextControl, ToolbarButton, ToolbarGroup, Gradient, @@ -46,7 +46,7 @@ import { } from '@wordpress/block-editor'; import { compose, withPreferredColorScheme } from '@wordpress/compose'; import { withSelect, withDispatch } from '@wordpress/data'; -import { useEffect, useState, useRef } from '@wordpress/element'; +import { useEffect, useState, useRef, useCallback } from '@wordpress/element'; import { cover as icon, replace, image, warning } from '@wordpress/icons'; import { getProtocol } from '@wordpress/url'; @@ -88,6 +88,7 @@ const Cover = ( { setAttributes, openGeneralSidebar, closeSettingsBottomSheet, + isSelected, } ) => { const { backgroundType, @@ -169,15 +170,15 @@ const Cover = ( { onSelect( media ); }; - const onHeightChange = ( value ) => { + const onHeightChange = useCallback( ( value ) => { if ( minHeight || value !== COVER_DEFAULT_HEIGHT ) { setAttributes( { minHeight: value } ); } - }; + }, [] ); - const onOpacityChange = ( value ) => { + const onOpacityChange = useCallback( ( value ) => { setAttributes( { dimRatio: value } ); - }; + }, [] ); const onMediaPressed = () => { if ( isUploadInProgress ) { @@ -199,10 +200,10 @@ const Cover = ( { setIsVideoLoading( false ); }; - const onClearMedia = () => { + const onClearMedia = useCallback( () => { setAttributes( { id: undefined, url: undefined } ); closeSettingsBottomSheet(); - }; + }, [] ); function setColor( color ) { setAttributes( { @@ -278,7 +279,7 @@ const Cover = ( { ); - const onChangeUnit = ( nextUnit ) => { + const onChangeUnit = useCallback( ( nextUnit ) => { setAttributes( { minHeightUnit: nextUnit, minHeight: @@ -286,12 +287,15 @@ const Cover = ( { ? Math.max( CONTAINER_HEIGHT, COVER_MIN_HEIGHT ) : CONTAINER_HEIGHT, } ); - }; + }, [] ); const controls = ( { url ? ( @@ -321,10 +325,9 @@ const Cover = ( { key={ minHeightUnit } /> - { url ? ( - - { controls } + { isSelected && controls } { isImage && url && diff --git a/packages/block-library/src/cover/overlay-color-settings.native.js b/packages/block-library/src/cover/overlay-color-settings.native.js index 7ceffd1fafd636..fec53f8add57f8 100644 --- a/packages/block-library/src/cover/overlay-color-settings.native.js +++ b/packages/block-library/src/cover/overlay-color-settings.native.js @@ -14,19 +14,19 @@ import { __experimentalPanelColorGradientSettings as PanelColorGradientSettings, __experimentalUseEditorFeature as useEditorFeature, } from '@wordpress/block-editor'; +import { useMemo } from '@wordpress/element'; -function OverlayColorSettings( { attributes, setAttributes } ) { +function OverlayColorSettings( { + overlayColor, + customOverlayColor, + gradient, + customGradient, + setAttributes, +} ) { const EMPTY_ARRAY = []; const colors = useEditorFeature( 'color.palette' ) || EMPTY_ARRAY; const gradients = useEditorFeature( 'color.gradients' ) || EMPTY_ARRAY; - const { - overlayColor, - customOverlayColor, - gradient, - customGradient, - } = attributes; - const gradientValue = customGradient || getGradientValueBySlug( gradients, gradient ); @@ -36,56 +36,60 @@ function OverlayColorSettings( { attributes, setAttributes } ) { customOverlayColor ).color; - const setOverlayAttribute = ( attributeName, value ) => { - setAttributes( { - // clear all related attributes (only one should be set) - overlayColor: undefined, - customOverlayColor: undefined, - gradient: undefined, - customGradient: undefined, - [ attributeName ]: value, - } ); - }; + const settings = useMemo( () => { + const setOverlayAttribute = ( attributeName, value ) => { + setAttributes( { + // clear all related attributes (only one should be set) + overlayColor: undefined, + customOverlayColor: undefined, + gradient: undefined, + customGradient: undefined, + [ attributeName ]: value, + } ); + }; + + const onColorChange = ( value ) => { + // do nothing for falsy values + if ( ! value ) { + return; + } + const colorObject = getColorObjectByColorValue( colors, value ); + if ( colorObject?.slug ) { + setOverlayAttribute( 'overlayColor', colorObject.slug ); + } else { + setOverlayAttribute( 'customOverlayColor', value ); + } + }; - const onColorChange = ( value ) => { - // do nothing for falsy values - if ( ! value ) { - return; - } - const colorObject = getColorObjectByColorValue( colors, value ); - if ( colorObject?.slug ) { - setOverlayAttribute( 'overlayColor', colorObject.slug ); - } else { - setOverlayAttribute( 'customOverlayColor', value ); - } - }; + const onGradientChange = ( value ) => { + // do nothing for falsy values + if ( ! value ) { + return; + } + const slug = getGradientSlugByValue( gradients, value ); + if ( slug ) { + setOverlayAttribute( 'gradient', slug ); + } else { + setOverlayAttribute( 'customGradient', value ); + } + }; - const onGradientChange = ( value ) => { - // do nothing for falsy values - if ( ! value ) { - return; - } - const slug = getGradientSlugByValue( gradients, value ); - if ( slug ) { - setOverlayAttribute( 'gradient', slug ); - } else { - setOverlayAttribute( 'customGradient', value ); - } - }; + return [ + { + label: __( 'Color' ), + onColorChange, + colorValue, + gradientValue, + onGradientChange, + }, + ]; + }, [ colorValue, gradientValue, colors, gradients ] ); return ( ); } diff --git a/packages/block-library/src/file/edit.native.js b/packages/block-library/src/file/edit.native.js index 062270b322a58c..0a88b9c362fc23 100644 --- a/packages/block-library/src/file/edit.native.js +++ b/packages/block-library/src/file/edit.native.js @@ -28,7 +28,7 @@ import { ToolbarGroup, PanelBody, ToggleControl, - BottomSheet, + TextControl, SelectControl, Icon, } from '@wordpress/components'; @@ -264,13 +264,16 @@ export class FileEdit extends Component { ); const isCopyUrlDisabled = isUploadFailed || isUploadInProgress; - const dimmedStyle = isCopyUrlDisabled && styles.disabledButton; - const finalButtonStyle = Object.assign( - {}, - actionButtonStyle, - dimmedStyle + + const dimmedActionButtonStyle = this.props.getStylesFromColorScheme( + styles.dimmedActionButton, + styles.dimmedActionButtonDark ); + const finalButtonStyle = isCopyUrlDisabled + ? dimmedActionButtonStyle + : actionButtonStyle; + return ( { isSidebarLinkSettings || ( @@ -298,7 +301,7 @@ export class FileEdit extends Component { onChange={ this.onChangeDownloadButtonVisibility } /> ) } - - - - { images.length > 1 && ( - + + { images.length > 1 && ( + + ) } + - ) } - - - { shouldShowSizeOptions && ( - ) } - - + { shouldShowSizeOptions && ( + + ) } + + + ) } { noticeUI } ( { + value: slug, + name, + } ) ); } componentDidMount() { @@ -336,65 +359,48 @@ export class ImageEdit extends React.Component { : width; } + setMappedAttributes = ( { url: href, ...restAttributes } ) => { + const { setAttributes } = this.props; + return href === undefined + ? setAttributes( restAttributes ) + : setAttributes( { ...restAttributes, href } ); + }; + getLinkSettings() { const { isLinkSheetVisible } = this.state; const { attributes: { href: url, ...unMappedAttributes }, - setAttributes, } = this.props; const mappedAttributes = { ...unMappedAttributes, url }; - const setMappedAttributes = ( { url: href, ...restAttributes } ) => - href === undefined - ? setAttributes( restAttributes ) - : setAttributes( { ...restAttributes, href } ); - - const options = { - url: { - label: __( 'Image Link URL' ), - placeholder: __( 'Add URL' ), - autoFocus: false, - autoFill: true, - }, - openInNewTab: { - label: __( 'Open in new tab' ), - }, - linkRel: { - label: __( 'Link Rel' ), - placeholder: __( 'None' ), - }, - }; return ( ); } + onSizeChangeValue( newValue ) { + this.onSetSizeSlug( newValue ); + } + render() { const { isCaptionSelected } = this.state; - const { - attributes, - isSelected, - image, - imageSizes, - clientId, - } = this.props; + const { attributes, isSelected, image, clientId } = this.props; const { align, url, alt, id, sizeSlug, className } = attributes; - const sizeOptions = map( imageSizes, ( { name, slug } ) => ( { - value: slug, - name, - } ) ); - const sizeOptionsValid = find( sizeOptions, [ + const sizeOptionsValid = find( this.sizeOptions, [ 'value', DEFAULT_SIZE_SLUG, ] ); @@ -427,10 +433,8 @@ export class ImageEdit extends React.Component { icon={ expand } label={ __( 'Size' ) } value={ sizeSlug || DEFAULT_SIZE_SLUG } - onChangeValue={ ( newValue ) => - this.onSetSizeSlug( newValue ) - } - options={ sizeOptions } + onChangeValue={ this.onSizeChangeValue } + options={ this.sizeOptions } /> ) } - { getInspectorControls() } - { getMediaOptions() } + { isSelected && getInspectorControls() } + { isSelected && getMediaOptions() } { ! this.state.isCaptionSelected && getToolbarEditButton( openMediaOptions ) } - { this.getInspectorControls() } + { isSelected && this.getInspectorControls() } { blockType.settings.title } diff --git a/packages/block-library/src/media-text/edit.native.js b/packages/block-library/src/media-text/edit.native.js index f6ad9cf9fdbc43..5ff58b8696be02 100644 --- a/packages/block-library/src/media-text/edit.native.js +++ b/packages/block-library/src/media-text/edit.native.js @@ -337,28 +337,33 @@ class MediaTextEdit extends Component { return ( <> - { mediaType === MEDIA_TYPE_IMAGE && this.getControls() } - - { ( isMediaSelected || mediaType === MEDIA_TYPE_VIDEO ) && ( - -