Skip to content

Conversation

juanfra
Copy link
Member

@juanfra juanfra commented Jan 19, 2023

Addition by @t-hamano

To resume this PR, I did the following:

  • Merge the latest trunk into this branch
  • Resolve all conflicts
  • Rechecked the whole code and added the missing paragraph block class

This PR has a lot of diffs, mostly related to testing and documentation updates.

The actual files to check are:


Original description by @juanfra

What?

Adding the wp-block-paragraph to the paragraph block elements.

Fixes #46863

Why?

It was reported in #46863 that the style changes for the paragraph block were affecting all the paragraphs, and this was affecting other pieces of the site such as the site tagline (and others).

How?

Adding the wp-block-paragraph class to the paragraph block for new content, and parsing the block output to add it for previously generated content.

Testing Instructions

For new content

  1. Create a new page with the build from this PR.
  2. Add some paragraphs
  3. Save the page.
  4. Confirm that the paragraphs have the wp-block-paragraph class.

For previously generated content

  1. Create a sandbox with the trunk build for this plugin.
  2. Create a page with paragraphs (you can use custom classes).
  3. Save the page.
  4. Update the build to this one.
  5. Open the page and confirm that the wp-block-paragraph class was added to the previously generated content.

For the problem reported in #46863

  1. Open Site Editor > Styles.
  2. Head to Blocks > Paragraph
  3. Make changes and notice it doesn't have an impact on the Site Title and Site Tagline (or any other <p> that doesn't have the wp-block-paragraph class)

Screenshots or screencast

Screen.Recording.2023-02-03.at.17.18.43.mov

@juanfra
Copy link
Member Author

juanfra commented Jan 19, 2023

I'll be working on fixing tests and all.

@carolinan carolinan self-requested a review January 30, 2023 12:42
# Conflicts:
#	lib/compat/wordpress-6.2/block-patterns/centered-footer-with-social-links.php
#	lib/compat/wordpress-6.2/block-patterns/centered-footer.php
#	lib/compat/wordpress-6.2/block-patterns/footer-with-large-font-size.php
#	lib/compat/wordpress-6.2/block-patterns/simple-header-with-image.php
@juanfra juanfra changed the title [WIP] Add CSS class to paragraph block Add CSS class to paragraph block Feb 3, 2023
@juanfra juanfra marked this pull request as ready for review February 3, 2023 16:21
juanfra added 3 commits March 1, 2023 16:36
# Conflicts:
#	packages/block-editor/src/hooks/test/__snapshots__/align.native.js.snap
#	packages/block-library/src/cover/test/__snapshots__/edit.native.js.snap
#	packages/block-library/src/embed/test/__snapshots__/index.native.js.snap
#	packages/e2e-tests/specs/editor/blocks/__snapshots__/heading.test.js.snap
#	packages/e2e-tests/specs/editor/blocks/pullquote.test.js
#	packages/e2e-tests/specs/editor/plugins/__snapshots__/container-blocks.test.js.snap
#	packages/e2e-tests/specs/editor/plugins/__snapshots__/cpt-locking.test.js.snap
#	packages/e2e-tests/specs/editor/plugins/__snapshots__/inner-blocks-render-appender.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/block-deletion.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/block-grouping.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/block-hierarchy-navigation.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/embedding.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/inserting-blocks.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/keep-styles-on-block-transforms.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/links.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/list-view.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap
#	packages/e2e-tests/specs/editor/various/__snapshots__/rich-text.test.js.snap
#	packages/e2e-tests/specs/editor/various/block-grouping.test.js
#	packages/e2e-tests/specs/editor/various/format-library/__snapshots__/text-color.test.js.snap
#	packages/e2e-tests/specs/editor/various/multi-block-selection.test.js
#	packages/e2e-tests/specs/widgets/editing-widgets.test.js
#	packages/format-library/src/text-color/test/__snapshots__/index.native.js.snap
#	packages/react-native-editor/__device-tests__/helpers/test-data.js
#	packages/react-native-editor/src/initial-html.js
#	packages/rich-text/src/test/__snapshots__/index.native.js.snap
#	test/e2e/specs/editor/blocks/__snapshots__/List-can-be-exited-to-selected-paragraph-1-chromium.txt
#	test/e2e/specs/editor/blocks/__snapshots__/List-selects-all-transformed-output-1-chromium.txt
#	test/e2e/specs/editor/various/font-size-picker.spec.js
#	test/integration/__snapshots__/blocks-raw-handling.test.js.snap
# Conflicts:
#	packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap
#	packages/e2e-tests/specs/editor/various/multi-block-selection.test.js
@t-hamano
Copy link
Contributor

t-hamano commented Jul 3, 2025

A more conservative approach might be to disable block support for className and dynamically add CSS classes, as was previously proposed in this PR. The advantage of this approach is that it doesn't pollute the saved HTML, so if the impact proves to be significant, we'd be able to revert before shipping the feature in core.

@justintadlock
Copy link
Contributor

This PR will definitely create back-compat issues for some themes, but I think we should implement it. It fixes a larger problem of styles unintentionally bleeding out elsewhere.

But we do need a followup PR that provides an upgrade path for themers who were using core/paragraph to style general paragraphs, which I think should be styles.elements.paragraph. If we land this early for 6.9, it gives us plenty of time to get the word out and for folks to test.

@fabiankaegy
Copy link
Member

A more conservative approach might be to disable block support for className and dynamically add CSS classes, as was previously proposed in this PR. The advantage of this approach is that it doesn't pollute the saved HTML, so if the impact proves to be significant, we'd be able to revert before shipping the feature in core.

Personally I would vote for this approach. Adding the class via the render_callback dynamically gives us most of the benefit with almost none of the uncertainty / downsides.

We can then introduce the new paragraph element in theme.json independently from this class name change and treat them as individual features. And we don't have backwards compatibility issues.

@t-hamano
Copy link
Contributor

t-hamano commented Jul 15, 2025

Thank you everyone for your opinions. I think the following approach is the best, but is my understanding correct?

  1. Dynamically add a CSS class to the Paragraph block and change the global style selector from p to .wp-block-paragraph. This allows users to test the new behavior on sites that use the latest Gutenberg plugin. However, the className support for the Paragraph block remains disabled, so the HTML saved to the database remains unchanged.
  2. Adds support for styles.elements.paragraph so that theme developers can still apply styles to all p elements.
  3. If any significant issues are discovered during testing, revert these changes before shipping them in WP 6.9 and consider a more optimal approach.
  4. Enables the className support for the Paragraph block. This means that when content is updated or a new Paragraph block is inserted, the HTML will have the .wp-block-paragraph class.

Pinging @WordPress/gutenberg-core for visibility

@t-hamano
Copy link
Contributor

  1. Adds support for styles.elements.typography so that theme developers can still apply styles to all p elements.

There is one concern about this: in the default themes, the elements.link field is defined to use styling for link elements in most cases. Example:

Details
{
	"version": 3,
	"styles": {
		"blocks": {
			"core/paragraph": {
				"elements": {
					"link": {
						"color": {
							"text": "#ff0000"
						}
					}
				}
			}
		}
	}
}

Even if styles.elements.paragraph were supported, nested element selectors would not be supported, so we would not be able to write it like this:

Details
{
	"version": 3,
	"styles": {
		"elements": {
			"paragraph": {
				"elements": {
					"link": {
						"color": {
							"text": "#ff0000"
						}
					}
				}
			}
		}
	}
}

Therefore, theme developers are forced to write the following to preserve their existing selectors by using the css field:

Details
{
	"version": 3,
	"styles": {
		"elements": {
			"paragraph": {
				"css": "& a {color:#ff0000;}"
			}
		}
	}
}

@mtias
Copy link
Member

mtias commented Aug 1, 2025

I'm not sure we should do this. The original issue describes problems with site tagline and site title, two blocks that are good candidates to be a paragraph block + bindings in the future instead of separate custom blocks, so they would probably inherit the classes anyways.

@carolinan
Copy link
Contributor

I think we must to consider other than core blocks, anything that uses <p> is affected by the current problem.

@t-hamano
Copy link
Contributor

  1. Dynamically add a CSS class to the Paragraph block and change the global style selector from p to .wp-block-paragraph. This allows users to test the new behavior on sites that use the latest Gutenberg plugin. However, the className support for the Paragraph block remains disabled, so the HTML saved to the database remains unchanged.

I would like to recommend this approach, but implementing it in this PR would require reverting too many files, so I have submitted a new #71207.

@scruffian
Copy link
Contributor

What is the use case for setting styles on the paragraph block?

@t-hamano
Copy link
Contributor

What is the use case for setting styles on the paragraph block?

For example, a user may want to change the typography styles on the Paragraph block via the global styles. The problem is that this style will affect p elements outside of the Paragraph block as well. Currently, there is no way to apply a style to just the Paragraph block.

The purpose of this PR is similar to #42122.

@scruffian
Copy link
Contributor

For example, a user may want to change the typography styles on the Paragraph block via the global styles.

Why would a user want to do this? What would it achieve?

@t-hamano
Copy link
Contributor

For example, a user may want to change the typography styles on the Paragraph block via the global styles.

Why would a user want to do this? What would it achieve?

I don't think there's any particular reason. I think users simply want to change the style of the paragraph block, just like with any other block.

@scruffian
Copy link
Contributor

I don't think there's any particular reason. I think users simply want to change the style of the paragraph block, just like with any other block.

Without a use case in mind it's hard to be certain that the current behaviour is not the right one :)

@t-hamano
Copy link
Contributor

Users want to apply styles to Paragraph blocks only, without affecting other p elements. Isn't this exactly the use case?

@ramonjd
Copy link
Member

ramonjd commented Aug 14, 2025

Users want to apply styles to Paragraph blocks only, without affecting other p elements. Isn't this exactly the use case?

I think it's conceivable that someone out there might want to:

  1. Set a root typography style for the entire site
  2. Create an exception for paragraph blocks

Why not?

Edit: I realize this is already possible, but I forgot to mention the assumption that it'd be done to the exclusion of paragraph elements that live in cover blocks for example. In that case, you'd want the cover block global style to have precedence. All that comes to mind is a news site - it's text-based, and article paragraphs have a syntactical relevance that might need to be style to the exclusivity of all others. I'm reaching 😄 Might be an edge case; the original issue outlines another. Can folks think of better examples?

@carolinan
Copy link
Contributor

Users want to apply styles to Paragraph blocks only, without affecting other p elements. Isn't this exactly the use case?

Exactly. And to have different styling for P elements inside other blocks without added complexity.

@scruffian
Copy link
Contributor

article paragraphs have a syntactical relevance that might need to be style to the exclusivity of all others

Thanks for the example @ramonjd. In this case I would expect that styling the Post Content block would be more effective, since paragraph blocks are more general purpose.

@t-hamano
Copy link
Contributor

Please note that there is also a classic theme that has a theme.json. The post content block does not exist in the classic theme.

@carolinan
Copy link
Contributor

carolinan commented Aug 17, 2025

I think we are missing the reason why there is a reluctancy to add this. I don't see the motivation voiced here?

A motivation such as "I think it is a bad idea to add a class to the paragraph because it will increase the size of the markup exactly because the paragraph is the most used block".

One person made the argument that the current way the paragraph is styled may be wrong.
I can perhaps buy that we as contributors made a mistake by allowing the paragraph block to be styled with global styles and theme.json in the first place: But I don't see a way to back out of it now since removing this ability would break the styling of existing sites? Do you?

@t-hamano
Copy link
Contributor

I can perhaps buy that we as contributors made a mistake by allowing the paragraph block to be styled with global styles and theme.json in the first place: But I don't see a way to back out of it now since removing this ability would break the styling of existing sites? Do you?

I don't think the fact that global styles and theme.json support the paragraph block is a problem in itself, but I think the important thing is whether we should use an element or class name selector.

Currently, the Paragraph block is the only core block that defines an element as a selector instead of a wrapper classname, which I find strange.


P.S. I did a bit of history research and discovered that the wrapper classname for the Paragraph block (formerly known as the Text block) was intentionally disabled from the very beginning when the mechanism was introduced (over 8 years ago!).

https://github.com/WordPress/gutenberg/pull/1381/files#diff-7ae639823f537a52a09815b3df39ae066a9b3b86c95c185abe184d4c246aba45R26

I'm not sure why the wrapper classname was disabled, but maybe there was no global styles or theme.json back then, so there was no need for a classname on paragraph elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Paragraph Affects the Paragraph Block [Status] In discussion Used to indicate that an issue is in the process of being discussed [Type] Enhancement A suggestion for improvement.
Projects
No open projects
Status: Punted to 6.5
Development

Successfully merging this pull request may close these issues.

Styles: Paragraph styling impacts Site Title & Site Tagline