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

Table Block: Fix margin/padding to include caption in spacing #68281

Merged

Conversation

Infinite-Null
Copy link
Contributor

Fixes: #68217

What?

The PR modifies the margin application in table blocks to ensure theme.json margin settings are applied to the figure wrapper instead of the inner table element.

Why?

When margins are set via theme.json, they are applied to the inner table element, causing undesirable spacing between the table and its caption. This PR fixes these spacing inconsistencies by applying margins to the correct element.

Testing Instructions

  1. Create a new post
  2. Insert a table block
  3. Add a caption to the table
  4. In theme.json, set a margin for the core/table block (e.g., 128px)
  5. Check the caption and table block

Screenshots or screencast

Before After
image image

Copy link

github-actions bot commented Dec 25, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Infinite-Null <ankitkumarshah@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: andersnoren <anlino@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@yogeshbhutkar
Copy link
Contributor

Hi @Infinite-Null, thanks for the PR ✨

I've tested out the PR and it seems to work as expected. The bug has been resolved.

Screenshot 2024-12-25 at 2 22 01 PM

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Table Affects the Table Block labels Dec 26, 2024
@t-hamano t-hamano self-requested a review December 26, 2024 04:03
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

One thing to be aware of is that the __experimentalSelector field affects all global styles. Other styles, except padding/margin, etc. are expected to apply to the table element, but in this PR they are applied to the figure element, which is not the intention.

Below is a screenshot of the border, color, and font styles applied to the Table block via the global styles:

trunk

image

This PR

image

Therefore, we cannot modify the __experimentalSelector field directly.

Instead, there is now a new Block Selectors API. Can we use this to target spacing support to figure elements and other elements to table elements as before?

@Infinite-Null
Copy link
Contributor Author

Hi @t-hamano,

Thank you for the suggestion. I’ve reverted the __experimentalSelector and applied the following to address the issue:

"selectors": {
    "spacing": {
	"margin": ".wp-block-table"
   }
},

Please review it at your convenience.
Thank You!

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

Sorry, my explanation was insufficient. The problem is that padding is applied to the table element, resulting in space between the table and the caption. I think what is expected here is that both padding and margin are applied to the figure element, just like with block instances.

"selectors": {
    "spacing": {
	"margin": ".wp-block-table"
   }
},

Therefore, the above code alone is not sufficient.

Furthermore, it would be a good idea to delete the __experimentalSelector field and aggregate it into the selectors field.

Considering these things, the following definition should work fine:

"selectors": {
	"root": ".wp-block-table > table",
	"spacing": ".wp-block-table"
},

@Infinite-Null
Copy link
Contributor Author

Thanks for the update!

Sorry, my explanation was insufficient. The problem is that padding is applied to the table element, resulting in space between the table and the caption. I think what is expected here is that both padding and margin are applied to the figure element, just like with block instances.

"selectors": {
    "spacing": {
	"margin": ".wp-block-table"
   }
},

Therefore, the above code alone is not sufficient.

Furthermore, it would be a good idea to delete the __experimentalSelector field and aggregate it into the selectors field.

Considering these things, the following definition should work fine:

"selectors": {
	"root": ".wp-block-table > table",
	"spacing": ".wp-block-table"
},

Hi @t-hamano,
Thank you for your guidance! I’ve made the necessary changes and submitted a review request. Please take a look at it at your convenience.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM! I tested it using the following theme.json:

Details
{
	"$schema": "../../schemas/json/theme.json",
	"version": 3,
	"settings": {
		"appearanceTools": true,
		"layout": {
			"contentSize": "840px",
			"wideSize": "1100px"
		}
	},
	"styles": {
		"blocks": {
			"core/table": {
				"color": {
					"text": "green",
					"background": "#ccc"
				},
				"border": {
					"width": "5px",
					"style": "dashed",
					"color": "#000"
				},
				"typography": {
					"fontSize": "20px",
					"fontWeight": "bold",
					"textTransform": "uppercase",
					"textDecoration": "underline"
				},
				"spacing": {
					"padding": {
						"top": "60px",
						"bottom": "60px"
					},
					"margin": {
						"top": "60px",
						"bottom": "60px"
					}
				}
			}
		}
	}

}
  • ✅ Padding and margin are applied to the figure element. All other styles are applied to the table element.
  • ✅ Styles applied in the Global Styles UI correctly override styles defined in theme.json.
  • Block instance styles correctly override styles applied in the Global Styles UI.

By the way, while reviewing this PR, I noticed that block instances cannot override typography styles applied in theme.json or global styles.

This is not a blocker because it occurs in trunk. I will merge this PR and then submit the issue.

@t-hamano t-hamano merged commit a34eaca into WordPress:trunk Dec 27, 2024
62 checks passed
@t-hamano t-hamano changed the title Table Block: Fix margin to include caption in spacing Table Block: Fix margin/padding to include caption in spacing Dec 27, 2024
@github-actions github-actions bot added this to the Gutenberg 20.0 milestone Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Table Affects the Table Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table block: Bottom margin pushes the caption down
3 participants