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

Style default attributes for block supports are not respected in dynamic render #52600

Closed
dutchigor opened this issue Jul 13, 2023 · 2 comments
Closed
Labels
[Package] Block library /packages/block-library [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Bug An existing feature does not function as intended

Comments

@dutchigor
Copy link

dutchigor commented Jul 13, 2023

Description

Adding default values for at least a color or dimensions.minHeight support in the block.json is not respected when adding them to a dynamic block.

It seems that get_block_wrapper_attributes only generates the style tag when values have been set in the block, but not when a default value is passed in the block type arguments.

I have tried to add the attributes both to the block.json file and the register_block_type $args parameter but neither do the trick.

Step-by-step reproduction instructions

  1. Add color and minHeight support, and attribute defaults for these supports to the block.json:
  "supports": {
  	"color": {
  		"text": true
  	},
  	"dimensions": {
  		"minHeight": true
  	}
  },
  "attributes": {
  	"style": {
  		"type": "object",
  		"default": {
  			"color": {
  				"text": "#ff0000"
  			},
  			"dimensions": {
  				"minHeight": "100px"
  			}
  		}
  	}
  }
  1. Add render callbacck to plugin file:
  register_block_type( __DIR__ . '/build', array(
  	'render_callback' => fn ($attributes) =>
  		'<p ' . get_block_wrapper_attributes() . '>' . $attributes['style']['color']['text'] . '</p>',
  	// Adding supports and attributes here does not matter. The problem exists either way.
  	'supports' => array(
  		'color' => array(
  			'text' => true,
  		),
  	),
  	"attributes" => array(
  		"style" => array(
  			"type" => "object",
  			"default" => array(
  				"color" => array(
  					"text" => "#ff0000"
  				),
  			),
  		),
  	),
  ) );
  1. Add a setter for the color in the edit component:
function Edit({attributes, setAttributes}) {
  return (
  	<div { ...useBlockProps() }>
  		<button onClick={() => setAttributes({style: { color: {text: "#ffff00"}}})}>
  			Change color
  		</button><span>{ attributes.style.color.text }</span>
  	</div>
  );
}
  1. Add the block to a post or page. Notice that the min height and text color show as expected.
  2. Update the post without pressing the change color button
  3. View the updated post. Notice that the printed value for the color is #ff0000 but neither the text color nor the default height styling is applied.
  4. Go back to the editor, press the change color button and save the post
  5. View the post again. Notice that now styling is applied in line with the printed variable.

Screenshots, screen recording, code snippet

No response

Environment info

  • Wordpress: 6.2.2
  • Gutenberg: default for WP 6.2.2
  • Theme: Tested on Twenty Twenty-Three and Twenty Twenty
  • Browser: Chrome.

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@jordesign jordesign added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. [Package] Style Engine /packages/style-engine labels Jul 13, 2023
@t-hamano
Copy link
Contributor

Hi @dutchigor,

The problem you reported appears to have been reported several times in the past.

As far as I know, the current solution to this problem is to set a default value similar to block.json if the key does not exist in $attributes. Here is an example of an archive block:

$type = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly';

I will close this issue as a duplicate, but if I have missed something, please let me know.

@t-hamano t-hamano closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2023
@t-hamano t-hamano added [Package] Block library /packages/block-library [Tool] Create Block /packages/create-block and removed Needs Testing Needs further testing to be confirmed. [Package] Style Engine /packages/style-engine [Tool] Create Block /packages/create-block labels Jul 15, 2023
@dutchigor
Copy link
Author

Thanks! Not sure how I missed those. This would mean manually recreating the styles otherwise provided by adding supports in the block registration. I'll do this for now and continue the discussion on #50229 as this seems to be about the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block library /packages/block-library [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants