-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add CSS class to paragraph block #47282
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
base: trunk
Are you sure you want to change the base?
Conversation
I'll be working on fixing tests and all. |
# 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
…k and affect test results.
# 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
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. |
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 |
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. |
Thank you everyone for your opinions. I think the following approach is the best, but is my understanding correct?
Pinging @WordPress/gutenberg-core for visibility |
There is one concern about this: in the default themes, the Details{
"version": 3,
"styles": {
"blocks": {
"core/paragraph": {
"elements": {
"link": {
"color": {
"text": "#ff0000"
}
}
}
}
}
}
} Even if 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 Details{
"version": 3,
"styles": {
"elements": {
"paragraph": {
"css": "& a {color:#ff0000;}"
}
}
}
} |
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. |
I think we must to consider other than core blocks, anything that uses |
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. |
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 The purpose of this PR is similar to #42122. |
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. |
Without a use case in mind it's hard to be certain that the current behaviour is not the right one :) |
Users want to apply styles to Paragraph blocks only, without affecting other |
I think it's conceivable that someone out there might want to:
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? |
Exactly. And to have different styling for P elements inside other blocks without added complexity. |
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. |
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. |
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 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!). 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. |
Addition by @t-hamano
To resume this PR, I did the following:
This PR has a lot of diffs, mostly related to testing and documentation updates.
The actual files to check are:
packages/block-library/src/paragraph/block.json
: The paragraph block will have the class namewp-block-paragraph
added to it, and the CSS selectors generated by the global styles will also use this class.packages/block-library/src/paragraph/index.php
: Add a class name to the Paragraph block that is already saved in the database via the render callback function.phpunit/block-supports/layout-test.php
phpunit/block-supports/typography-test.php
phpunit/class-wp-theme-json-test.php
phpunit/blocks/render-block-media-text-test.php
phpunit/blocks/render-block-paragraph-test.php
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
wp-block-paragraph
class.For previously generated content
wp-block-paragraph
class was added to the previously generated content.For the problem reported in #46863
<p>
that doesn't have thewp-block-paragraph
class)Screenshots or screencast
Screen.Recording.2023-02-03.at.17.18.43.mov