-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Editor: Introduce name
prop for RichText to handle changes automatically
#6705
Conversation
4a24841
to
4d3dc90
Compare
@@ -10,6 +10,8 @@ import { createContext, createHigherOrderComponent } from '@wordpress/element'; | |||
|
|||
const { Consumer, Provider } = createContext( { | |||
name: '', | |||
attributes: {}, | |||
setAttribute: noop, |
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.
We always used the setAttributes "concept", maybe here we can expose setAttributes, so we are not exposing a new concept (setAttribute).
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 don't have any preference here, I can update to setAttributes
if it makes things easier.
@@ -93,11 +97,8 @@ wp.blocks.registerBlockType( /* ... */, { | |||
edit: function( props ) { | |||
return wp.element.createElement( wp.editor.RichText, { | |||
tagName: 'h2', | |||
className: props.className, |
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.
Maybe we can leave this sample in the docs. As we still support using value + onChange and it may be useful in some cases.
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.
It's hard to tell, maybe we should update only Gutenberg handbook to contain the shorter version.
onChange() { | ||
const { name, onChange, setAttribute } = this.props; |
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 feel our RichText component is getting bigger with more props. Maybe we can divide RichText into a more generic low-level version that does not use context. And a higher level version that uses context. When passing name the higher level version would just pass value + onChange to the low-level version. I feel this might simplify things and make it easier to add more high-level abstractions in the future.
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, that's a good observation. It's definitely something we should explore soon to make sure RichText
doesn't reach 1k lines :)
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.
@jorgefilipecosta I agree with you, it's really hard to make sure there's no bugs with how big it's getting. A better idea in my opinion would be to compose components and add features that way.
I like this new name prop, and I think it is a good step to make block creation simpler 👍 |
|
What about How far do we take it? If a block implementer comes to expect this to be automated from I mean, I like the idea, and it could encourage developers to use the abstracted component forms rather than the DOM primitives (e.g. I think also @mtias mentioned this at one point in the past as being a good thing to offer. |
See my note in the description:
As usual in such case, my answer is - yes for all for consistency. Technically it is possible, but it would require forwarding the context. There is also an architectural decision to be made, because by design all components in
|
Yes, this would be key and one of the reasons we didn't push for it earlier — once we go with the abstraction it has to be the right one, as we are bound to support it. I agree with going with @nb had expressed wanting something like this when building blocks, so looping him in. The UI control components are trickier in that we could be generating excessive noise by wrapping them with others, and having to explain when you use which. It doesn't sound like a great situation to be in. |
Just noting that a very similar idea was also proposed by @zackify in gutenblock plugin but using another wrapper component: import { RichText } from 'gutenblock-controls';
const Edit = () => (
<div>
<RichText name="description" />
</div>
); I will reach out to him to collect feedback on this one. |
Another reading comprehension fail on my part 😅 I think I've mentioned on at least one earlier occasion, but I'd love if we could migrate toward an exclusively portal-based slot/fill, since I believe it should only be the one based in DOM that is problematic. Some prior art for context forwarding, though requires explicitly targeting values: https://github.com/Automattic/wp-calypso/blob/44ca12f43a99dde4e65a00bd0febf5d143fad323/client/components/root-child/index.jsx#L46-L50 |
Closing it for now as it got outdated. We should definitely get back to this idea after WordPress 5.0 is out. |
Description
Similar to #6419 where we introduced an automatic handling of focus for
RichText
component.This PR proposes to make
value
andonChange
props fromRichText
optional. When omitting those props you would have to usename
instead. It would have to match the name of one of the block's attributes and it would enable the default handling ofvalue
andonChange
props controlled by the editor.TODO
When we agree on the proposed improvement, we can also do:
PlainText
and update all usage in core blocks.RichText
in a way that Paragraph block does.In theory, it could be sprinkled on all controls, but we would have to fix the issue with the context not being properly applied to components rendered as children of fills. This applies to
BlockControls
,InspectorControls
andAdvancedInspectorControls
.How has this been tested?
Manually.
I made sure that editing
RichText
still works as before when it's handled automatically withname
props (Paragraph block) or withvalue
andonChange
for all other blocks.Types of changes
Refactoring - improvement for developers convenience.
Checklist: