-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Blocks: Support implicit attributes and test them for useColors
.
#20308
Conversation
@mtias, you asked for something like this a while back. |
Another idea is to make these attributes have a distinctive prefix like |
@@ -18,18 +18,6 @@ | |||
"placeholder": { | |||
"type": "string" | |||
}, | |||
"textColor": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is lovely to see :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
@@ -245,6 +249,11 @@ export function registerBlockType( name, settings ) { | |||
return; | |||
} | |||
|
|||
settings.attributes = addImplicitAttributes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call these supportedAttributes
maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that.
I wanted to share a few thoughts for the full picture. The fun part is that we were blocking using Another interesting aspect is that there are a few 3rd party plugins that provide some abstraction over Besides, If we decide that we don’t care about Another observation is that all the |
This implementation could be replicated in the server, but I think I'd prefer a less magical solution like using the prefixes I suggested. What do you think of that? |
@gziolo That's a great observation! Below are some of my thoughts (don't consider them blockers by any means) Types of AttributesUsing a pre-build mechanism like Aesthetics vs function. "Font Size" vs something like "Gallery preview type" Easily registering attributesThe spirit of @epiqueras 's PR (as I see it), hopes to make it easier for blocks (core to custom) to use attributes without having to manually register every single attribute. So things like Automatically rendered controls?(Extending things further) Recently caught up with @mtias! An idea that came about was the (automatic) rendering of controls based on "known"/associated attributes as part of the system. So by using something like Anywho! Those are my thoughts 😊 Edit: Some of the thoughts above are touched upon here: |
Since I can only come up with examples of attributes that we would like to hide of the former kind, and since those examples are now planned to be covered by global style hooks. I'll close this PR. |
Description
Gutenberg provides tools for block authors to quickly implement Common Block Functionality.
An example of this is
useColors
for arbitrary color picking and application.Most of these tools persist data in the form of new block attributes, which forces developers to add to their block definitions manually.
This PR explores the idea of implicit attributes and leverages them inside
useColors
to dynamically resolve the new block attribute definitions during block registration.It does this by monkey patching the React dispatcher to be able to render functional components with hooks to strings (this might also be useful for our block serialization implementation, which currently does not support hooks in block
save
functions). It then injects an internal hook,useImplicitAttributes
, for common block tools or third party custom hooks to register additional attributes during a block's registration. Then it sets everything back to normal before continuing as usual.It's worth noting that errors are swallowed to avoid potential breakage with blocks that do strange things or depend on context. We can continue to refine the approach as we see run into these edge cases, but blocks can always register attributes explicitly to be safe.
How has this been tested?
It was verified that the paragraph block is still being registered with all color attributes even though they are no longer in its
block.json
file.Types of Changes
New Feature: A new API for implicit attributes allows for custom hooks to automatically add attributes to a block's type.
Checklist: