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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `InputControl`: Implement wrapper subcomponents for adding responsive padding to `prefix`/`suffix` ([#42378](https://github.com/WordPress/gutenberg/pull/42378)).
- `SelectControl`: Add flag for larger default size ([#42456](https://github.com/WordPress/gutenberg/pull/42456/)).
- `UnitControl`: Update unit select's focus styles to match input's ([#42383](https://github.com/WordPress/gutenberg/pull/42383)).
- `ColorPalette`: Display checkered preview background when `value` is transparent ([#42232](https://github.com/WordPress/gutenberg/pull/42232)).
- `CustomSelectControl`: Add size variants ([#42460](https://github.com/WordPress/gutenberg/pull/42460/)).
- `CustomSelectControl`: Add flag to opt in to unconstrained width ([#42460](https://github.com/WordPress/gutenberg/pull/42460/)).
- `Dropdown`: Implement wrapper subcomponent for adding different padding to the dropdown content ([#42595](https://github.com/WordPress/gutenberg/pull/42595/)).
Expand Down
27 changes: 19 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,13 @@ export const extractColorNameFromCurrentValue = (
return __( 'Custom' );
};

export const showTransparentBackground = ( currentValue ) => {
if ( typeof currentValue === 'undefined' ) {
return true;
}
return colord( currentValue ).alpha() === 0;
};

export default function ColorPalette( {
clearable = true,
className,
Expand Down Expand Up @@ -228,14 +235,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
20 changes: 19 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,19 @@ describe( 'ColorPalette: Utils', () => {
expect( result ).toBe( 'Blue' );
} );
} );
describe( 'showTransparentBackground', () => {
test( 'should return true for undefined color values', () => {
expect( showTransparentBackground( undefined ) ).toBe( true );
} );
test( 'should return true for transparent colors', () => {
expect( showTransparentBackground( 'transparent' ) ).toBe( true );
expect( showTransparentBackground( '#75757500' ) ).toBe( true );
} );

test( 'should return false for non-transparent colors', () => {
expect( showTransparentBackground( '#FFF' ) ).toBe( false );
expect( showTransparentBackground( '#757575' ) ).toBe( false );
expect( showTransparentBackground( '#f5f5f524' ) ).toBe( false ); // 0.14 alpha.
} );
} );
} );