Picking colors for various parts of a WordPress site is a popular thing. Many plugins and themes have such options so users can customize the appearance of their site.

Gutenberg has a component which you might use when working on your new Gutenberg Block or even a separate app. If your block or app will have an element for which the user might need to change colors, you will use the component ColorPicker.

This Gutenberg Component can be found in the repository.

ColorPicker Properties

There are not a lot of properties for the ColorPicker since it is a quite simple component which does one thing only; picking a color 🙂

  • color – the color that we provide as the default/initial color
  • className – if provided, it will add the CSS class to the element alongside other classes
  • disableAlpha – this does not need a value at all. Just put it as a property and it will disable the alpha color picker
  • onChangeComplete – this is the function that you will use to handle the color change. You can then applying to other components of your block or anywhere else.

The function provided in onChangeComplete property will receive an object as a color which will have this attributes:

  • color – Hex value of the color
  • hex – transparent or #hexColor
  • hsl
  • hsv
  • rgb,
  • source,
  • oldHue – reference to the previous color hue

Default Classes

The default classes that you can use to change the component design are:

  • components-color-picker,
  • is-alpha-disabled – applied if the property disableAlpha is provided,
  • is-alpha-enabled – applied if the property disableAlpha is not provided.

Using the ColorPicker Component

Since this is a Gutenberg Component, it will be available on a page with the Gutenberg editor by using:

const { ColorPicker } = wp.components

Let’s consider wrapping the ColorPicker Component with our own dumb component. Maybe we have a naming convention that we want to follow and for example, onChange is used everything. By using that, the property onChangeComplete is not following that naming convention.

We created a wrapper, a function, that returns the same ColorPicker components. We then accept the property onChange and use that in the place of onChangeComplete.

How Gutenberg uses the ColorPicker Component

Since this component is a simple one and it’s usage is pretty straightforward, you don’t really have to search on how Gutenberg has implemented it. But it is always nice to see how the developers who created this component, use it in their own app.

I’ve found out that ColorPicker is being used by the component ColorPalette. Since the code is a bit longer here, I will paste only the part where ColorPicker is used. For the whole code of it, you can check the repo.

Conclusion

This is a component that I think it will be used a lot by other blocks. I can even see it being used in separate apps that utilize the Gutenberg packages for creating UIs.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

3 Comments

  1. Hello

    Please help me understand, I don’t understand how to embed Gutenberg components in blocks, namely in the structure itself … For example, here is such a code – https://paste.ubuntu.com/p/xWq6mSnYCs/

    If I saw an example of how this is done, then I could freely work with blocks and create them without problems. I need to see an example … I created a topic at https://wordpress.org/support/topic/help-understand-how-to-embed-components-in-blocks/

    thank you in advance

    Reply

    1. The code you’ve pasted here should be fine so that’s a good example to learn from.

      I would suggest you to go over the Gutenberg handbook for developers: https://developer.wordpress.org/block-editor/developers/

      Reply

      1. I found a solution to my problem here – https://rudrastyh.com/gutenberg/inspector-controls.html. And I re-read the documentation 10 times all lengthwise and across

        Reply

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.