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 #5835

Closed
wants to merge 19 commits into from

Conversation

diegoliv
Copy link

Description

Currently, the <ColorPalette /> component enforces the alpha channel from react-color to be disabled. Like described on #5811, there are benefits of giving developers the option to enable the alpha channel inside the color picker.

Tested With

WordPress 4.9.4
Gutenberg 2.4.0
PHP 7.0.3 / nginx
MySQL 5.5.55

Types of changes

This PR introduces the ability to conditionally set a disableAlpha parameter for the <ColorPallete /> component. It is set to false by default so it won't conflict with existing blocks that uses this component. Also, the returned color string depends on how the alpha channel inside the color picker is set. If opacity is 100%, we just return the hex value for the color. If opacity has a different level, then we return the rgba() value for the color.

@bordoni
Copy link
Member

bordoni commented Mar 28, 2018

Related: #5382

aduth
aduth previously requested changes Mar 28, 2018
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

There should not be modifications to the package-lock.json file introduced by these changes.

@@ -59,9 +59,13 @@ export function ColorPalette( { colors, disableCustomColors = false, value, onCh
renderContent={ () => (
<ChromePicker
color={ value }
onChangeComplete={ ( color ) => onChange( color.hex ) }
// onChangeComplete={ ( color ) => onChange( color.hex ) }
Copy link
Member

Choose a reason for hiding this comment

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

Commented line should be removed.

onChangeComplete={ ( color ) => onChange( color.hex ) }
// onChangeComplete={ ( color ) => onChange( color.hex ) }
onChangeComplete={ ( color ) => {
const colorString = color.rgb.a === 1 ? color.hex : sprintf( 'rgba(%s,%s,%s,%s)', color.rgb.r, color.rgb.g, color.rgb.b, color.rgb.a );
Copy link
Member

Choose a reason for hiding this comment

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

Unlike in PHP, sprintf is not a native function, and could have performance implications. While this is not a function we would expect to be called with any frequency, I'd rather we avoided become too comfortable with its use where simple concatenation would suffix.

You could, for example, take advantage of Array#join's default argument being a comma:

let colorString;
if ( color.rgb.a === 1 ) {
	colorString = color.hex;
} else {
	const { r, g, b, a } = color.rgb;
	colorString = `rgba(${ [ r, g, b, a ].join() })`;
}

(Or be explicit with .join( ',' ))

@aduth
Copy link
Member

aduth commented Mar 28, 2018

@bordoni Is the need to create a color string as rgba not needed? (Looking at #5382)

@diegoliv
Copy link
Author

@aduth thanks for reviewing my PR! Ok, I think that the review request is now addressed. Let me know if it's all good.

@danielbachhuber
Copy link
Member

@diegoliv Looks like you have a test failing on:

TypeError: Cannot read property 'a' of undefined

Can you address this issue?

@diegoliv
Copy link
Author

diegoliv commented Apr 3, 2018

@danielbachhuber ok, now it seems that all tests have passed. Let me know what you think!

@danielbachhuber
Copy link
Member

@diegoliv Looks better. Can you include a test or two for your change?

@diegoliv
Copy link
Author

diegoliv commented Apr 4, 2018

@danielbachhuber if you're talking about writing some unit tests, I'm sorry, I don't have much experience with that. Can someone help me on this? I can work on something with the right guidance. Now, if you're talking about real tests, I tested the disableAlpha parameter on one of my own custom blocks. Tested on:

  • Chrome 65
  • Firefox 59
  • Edge 41

All of my tests worked as expected. I also recorded a quick video showing the alpha channel in action on my custom block. Link to the video here.

@danielbachhuber
Copy link
Member

@diegoliv Apologies for the delay!

I am talking about unit tests. However, I'm not yet an expert on Gutenberg's test suite, so I'll pull what I can together for you :)

  • Here's an overview to the Gutenberg test suite.
  • For this particular case, I think Jest tests are most appropriate.
  • You're looking to modify the blocks/color-palette/test/index.js file.
  • For the purposes of this pull request, we'll want to make sure:
    • both variants of disableAlpha are covered (one case with true and one case with false).
    • your changes to onChangeComplete are covered too.

Hope this helps! @aduth Feel free to weigh in with any other details you think relevant.

@diegoliv
Copy link
Author

diegoliv commented Apr 6, 2018

@danielbachhuber thanks for the help! I don't have any experience with unit tests, so this is a good place to start learning. The problem is that I'm not sure if I'm doing it right, since I'm learning as I go, so I probably need someone with more experience to review my work. I just pushed to this PR two new tests, can you take a look and let me know if I'm on the right path or not? Thanks!

@danielbachhuber
Copy link
Member

@diegoliv I think so (although it looks like your test is still failing). I'll defer to @aduth on a second opinion though.

@@ -45,19 +47,19 @@ describe( 'ColorPalette', () => {

test( 'should call onClick with undefined, when the clearButton onClick is triggered', () => {
const clearButton = wrapper.find( '.button-link.blocks-color-palette__clear' );

Copy link
Member

Choose a reason for hiding this comment

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

These lines with added whitespace should be removed

Your IDE or Editor probably has an option along the lines of "When enabled, will trim trailing whitespace when saving a file."

You can also install an "Editor Config" extension for your IDE/Editor from http://editorconfig.org/#download that will then inherit the Gutenberg settings from https://github.com/WordPress/gutenberg/blob/master/.editorconfig

expect( clearButton ).toHaveLength( 1 );

Copy link
Member

Choose a reason for hiding this comment

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

extraneous whitespace should be removed

clearButton.simulate( 'click' );

Copy link
Member

Choose a reason for hiding this comment

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

extraneous whitespace should be removed

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

Copy link
Member

Choose a reason for hiding this comment

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

extraneous whitespace should be removed

test( 'should allow disabling custom color picker', () => {
expect( shallow( <ColorPalette colors={ colors } disableCustomColors={ true } value={ currentColor } onChange={ onChange } /> ) ).toMatchSnapshot();
} );

Copy link
Member

Choose a reason for hiding this comment

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

extraneous whitespace should be removed

} );
} );

Copy link
Member

Choose a reason for hiding this comment

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

extraneous new line here should be removed

@diegoliv
Copy link
Author

diegoliv commented Apr 9, 2018

@ntwb thanks for the heads up on the white spaces, I'm using Visual Studio Code on Windows. Just fixed the white spaces and line breaks. It should be fine now!

@danielbachhuber
Copy link
Member

@diegoliv Sorry for the delay here. Can you resolve the merge conflict and then we'll get this landed?

@diegoliv
Copy link
Author

@danielbachhuber ok, just removed the legacy blocks/color-palette directory, since it was moved to the components directory, but I'm having some issues on updating the unit test snapshot. I merged master, updated node modules, but the unit-test fails. Here's the error I'm seeing:

gutenberg_unit-test-issue

Am I doing something wrong?

@diegoliv
Copy link
Author

@danielbachhuber ok, so I forgot to run npm run build. After I did it, the snapshot was generated, but I the test is pointing for some fails that are not related with the changes I did. Should I do something about it?

Here's what I'm seeing:
gutenberg_unit-test-issue_2

@@ -830,7 +830,27 @@ function gutenberg_common_scripts_and_styles() {
function gutenberg_enqueue_registered_block_scripts_and_styles() {
$is_editor = ( 'enqueue_block_editor_assets' === current_action() );

// parse and prepare blocks attributes to be used on the 'assets_callback' call
if( !$is_editor && is_singular() ) {
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be here....

@danielbachhuber
Copy link
Member

It looks like some unexpected changes snuck into this pull request. #6956 might be related to the unexpected test failures. Can you port your changes over to a new pull request and we can work from there?

@aduth aduth dismissed their stale review May 30, 2018 13:42

Original requested changes addressed.

@diegoliv
Copy link
Author

@danielbachhuber ok, so I updated my local repo and tried to run npm install to install any new dependencies. But I got this error:

$ npm install

> [email protected] postinstall C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg
> lerna bootstrap --hoist && npm run build:packages

module.js:487
    throw err;
    ^

Error: Cannot find module 'restore-cursor'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg\node_modules\cli-cursor\index.js:2:23)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `lerna bootstrap --hoist && npm run build:packages`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: (...)

Also, running npm run build throws this error:

$ npm run build

> [email protected] prebuild C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg
> check-node-version --package


> [email protected] build C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg
> npm run build:packages && cross-env NODE_ENV=production webpack


> [email protected] build:packages C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg
> rimraf ./packages/*/build ./packages/*/build-module && node ./bin/packages/build.js

C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg\bin\packages\get-babel-config.js:24
                ...babelDefaultConfig,
                ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Diego\Local Sites\wordpress\app\public\wp-content\plugins\gutenberg\bin\packages\build.js:22:24)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build:packages: `rimraf ./packages/*/build ./packages/*/build-module && node ./bin/packages/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build:packages script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Diego\AppData\Roaming\npm-cache\_logs\2018-05-31T05_16_59_359Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `npm run build:packages && cross-env NODE_ENV=production webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Diego\AppData\Roaming\npm-cache\_logs\2018-05-31T05_16_59_413Z-debug.log

I'm doing this on the master branch. Is there any additional steps that I should do?

@danielbachhuber
Copy link
Member

I'm doing this on the master branch. Is there any additional steps that I should do?

Not sure, to be honest. Are you running node v8.x? Feel free to pop into #core-editor if you need help debugging.

@diegoliv
Copy link
Author

@danielbachhuber yep, using 8.0.0. I'll check on Slack if someone can help me on this.

@danielbachhuber
Copy link
Member

Closing in favor of #7151

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants