Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColorPalette: Display checkered preview background when value is transparent #42232

Merged
24 changes: 16 additions & 8 deletions packages/components/src/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export const extractColorNameFromCurrentValue = (
return __( 'Custom' );
};

export const showTransparentBackground = ( currentValue ) => {
return colord( currentValue ).alpha() === 0;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add handling (and a test case) for an undefined color value? It will be undefined when the "Clear" button is clicked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add handling (and a test case) for an undefined color value? It will be undefined when the "Clear" button is clicked.

@mirka Yep, did with d482da9


export default function ColorPalette( {
clearable = true,
className,
Expand Down Expand Up @@ -228,14 +232,18 @@ export default function ColorPalette( {
aria-haspopup="true"
onClick={ onToggle }
aria-label={ customColorAccessibleLabel }
style={ {
background: value,
color:
colordColor.contrast() >
colordColor.contrast( '#000' )
? '#fff'
: '#000',
} }
style={
showTransparentBackground( value )
? { color: '#000' }
: {
background: value,
color:
colordColor.contrast() >
colordColor.contrast( '#000' )
? '#fff'
: '#000',
}
}
>
<FlexItem
isBlock
Expand Down
17 changes: 16 additions & 1 deletion packages/components/src/color-palette/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* Internal dependencies
*/
import { extractColorNameFromCurrentValue } from '../';
import {
extractColorNameFromCurrentValue,
showTransparentBackground,
} from '../';

describe( 'ColorPalette: Utils', () => {
describe( 'extractColorNameFromCurrentValue', () => {
Expand All @@ -21,4 +24,16 @@ describe( 'ColorPalette: Utils', () => {
expect( result ).toBe( 'Blue' );
} );
} );
describe( 'showTransparentBackground', () => {
test( 'should return true for transparent colors', () => {
expect( showTransparentBackground( 'transparent' ) ).toBe( true );
expect( showTransparentBackground( '#75757500' ) ).toBe( true );
} );

test( 'should return true for transparent colors', () => {
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
expect( showTransparentBackground( '#FFF' ) ).toBe( false );
expect( showTransparentBackground( '#757575' ) ).toBe( false );
expect( showTransparentBackground( '#f5f5f524' ) ).toBe( false ); // 0.14 alpha.
} );
} );
} );