gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Add background colour transparency to blocks that support background colour

Open timhibberd opened this issue 7 years ago • 20 comments

Is your feature request related to a problem? Please describe. In modern design transparency background colour is used frequently (e.g. sub-blocks of text in a group block). On all blocks that provide a background colour the GUI control supports colour adjustment but not transparency adjustment. This forces the user to add custom CSS for a trivial action which is counter to the intention of GB to be Wix-like and useable by ordinary consumers.

Describe the solution you'd like On all blocks that provide a background colour the GUI control should include a transparency slider (like the opacity slider for the background image of the cover block).

Describe alternatives you've considered Unless there is a reason I am not aware of the technical implementation would be changing the background colour setter to use CSS RBGA vs. RGB as at present. The transparency slider control should be able to reuse the opacity slider currently in use by the Cover block.

timhibberd avatar Oct 24 '19 19:10 timhibberd

Agreed that this would be a helpful addition. Here's a super-quick mockup:

Screen Shot 2020-08-27 at 9 41 01 AM

kjellr avatar Aug 27 '20 13:08 kjellr

Hmmm colorpickers already have a disableAlpha parameter, and internally they're disabled. But I suppose we could expose that and enable it for all background-colors... :thinking:

aristath avatar Aug 27 '20 14:08 aristath

Thanks @aristath for the heads up! Indeed switching disableAlpha to false returns an opacity picker inside the color picker I have prepared the groundwork https://github.com/WordPress/gutenberg/pull/25421 and trying to apply it in the background-color of the cover block. Haven't found a global way to enable it for all background-colors and I am not sure we want that.

Now I am searching how to actually pass that alpha value to the component cause currently defaults to rgb in the editor and hex in the frontend.

cpapazoglou avatar Sep 17 '20 15:09 cpapazoglou

Added the transparency control to cover, button, paragraph, heading, list. I hope it is not overused 😃 !

cpapazoglou avatar Sep 18 '20 13:09 cpapazoglou

Can the transparency control also be added to container and group?

rooksads avatar Oct 06 '20 17:10 rooksads

Can the transparency control also be added to container and group?

Excellent suggestion, I will look into it!

cpapazoglou avatar Oct 16 '20 09:10 cpapazoglou

I would love this in general <3 I was thinking for group blocks specifically today, but things like Media & Text, or paragraph blocks etc would be nifty.

sophiegyo avatar Nov 13 '20 09:11 sophiegyo

Any news on that issue? Would love to see a fix as well

laidbackindustries avatar May 07 '21 08:05 laidbackindustries

This task is 19 years old, how about already making progress in it? This is one of the reasons why gutenberg is so lagging behind the competition, there are no basic functions that should be built in from the beginning.

mupic avatar May 17 '21 15:05 mupic

#27960 It is related. I believe many users want to have it. Also, related PR is #30493

ami-momo avatar Jul 04 '21 20:07 ami-momo

Any news on this? I'm making a new theme using blocks and I need opacity but I'm so surprised that there is no alpha selector! Don't you think alpha selection is a must-have in every serious design solution? It would be so nice to have this feature in the next Gutenberg release...

+1 to add color transparency to any block that supports background color, especially the group one!

lecoqlibre avatar Aug 29 '21 21:08 lecoqlibre

+1; someone contacted us today for help with this. We couldn't find another way to fix it but to give them CSS.

supernovia avatar Sep 03 '21 00:09 supernovia

Maybe this will interest someone here like @supernovia?

I made a filter to handle the opacity of the background color. One you have put it in your theme's function file, you just have to set the background color and add the CSS class bg-opacity-<opacity percent> to the block you want to set the opacity for (eg. bg-opacity-80 to make it 80% opacity). It works with custom colors and also with the predefined WordPress colors (white, pale pink, and so on). The filter append the alpha hex value to the end of the color hex.

