Skip to content

Commit

Permalink
Refactor to minimize changes required
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 8, 2021
1 parent 3f0fe72 commit f9c2f67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
10 changes: 6 additions & 4 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
useSetting,
} from '../components';

const EMPTY_ARRAY = [];

/**
* Convert a list of colors to an object of R, G, and B values.
*
Expand Down Expand Up @@ -124,14 +126,14 @@ function DuotonePanel( { name, attributes, setAttributes } ) {
const style = attributes?.style;
const duotone = style?.color?.duotone;

const duotonePalette = useSetting( 'color.duotone' );
const colorPalette = useSetting( 'color.palette' );
const duotonePalette = useSetting( 'color.duotone' ) || EMPTY_ARRAY;
const colorPalette = useSetting( 'color.palette' ) || EMPTY_ARRAY;
const disableCustomColors = ! useSetting( 'color.custom' );

if (
! hasBlockSupport( name, 'color.__experimentalDuotone' ) ||
( ( ! duotonePalette || duotonePalette?.length === 0 ) &&
( ! colorPalette || colorPalette?.length === 0 ) &&
( duotonePalette?.length === 0 &&
colorPalette?.length === 0 &&
disableCustomColors )
) {
return null;
Expand Down
74 changes: 35 additions & 39 deletions packages/components/src/duotone-picker/duotone-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,43 @@ function DuotonePicker( {
[ colorPalette ]
);

let duotonePresets = null;
if ( !! duotonePalette ) {
duotonePresets = duotonePalette.map( ( { colors, slug, name } ) => {
const style = {
background: getGradientFromCSSColors( colors, '135deg' ),
color: 'transparent',
};
const tooltipText =
name ??
sprintf(
// translators: %s: duotone code e.g: "dark-grayscale" or "7f7f7f-ffffff".
__( 'Duotone code: %s' ),
slug
);
const label = name
? sprintf(
// translators: %s: The name of the option e.g: "Dark grayscale".
__( 'Duotone: %s' ),
name
)
: tooltipText;
const isSelected = isEqual( colors, value );

return (
<CircularOptionPicker.Option
key={ slug }
value={ colors }
isSelected={ isSelected }
aria-label={ label }
tooltipText={ tooltipText }
style={ style }
onClick={ () => {
onChange( isSelected ? undefined : colors );
} }
/>
);
} );
}
return (
<CircularOptionPicker
options={ duotonePresets }
options={ duotonePalette.map( ( { colors, slug, name } ) => {
const style = {
background: getGradientFromCSSColors( colors, '135deg' ),
color: 'transparent',
};
const tooltipText =
name ??
sprintf(
// translators: %s: duotone code e.g: "dark-grayscale" or "7f7f7f-ffffff".
__( 'Duotone code: %s' ),
slug
);
const label = name
? sprintf(
// translators: %s: The name of the option e.g: "Dark grayscale".
__( 'Duotone: %s' ),
name
)
: tooltipText;
const isSelected = isEqual( colors, value );

return (
<CircularOptionPicker.Option
key={ slug }
value={ colors }
isSelected={ isSelected }
aria-label={ label }
tooltipText={ tooltipText }
style={ style }
onClick={ () => {
onChange( isSelected ? undefined : colors );
} }
/>
);
} ) }
actions={
<CircularOptionPicker.ButtonAction
onClick={ () => onChange( undefined ) }
Expand Down

0 comments on commit f9c2f67

Please sign in to comment.