Skip to content

Commit

Permalink
Mobile - ColorPalette - Add optional chaining for allColors and gradi…
Browse files Browse the repository at this point in the history
…ents, in some cases these values can be undefined, depending on the theme colors/configuration. It also adds a fallback name for colors that don't have a name value
  • Loading branch information
Gerardo committed Apr 20, 2023
1 parent 4a7a4c0 commit 3994388
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/components/src/color-palette/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useRef, useEffect } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';

Expand Down Expand Up @@ -176,14 +176,19 @@ function ColorPalette( {
}

function getColorGradientName( value ) {
const fallbackName = sprintf(
/* translators: %s: the hex color value */
__( 'Unlabeled color. %s' ),
value
);
const foundColorName = isGradientSegment
? defaultSettings.gradients.find(
? defaultSettings.gradients?.find(
( gradient ) => gradient.gradient === value
)
: defaultSettings.allColors.find(
: defaultSettings.allColors?.find(
( color ) => color.color === value
);
return foundColorName ? foundColorName?.name : '';
return foundColorName ? foundColorName?.name : fallbackName;
}

function onColorPress( color ) {
Expand Down

0 comments on commit 3994388

Please sign in to comment.