Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions packages/format-library/src/text-color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { useMemo, useState } from '@wordpress/element';
import { RichTextToolbarButton, useSettings } from '@wordpress/block-editor';
import {
Icon,
Expand Down Expand Up @@ -66,21 +66,13 @@ function TextColorEdit( {
'color.palette'
);
const [ isAddingColor, setIsAddingColor ] = useState( false );
const enableIsAddingColor = useCallback(
() => setIsAddingColor( true ),
[ setIsAddingColor ]
);
const disableIsAddingColor = useCallback(
() => setIsAddingColor( false ),
[ setIsAddingColor ]
);
const colorIndicatorStyle = useMemo(
() =>
fillComputedColors(
contentRef.current,
getActiveColors( value, name, colors )
),
[ value, colors ]
[ contentRef, value, colors ]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: The contentRef is a ref object, but the ESLint plugin can't know this because it's passed as props. tl;dr; It's a false positive that we must fix, but it won't regress memo behavior.

);

const hasColorsToChoose = colors.length || ! allowCustomControl;
Expand All @@ -107,15 +99,15 @@ function TextColorEdit( {
// If has no colors to choose but a color is active remove the color onClick.
onClick={
hasColorsToChoose
? enableIsAddingColor
? () => setIsAddingColor( true )
: () => onChange( removeFormat( value, name ) )
}
role="menuitemcheckbox"
/>
{ isAddingColor && (
<InlineColorUI
name={ name }
onClose={ disableIsAddingColor }
onClose={ () => setIsAddingColor( false ) }
activeAttributes={ activeAttributes }
value={ value }
onChange={ onChange }
Expand Down
28 changes: 8 additions & 20 deletions packages/format-library/src/text-color/index.native.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quickly tested the these changes on an iPhone simulator and Android emulator. I did not note any issues. Mentioning @geriux for awareness, just in case you have any concerns regarding these changes. 🙇🏻

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { StyleSheet, View } from 'react-native';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { useMemo, useState } from '@wordpress/element';
import {
BlockControls,
useSettings,
Expand Down Expand Up @@ -72,33 +72,17 @@ function TextColorEdit( {
const [ allowCustomControl ] = useSettings( 'color.custom' );
const colors = useMobileGlobalStylesColors();
const [ isAddingColor, setIsAddingColor ] = useState( false );
const enableIsAddingColor = useCallback(
() => setIsAddingColor( true ),
[ setIsAddingColor ]
);
const disableIsAddingColor = useCallback(
() => setIsAddingColor( false ),
[ setIsAddingColor ]
);
const colorIndicatorStyle = useMemo(
() =>
fillComputedColors(
contentRef,
getActiveColors( value, name, colors )
),
[ value, colors ]
[ contentRef, value, colors ]
);

const hasColorsToChoose = colors.length || ! allowCustomControl;

const onPressButton = useCallback( () => {
if ( hasColorsToChoose ) {
enableIsAddingColor();
} else {
onChange( removeFormat( value, name ) );
}
}, [ hasColorsToChoose, value ] );

const outlineStyle = [
usePreferredColorSchemeStyle(
styles[ 'components-inline-color__outline' ],
Expand Down Expand Up @@ -153,14 +137,18 @@ function TextColorEdit( {
customContainerStyles,
} }
// If has no colors to choose but a color is active remove the color onClick
onClick={ onPressButton }
onClick={
hasColorsToChoose
? () => setIsAddingColor( true )
: () => onChange( removeFormat( value, name ) )
}
/>
</ToolbarGroup>
</BlockControls>
{ isAddingColor && (
<InlineColorUI
name={ name }
onClose={ disableIsAddingColor }
onClose={ () => setIsAddingColor( false ) }
activeAttributes={ activeAttributes }
value={ value }
onChange={ onChange }
Expand Down
16 changes: 6 additions & 10 deletions packages/format-library/src/text-color/inline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useCallback, useMemo } from '@wordpress/element';
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
applyFormat,
Expand Down Expand Up @@ -129,14 +129,6 @@ function ColorPicker( { name, property, value, onChange } ) {
const { getSettings } = select( blockEditorStore );
return getSettings().colors ?? [];
}, [] );
const onColorChange = useCallback(
( color ) => {
onChange(
setColors( value, name, colors, { [ property ]: color } )
);
},
[ colors, onChange, property ]
);
const activeColors = useMemo(
() => getActiveColors( value, name, colors ),
[ name, value, colors ]
Expand All @@ -145,7 +137,11 @@ function ColorPicker( { name, property, value, onChange } ) {
return (
<ColorPalette
value={ activeColors[ property ] }
onChange={ onColorChange }
onChange={ ( color ) => {
onChange(
setColors( value, name, colors, { [ property ]: color } )
);
} }
Comment on lines +140 to +144
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked other usages of the ColorPalette component; memoization isn't required.

Copy link
Member

@dcalhoun dcalhoun Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity and my own edification, would you please elaborate on what you mean by "required?"

My interpretation is that you mean that memoizing this callback is unnecessary as ColorPalette itself it not memoized. Is that accurate?

Thanks! 🙇🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My interpretation is that you mean that memoizing this callback is unnecessary as ColorPalette itself it not memoized. Is that accurate?

Correct and the onChange prop isn't a dependency for any side-effect ( useEffect ).

/>
);
}
Expand Down