/** Add an alpha to the background-color of any block that support bg based onto the CSS class bg-opacity-%% */
add_filter( 'render_block', function( string $block_content, array $block ) {
  if( ! isset( $block[ 'attrs' ][ 'className' ] ) )
    return $block_content;

  if( ! isset( $block[ 'attrs' ][ 'style' ][ 'color' ][ 'background' ] ) && ! isset( $block[ 'attrs' ][ 'backgroundColor' ] ) )
    return $block_content;

  $classes = explode( ' ', $block[ 'attrs' ][ 'className' ] ); // classes should be separated with space
  $mask = 'bg-opacity-'; // the base CSS class name to search for
  $opacity = array();
  
  // Search for the opacity through the CSS classes, the first one will be used
  foreach( $classes as $class ) {
    $match = array();
    if( ! 1 == preg_match( "#{$mask}([0-9]+)#", $class, $match ) )
      continue;
    $opacity = intval( $match[ 1 ] );
    break;
  }
  
  if( is_array( $opacity ) && empty( $opacity ) )
    return $block_content;

  // Compute the alpha value
  $alpha = dechex( 255 * $opacity / 100 );

  // Add a background color with an alpha when using predefined color name
  if( isset( $block[ 'attrs' ][ 'backgroundColor' ] ) ) {
    $colorName = $block[ 'attrs' ][ 'backgroundColor' ];
    $colors = array(
      'pale-pink' => '#f78da7', 
      'vivid-red' => '#cf2e2e', 
      'luminous-vivid-orange' => '#ff6900', 
      'luminous-vivid-amber' => '#fcb900', 
      'light-green-cyan' => '#7bdcb5', 
      'vivid-green-cyan' => '#00d084', 
      'pale-cyan-blue' => '#8ed1fc', 
      'vivid-cyan-blue' => '#0693e3', 
      'vivid-purple' => '#9b51e0', 
      'white' => '#ffffff', 
      'very-light-gray' => '#eeeeee', 
      'cyan-bluish-gray' => '#abb8c3', 
      'very-dark-gray' => '#313131', 
      'black' => '#000000'
    );
    $color = $colors[ $colorName ] . $alpha;
    $block_content = preg_replace( '#style="#', "style=\"background-color:{$color};", $block_content, 1 );
  }

  // Replace the background color with the alpha when using custom color
  else {
    $color = $block[ 'attrs' ][ 'style' ][ 'color' ][ 'background' ] . $alpha;
    $block_content = preg_replace( '#background-color:(.+);#', "background-color:{$color};", $block_content, 1 );
  }
  
  return $block_content;
}, 10, 2 );

lecoqlibre avatar Sep 03 '21 07:09 lecoqlibre

I am not able to understand why can't we simply apply to group block, we already have it in cover block working perfectly, then why group not work the same way ? If we provide rgba format color in theme.json, then it should simply show opacity parameter too to change the color opacity.

uniquesolution avatar Nov 24 '21 08:11 uniquesolution

The ColorPicker component has support for alpha transparency.

See: https://developer.wordpress.org/block-editor/reference-guides/components/color-picker/#enablealpha-boolean

In this example, I've manually set the prop to enable it for all colors (background, link etc) in the block inspector:

Screen Shot 2022-01-05 at 3 26 21 pm

A question is whether we should allow enabling alpha transparency via block supports, e.g., on a per block basis via block.json, or everywhere.

I can't think of a reason why we would want to limit this useful and, thanks to the new color picker layout, unobtrusive feature.

Still, there might be a reason for theme developers to want to limit usage of the effect.

ramonjd avatar Jan 05 '22 04:01 ramonjd

Here is a PR waiting for review since mid 2021: https://github.com/WordPress/gutenberg/pull/30493

here is a second PR in WIP for some reasons: https://github.com/WordPress/gutenberg/pull/37731

we need some progress on this after 2 years

dennisheiden avatar Jan 17 '22 15:01 dennisheiden

here is a second PR in WIP for some reasons:

Hallo @dennisheiden

#30493 (and #25421) employs disableAlpha, which has been deprecated since the new ColorPicker, though there is a legacy bridge for such props, I guess we should probably move away from it.

#37731 enables alpha via the supported enableAlpha prop.

If you have time to test #37731 that would be great, thank you!

ramonjd avatar Jan 18 '22 00:01 ramonjd

@ramonjd Thanks for the clarification, this helps to prevent wasting time with obsolete PRs. ty.

dennisheiden avatar Jan 18 '22 13:01 dennisheiden

Any word on this? seems as if the time it takes to respond here would have already fixed this. As of today no transparency in the group block. Such an easy fix takes years, why?

richwalton avatar Feb 06 '22 17:02 richwalton

Hi @richwalton,

https://github.com/WordPress/gutenberg/pull/37731 was merged a couple of weeks back. The change should be part of Gutenberg 12.5, which was released a few days back.

If you update the Gutenberg plugin to 12.5 you should see the following controls:

Screen Shot 2022-02-07 at 3 49 42 pm

ramonjd avatar Feb 07 '22 04:02 ramonjd