Skip to content

Commit

Permalink
Mobile - Fix missing Custom indicator for custom gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo committed Jan 5, 2024
1 parent 49bd0fa commit 7da50c5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ exports[`Buttons block color customization sets a background color 1`] = `
<!-- /wp:buttons -->"
`;

exports[`Buttons block color customization sets a custom gradient background color 1`] = `
"<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"style":{"color":{"gradient":"linear-gradient(200deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)"}}} /--></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons block color customization sets a gradient background color 1`] = `
"<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"gradient":"light-green-cyan-to-vivid-green-cyan"} /--></div>
Expand Down
54 changes: 54 additions & 0 deletions packages/block-library/src/buttons/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,59 @@ describe( 'Buttons block', () => {
// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'sets a custom gradient background color', async () => {
// Arrange
const screen = await initializeEditor();
await addBlock( screen, 'Buttons' );

// Act
const buttonsBlock = getBlock( screen, 'Buttons' );
fireEvent.press( buttonsBlock );

// Trigger onLayout for the list
await triggerBlockListLayout( buttonsBlock );

const buttonBlock = await getBlock( screen, 'Button' );
fireEvent.press( buttonBlock );

// Open Block Settings.
fireEvent.press( screen.getByLabelText( 'Open Settings' ) );

// Wait for Block Settings to be visible.
const blockSettingsModal = screen.getByTestId(
'block-settings-modal'
);
await waitFor( () => blockSettingsModal.props.isVisible );

// Open Text color settings
fireEvent.press( screen.getByLabelText( 'Background, Default' ) );

// Tap on the gradient segment
fireEvent.press( screen.getByLabelText( 'Gradient' ) );

// Tap one gradient color
fireEvent.press(
screen.getByLabelText( 'Light green cyan to vivid green cyan' )
);

// Tap on Customize Gradient
fireEvent.press( screen.getByLabelText( /Customize Gradient/ ) );

// Change the current angle
fireEvent.press( screen.getByText( '135', { hidden: true } ) );
const angleTextInput = screen.getByDisplayValue( '135', {
hidden: true,
} );
fireEvent.changeText( angleTextInput, '200' );

// Go back to the settings list.
fireEvent.press( await screen.findByLabelText( 'Go back' ) );

// Assert
const customButton = await screen.findByText( 'CUSTOM' );
expect( customButton ).toBeVisible();
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );
} );
27 changes: 19 additions & 8 deletions packages/components/src/color-palette/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let scrollPosition = 0;
let customIndicatorWidth = 0;

function ColorPalette( {
enableCustomColor = true,
enableCustomColor = false,
setColor,
activeColor,
isGradientColor,
Expand Down Expand Up @@ -62,24 +62,35 @@ function ColorPalette( {
const scale = useRef( new Animated.Value( 1 ) ).current;
const opacity = useRef( new Animated.Value( 1 ) ).current;

const defaultColors = [
const mergedColors = [
...new Set(
( defaultSettings.colors ?? [] ).map( ( { color } ) => color )
),
];
const mergedColors = [
const mergedGradients = [
...new Set(
( defaultSettings.gradients ?? [] ).map(
( { gradient } ) => gradient
)
),
];
const allAvailableColors = [
...new Set(
( defaultSettings.allColors ?? [] ).map( ( { color } ) => color )
),
];
const defaultGradientColors = [
const allAvailableGradiens = [
...new Set(
( defaultSettings.gradients ?? [] ).map(
( defaultSettings.allGradients ?? [] ).map(
( { gradient } ) => gradient
)
),
];
const colors = isGradientSegment ? defaultGradientColors : defaultColors;

const colors = isGradientSegment ? mergedGradients : mergedColors;
const allColors = isGradientSegment
? allAvailableGradiens
: allAvailableColors;

const customIndicatorColor = isGradientSegment
? activeColor
Expand Down Expand Up @@ -110,8 +121,8 @@ function ColorPalette( {

function isSelectedCustom() {
const isWithinColors =
activeColor && mergedColors && mergedColors.includes( activeColor );
if ( enableCustomColor && activeColor ) {
activeColor && allColors && allColors.includes( activeColor );
if ( activeColor ) {
if ( isGradientSegment ) {
return isGradientColor && ! isWithinColors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { colorsUtils } from './utils';
import styles from './style.scss';

const HIT_SLOP = { top: 8, bottom: 8, left: 8, right: 8 };
const THEME_PALETTE_NAME = 'Theme';

const PaletteScreen = () => {
const route = useRoute();
Expand All @@ -48,7 +47,6 @@ const PaletteScreen = () => {
const [ currentValue, setCurrentValue ] = useState( colorValue );
const isGradientColor = isGradient( currentValue );
const selectedSegmentIndex = isGradientColor ? 1 : 0;
const allAvailableColors = useMobileGlobalStylesColors();

const [ currentSegment, setCurrentSegment ] = useState(
segments[ selectedSegmentIndex ]
Expand All @@ -57,6 +55,10 @@ const PaletteScreen = () => {
const currentSegmentColors = ! isGradientSegment
? defaultSettings.colors
: defaultSettings.gradients;
const allAvailableColors = useMobileGlobalStylesColors();
const allAvailableGradients = currentSegmentColors
.map( ( item ) => item.gradients || [] )
.reduce( ( acc, val ) => acc.concat( val ), [] );

const horizontalSeparatorStyle = usePreferredColorSchemeStyle(
styles.horizontalSeparator,
Expand Down Expand Up @@ -184,10 +186,10 @@ const PaletteScreen = () => {
colors: palette.colors,
gradients: palette.gradients,
allColors: allAvailableColors,
allGradients: allAvailableGradients,
};
const enableCustomColor =
! isGradientSegment &&
palette.name === THEME_PALETTE_NAME;
// Limit to show the custom indicator to the first available palette
const enableCustomColor = paletteKey === 0;

return (
<ColorPalette
Expand Down

0 comments on commit 7da50c5

Please sign in to comment.