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

feat: get default WP block attributes #1

Merged
merged 1 commit into from Jan 22, 2024
Merged

feat: get default WP block attributes #1

merged 1 commit into from Jan 22, 2024

Conversation

Bush95
Copy link
Contributor

@Bush95 Bush95 commented Jan 18, 2024

This allows you to get the default wp block attributes in the template.
It was tricky to do as main function returns string with attributes.
example: string(39) “class=”wp-block-giantpeach-testimonial””

I had to keep acf in mind too https://www.advancedcustomfields.com/resources/acf-blocks-using-get_block_wrapper_attributes/
Wp by default is adding the styles, so there is no need to render it in the admin.

I managed to split it into chunks and use it as an array with keys.

There are 2 ways to use it in the template.
First by creating empty element and passing raw string directly into it like so:

<div {{ not isAdmin ? blockAttributes.raw | raw }}>
    ... content ...
</div>

Second option is to add it to class attribute by key:

<section class="{{ classes.block.name }} {{ classes.block.name }} {{ not isAdmin ? blockAttributes.class }} some-other-custom-class">
    ... content ...
</div>

I think second option is better as we don't have to create additional div as a wrapper and we have more control over different attributes. Because of these flexibility I decided to use this attributes individually per block, rather than using it globally as a wrapper for every block

@Bush95
Copy link
Contributor Author

Bush95 commented Jan 18, 2024

I played more with it and there is also an option to define custom padding - on frontend we can use it as a style attribute of additional div, so we avoid overriding paddings from parent.
<div {% if blockAttributes.style %}style="{{ blockAttributes.style | raw }}"{% endif %}>

Before that we need to add support in block.json - I think that background and vertical padding can be used for parent blocks

"supports": { "jsx": true, "color": { "text": false, "link": false, "background": true }, "spacing": { "padding": ["vertical"] } }

Sad news is that there is a serious bug, where we can't set default values of fields like background colour, paddings etc.
It works in editor, but it's not getting applied to frontend, so it's better to leave it transparent and select color manually

WordPress/gutenberg#7342 (comment)

@Bush95
Copy link
Contributor Author

Bush95 commented Jan 22, 2024

Also
I tried working with text colors "supports": { "color": { "text": true}}, but for some reason the styles created by WordPress don't cover all the elements in the container. This requires further investigation

@Bush95 Bush95 merged commit 6fcfe25 into main Jan 22, 2024
@Bush95 Bush95 deleted the wp_attributes branch January 22, 2024 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants