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

RichText: Add the RichText.Content component to be used in conjunction with RichText #6274

Merged
merged 4 commits into from
Apr 23, 2018

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Apr 19, 2018

related #6034

This PR adds a RichText.Content to clarify the usage of RichText values in the save function. Block authors don't need to know that the value is a react element and can be embedded. They just declare that it's a richtext value with a given format:

<RichText.Content>{ value }</RichText.Content>

Questions

Right now the API of RichText.Content doesn't match directly the API of RichText so we end up with something like this:

<RichText tagName="h2" value={ attributes.content ) /> // This is the edit function 

<h2><RichText.Content>{attributes.content}</RichText.Content></h2> // This is the save function

I was thinking that we could align both which means:

  • Adding tagName prop to RichText.Content
  • Use value prop instead of children
  • Support multiple (if multiple is not false, don't render any parent tag name)

Thoughts?

Notes

One of the ideas behind this component is that it will allow us to switch the default format at some point (tree or string) with a clear deprecation strategy.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with value prop in #5380 . One advantage is that it doesn't mislead one to think there's a wrapper element applied, which at a glance it might appear there is.

@@ -95,6 +100,12 @@ wp.blocks.registerBlockType( /* ... */, {
}
} );
},

save: function() {
return wp.element.createElement( 'h2, {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing single quote after h2.

@@ -876,7 +876,7 @@ RichText.defaultProps = {
format: 'element',
};

export default compose( [
const RichTextContainer = compose( [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just reassign RichText here? RichText = compose( [ ] )( RichText );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not possible because we use the inner RichText in unit tests.

@@ -112,4 +118,4 @@ The `RichText` component can be considered as a super-powered `textarea` element

Implementing this behavior as a component enables you as the block implementer to be much more granular about editable fields. Your block may not need `RichText` at all, or it may need many independent `RichText` elements, each operating on a subset of the overall block state.

Because `RichText` allows for nested nodes, you'll most often use it in conjunction with the `children` attribute source when extracting the value from saved content.
Because `RichText` allows for nested nodes, you'll most often use it in conjunction with the `children` attribute source when extracting the value from saved content. You'll also most likely use `RichText.Content` in the `save` function to output RichText values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"most likely" ? Uncertainty here would be confusing to a developer, particularly without explanation of if/when it should be applied.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is because the quote values are left alone? Not sure. Will be simpler after #6054.

@ellatrix
Copy link
Member

Support multiple (if multiple is not false, don't render any parent tag name)

What do you mean with this?


I lean towards the value prop as well.

  • Makes it very similar in use to RichText.
  • Perhaps we could also incorporate empty checking into it, so we return null instead of an empty <TagName> when value is empty. This could be very handy for e.g. <figcaption>.

@youknowriad
Copy link
Contributor Author

I was thinking:

<RichText.Content tagName="h2" value={ "blabla" } /> => <h2>blabla</h2>

<RichText.Content multiline value={ <p>a</p><p>b</p> } />(or multiline="p" to match the RichText) => <p>a</p><p>b</p>

<RichText.content value={"blabla"} /> => <p>blabla</p> (default TagName)

This way it matches RichText entirely

@ellatrix
Copy link
Member

Sounds great!

@youknowriad youknowriad force-pushed the try/rich-text-content branch from bb3f651 to 9865287 Compare April 20, 2018 09:21
@youknowriad
Copy link
Contributor Author

youknowriad commented Apr 20, 2018

Updated, I think it's nicer this way where we mimic the same API as RichText.

I was thinking of introducing a console.info message to encourage users to use RichText.Content what do you think? what should be the wording of the message?

@aduth
Copy link
Member

aduth commented Apr 20, 2018

I was thinking of introducing a console.info message to encourage users to use RichText.Content what do you think? what should be the wording of the message?

When would it be logged? There's a legitimate concern here that this is less convenient for developers in some way, as the previous approach was "simpler", albeit not always consistent with the new format options.

@youknowriad
Copy link
Contributor Author

When would it be logged?

I was thinking to always log it once when you load Gutenberg (RichText file) for some versions. I wonder how we can communicate about this better?

@@ -194,7 +194,7 @@ export const settings = {

<figure className={ align ? `align${ align }` : null }>
{ src && <video controls src={ src } /> }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && <RichText.Content tagName="figcaption" value={ caption } /> }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, but would it be good to incorporate the empty check in RichText.Content? This seems to be a common pattern. Even if the block registration has no such check, I don't think there are any cases where it makes sense to render an empty element in save context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on the fence here because I wonder if it's possible sometimes to render the container tag without its content ( like an empty list item or something). Let's keep it as is for now.

@ellatrix
Copy link
Member

There's a legitimate concern here that this is less convenient for developers in some way, as the previous approach was "simpler", albeit not always consistent with the new format options.

It seems we all agree we need to move away form storing the React objects, so this will be beneficial in any case, unless you'd prefer to handle this in the save function? It does feel strange to have non React objects as children though.

I'm also not sure if I agree that this is more complicated... It's more verbose, yes, but it's simple in the sense that it's very similar to what you use for the edit function now.

Copy link
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. +1 from me.

@youknowriad
Copy link
Contributor Author

After a second thought, I'm going to merge this as is. The warning should be introduced later when we settle on the format we want as a replacement for the default "element" format. That way we will ask people to only update their code once and not twice (Add RichText.Content then switch the format)

@youknowriad youknowriad merged commit 9213b9a into master Apr 23, 2018
@youknowriad youknowriad deleted the try/rich-text-content branch April 23, 2018 08:15
nuzzio pushed a commit to nuzzio/gutenberg that referenced this pull request Apr 25, 2018
…n with RichText (WordPress#6274)

* RichText: Add the RichText.Content component to be used in conjunction with RichText

* Support tagName and extra props in RichText.Content

* Use RichText.Content in quotes as well

* Fix unit tests
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.

3 participants