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

Allow "disableAlpha" for color Picker component to be optional (replaces #5835) #7151

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions components/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { __, sprintf } from '@wordpress/i18n';
import './style.scss';
import Button from '../button';

export default function ColorPalette( { colors, disableCustomColors = false, value, onChange } ) {
export default function ColorPalette( { colors, disableCustomColors = false, value, onChange, disableAlpha = true } ) {
Copy link
Member

Choose a reason for hiding this comment

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

A nitpick but it might be nice to sort these props alphabetically. The current ordering makes it harder to sort through arguments and makes this last one feel especially tacked-on.

Copy link
Author

Choose a reason for hiding this comment

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

@tofumatt done!

function applyOrUnset( color ) {
return () => onChange( value === color ? undefined : color );
}
Expand Down Expand Up @@ -65,8 +65,17 @@ export default function ColorPalette( { colors, disableCustomColors = false, val
renderContent={ () => (
<ChromePicker
color={ value }
onChangeComplete={ ( color ) => onChange( color.hex ) }
disableAlpha
onChangeComplete={ ( color ) => {
let colorString;
if ( typeof color.rgb === 'undefined' || color.rgb.a === 1 ) {
colorString = color.hex;
} else {
const { r, g, b, a } = color.rgb;
colorString = 'rgba(' + [ r, g, b, a ].join( ',' ) + ')';
Copy link
Member

Choose a reason for hiding this comment

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

You could use template strings here and omit the join; it'd probably be a bit more readable:

colorString = `rgba(${ r }, ${ g }, ${ b }, ${ a })`;

I'm not sure why the Array.join is being used at all here, it just seems to make it more complex.

Copy link
Author

Choose a reason for hiding this comment

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

@tofumatt yeah, I guess this is me still not 100% used to use ES6 features. But I totally agree with you. Just switched the variable to template strings.

}
onChange( colorString );
Copy link
Member

Choose a reason for hiding this comment

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

Seems there's a code path here where colorString is undefined, should we test for its truthiness before running onChange?

Copy link
Member

Choose a reason for hiding this comment

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

Seems there's a code path here where colorString is undefined

What's the code path?

Copy link
Member

Choose a reason for hiding this comment

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

Oh wait, there isn't, but because it's defined as undefined above it sort of read like maybe there could be.

I actually see why it's this way, never mind me, all good!

} }
disableAlpha={ disableAlpha }
/>
) }
/>
Expand Down
35 changes: 35 additions & 0 deletions components/color-palette/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ColorPalette Dropdown .renderContent should enable alpha channel inside color picker 1`] = `
<Chrome
color="#f00"
disableAlpha={false}
hex="#ff0000"
hsl={
Object {
"a": 1,
"h": 0,
"l": 0.5,
"s": 1,
}
}
hsv={
Object {
"a": 1,
"h": 0,
"s": 1,
"v": 1,
}
}
oldHue={0}
onChange={[Function]}
onChangeComplete={[Function]}
rgb={
Object {
"a": 1,
"b": 0,
"g": 0,
"r": 255,
}
}
/>
`;

exports[`ColorPalette Dropdown .renderContent should render dropdown content 1`] = `
<Chrome
color="#f00"
Expand Down
16 changes: 16 additions & 0 deletions components/color-palette/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import ColorPalette from '../';
describe( 'ColorPalette', () => {
const colors = [ { name: 'red', color: '#f00' }, { name: 'white', color: '#fff' }, { name: 'blue', color: '#00f' } ];
const currentColor = '#f00';
const currentColorWithAlpha = { r: 255, g: 0, b: 0, a: 0.5 };
const currentColorWithAlphaParsed = 'rgba(255,0,0,0.5)';
const onChange = jest.fn();

const wrapper = shallow( <ColorPalette colors={ colors } value={ currentColor } onChange={ onChange } /> );
const wrapperWithAlpha = shallow( <ColorPalette colors={ colors } value={ currentColor } onChange={ onChange } disableAlpha={ false } /> );
const buttons = wrapper.find( '.components-color-palette__item-wrapper button' );

beforeEach( () => {
Expand Down Expand Up @@ -84,6 +87,8 @@ describe( 'ColorPalette', () => {

describe( '.renderContent', () => {
const renderedContent = shallow( dropdown.props().renderContent() );
const dropdownWithAlpha = wrapperWithAlpha.find( 'Dropdown' );
const renderedContentWithAlpha = shallow( dropdownWithAlpha.props().renderContent() );

test( 'should render dropdown content', () => {
expect( renderedContent ).toMatchSnapshot();
Expand All @@ -95,6 +100,17 @@ describe( 'ColorPalette', () => {
expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( currentColor );
} );

test( 'should enable alpha channel inside color picker', () => {
expect( renderedContentWithAlpha ).toMatchSnapshot();
} );

test( 'should return an rgba value on click.', () => {
renderedContentWithAlpha.simulate( 'changeComplete', { rgb: currentColorWithAlpha } );

expect( onChange ).toHaveBeenCalledTimes( 1 );
expect( onChange ).toHaveBeenCalledWith( currentColorWithAlphaParsed );
} );
} );
} );
} );