Skip to content

Commit

Permalink
Fix: Color editor discards colors with default name. (#37496)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored and tellthemachines committed Dec 21, 2021
1 parent 4cb317d commit 8bcedba
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions packages/components/src/palette-edit/index.js
Expand Up @@ -40,6 +40,8 @@ import { NavigableMenu } from '../navigable-container';
import { DEFAULT_GRADIENT } from '../custom-gradient-picker/constants';
import CustomGradientPicker from '../custom-gradient-picker';

const DEFAULT_COLOR = '#000';

function NameInput( { value, onChange, label } ) {
return (
<NameInputControl
Expand All @@ -51,6 +53,14 @@ function NameInput( { value, onChange, label } ) {
);
}

function getNameForPosition( position ) {
return sprintf(
/* translators: %s: is a temporary id for a custom color */
__( 'Color %s ' ),
position + 1
);
}

function Option( {
canOnlyChangeValues,
element,
Expand Down Expand Up @@ -143,6 +153,14 @@ function Option( {
);
}

function isTemporaryElement( slugPrefix, { slug, color, gradient }, index ) {
return (
slug === slugPrefix + kebabCase( getNameForPosition( index ) ) &&
( ( !! color && color === DEFAULT_COLOR ) ||
( !! gradient && gradient === DEFAULT_GRADIENT ) )
);
}

function PaletteEditListView( {
elements,
onChange,
Expand All @@ -159,9 +177,14 @@ function PaletteEditListView( {
}, [ elements ] );
useEffect( () => {
return () => {
if ( elementsReference.current.some( ( { slug } ) => ! slug ) ) {
if (
elementsReference.current.some( ( element, index ) =>
isTemporaryElement( slugPrefix, element, index )
)
) {
const newElements = elementsReference.current.filter(
( { slug } ) => slug
( element, index ) =>
! isTemporaryElement( slugPrefix, element, index )
);
onChange( newElements.length ? newElements : undefined );
}
Expand Down Expand Up @@ -272,19 +295,19 @@ export default function PaletteEdit( {
: __( 'Add color' )
}
onClick={ () => {
const tempOptionName = sprintf(
/* translators: %s: is a temporary id for a custom color */
__( 'Color %s ' ),
elementsLength + 1
const tempOptionName = getNameForPosition(
elementsLength
);
onChange( [
...elements,
{
...( isGradient
? { gradient: DEFAULT_GRADIENT }
: { color: '#000' } ),
: { color: DEFAULT_COLOR } ),
name: tempOptionName,
slug: '',
slug:
slugPrefix +
kebabCase( tempOptionName ),
},
] );
setIsEditing( true );
Expand Down

0 comments on commit 8bcedba

Please sign in to comment.