Skip to content

Commit 1533a42

Browse files
Mamadukaellatrix
andauthored
Format Library: Clean up 'Highlight' format components (#62965)
Co-authored-by: Mamaduka <[email protected]> Co-authored-by: ellatrix <[email protected]>
1 parent 91d7e41 commit 1533a42

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

packages/format-library/src/text-color/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* WordPress dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { useCallback, useMemo, useState } from '@wordpress/element';
5+
import { useMemo, useState } from '@wordpress/element';
66
import { RichTextToolbarButton, useSettings } from '@wordpress/block-editor';
77
import {
88
Icon,
@@ -66,21 +66,13 @@ function TextColorEdit( {
6666
'color.palette'
6767
);
6868
const [ isAddingColor, setIsAddingColor ] = useState( false );
69-
const enableIsAddingColor = useCallback(
70-
() => setIsAddingColor( true ),
71-
[ setIsAddingColor ]
72-
);
73-
const disableIsAddingColor = useCallback(
74-
() => setIsAddingColor( false ),
75-
[ setIsAddingColor ]
76-
);
7769
const colorIndicatorStyle = useMemo(
7870
() =>
7971
fillComputedColors(
8072
contentRef.current,
8173
getActiveColors( value, name, colors )
8274
),
83-
[ value, colors ]
75+
[ contentRef, value, colors ]
8476
);
8577

8678
const hasColorsToChoose = colors.length || ! allowCustomControl;
@@ -107,15 +99,15 @@ function TextColorEdit( {
10799
// If has no colors to choose but a color is active remove the color onClick.
108100
onClick={
109101
hasColorsToChoose
110-
? enableIsAddingColor
102+
? () => setIsAddingColor( true )
111103
: () => onChange( removeFormat( value, name ) )
112104
}
113105
role="menuitemcheckbox"
114106
/>
115107
{ isAddingColor && (
116108
<InlineColorUI
117109
name={ name }
118-
onClose={ disableIsAddingColor }
110+
onClose={ () => setIsAddingColor( false ) }
119111
activeAttributes={ activeAttributes }
120112
value={ value }
121113
onChange={ onChange }

packages/format-library/src/text-color/index.native.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { StyleSheet, View } from 'react-native';
77
* WordPress dependencies
88
*/
99
import { __ } from '@wordpress/i18n';
10-
import { useCallback, useMemo, useState } from '@wordpress/element';
10+
import { useMemo, useState } from '@wordpress/element';
1111
import {
1212
BlockControls,
1313
useSettings,
@@ -72,33 +72,17 @@ function TextColorEdit( {
7272
const [ allowCustomControl ] = useSettings( 'color.custom' );
7373
const colors = useMobileGlobalStylesColors();
7474
const [ isAddingColor, setIsAddingColor ] = useState( false );
75-
const enableIsAddingColor = useCallback(
76-
() => setIsAddingColor( true ),
77-
[ setIsAddingColor ]
78-
);
79-
const disableIsAddingColor = useCallback(
80-
() => setIsAddingColor( false ),
81-
[ setIsAddingColor ]
82-
);
8375
const colorIndicatorStyle = useMemo(
8476
() =>
8577
fillComputedColors(
8678
contentRef,
8779
getActiveColors( value, name, colors )
8880
),
89-
[ value, colors ]
81+
[ contentRef, value, colors ]
9082
);
9183

9284
const hasColorsToChoose = colors.length || ! allowCustomControl;
9385

94-
const onPressButton = useCallback( () => {
95-
if ( hasColorsToChoose ) {
96-
enableIsAddingColor();
97-
} else {
98-
onChange( removeFormat( value, name ) );
99-
}
100-
}, [ hasColorsToChoose, value ] );
101-
10286
const outlineStyle = [
10387
usePreferredColorSchemeStyle(
10488
styles[ 'components-inline-color__outline' ],
@@ -153,14 +137,18 @@ function TextColorEdit( {
153137
customContainerStyles,
154138
} }
155139
// If has no colors to choose but a color is active remove the color onClick
156-
onClick={ onPressButton }
140+
onClick={
141+
hasColorsToChoose
142+
? () => setIsAddingColor( true )
143+
: () => onChange( removeFormat( value, name ) )
144+
}
157145
/>
158146
</ToolbarGroup>
159147
</BlockControls>
160148
{ isAddingColor && (
161149
<InlineColorUI
162150
name={ name }
163-
onClose={ disableIsAddingColor }
151+
onClose={ () => setIsAddingColor( false ) }
164152
activeAttributes={ activeAttributes }
165153
value={ value }
166154
onChange={ onChange }

packages/format-library/src/text-color/inline.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { useCallback, useMemo } from '@wordpress/element';
4+
import { useMemo } from '@wordpress/element';
55
import { useSelect } from '@wordpress/data';
66
import {
77
applyFormat,
@@ -129,14 +129,6 @@ function ColorPicker( { name, property, value, onChange } ) {
129129
const { getSettings } = select( blockEditorStore );
130130
return getSettings().colors ?? [];
131131
}, [] );
132-
const onColorChange = useCallback(
133-
( color ) => {
134-
onChange(
135-
setColors( value, name, colors, { [ property ]: color } )
136-
);
137-
},
138-
[ colors, onChange, property ]
139-
);
140132
const activeColors = useMemo(
141133
() => getActiveColors( value, name, colors ),
142134
[ name, value, colors ]
@@ -145,7 +137,11 @@ function ColorPicker( { name, property, value, onChange } ) {
145137
return (
146138
<ColorPalette
147139
value={ activeColors[ property ] }
148-
onChange={ onColorChange }
140+
onChange={ ( color ) => {
141+
onChange(
142+
setColors( value, name, colors, { [ property ]: color } )
143+
);
144+
} }
149145
/>
150146
);
151147
}

0 commit comments

Comments
 (0)