-
Notifications
You must be signed in to change notification settings - Fork 30
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
Properly handle custom block types with children #54
Conversation
Thanks for the PR! Some thoughts I'd like to discuss - /cc @portabletext/javascript: Currently, all blocks produced by the Sanity portable text editor use For instance, a portable text block that does not allow any annotations on its text nodes, only allows text nodes as children, and only allows the style of the node to be one of the heading levels (h1 through h6) could be thought of (and typed as) In these cases, we'd actually want to have the With this in mind, I was hoping to stay away from explicit While this approach solves not being able to override rendering for custom types, it still maintains the behavior where block-like types are automatically rendered as portable text blocks. The question I have is whether or not this is developer friendly behavior, or if it would be better to require the user to specify all their custom block types, if any. Either through us exposing the default block rendered as (say) import {PortableText, DefaultBlock} from '@portabletext/react'
<PortableText
// {...}
components: {
types: {
myCustomBlock: DefaultBlock
}
}
/> or import {PortableText} from '@portabletext/react'
const blockTypes = ['block', 'myCustomBlock']
<PortableText
// {...}
// prop name up for discussion, eg `blockTypes`, `renderAsBlocks` or similar
blockTypes={blockTypes}
/> |
@rexxars Thank you for the background. That makes sense then, that the existing check is how it is. I do like the idea of prioritizing the You can close this PR, though I would appreciate it if you pointed me towards an issue you'll track this with (#29, maybe) so I can follow it. |
@rexxars I'd be happy with the approach in used in 3a8a41b Since having a default block renderer is already established, I think having it "just work" is the way forward. Ie, I prefer this myself: import {PortableText, DefaultBlock} from '@portabletext/react' <PortableText
// {...}
components: {
types: {
myCustomBlock: DefaultBlock
}
}
/> |
Sounds good to me @snorrees - feel free to build on this branch: https://github.com/portabletext/react-portabletext/tree/feat/allow-override-block-like |
#29 most clearly defines the issue that this PR fixes (which I found only after doing an identical investigation with identical findings myself). In short, the logic to pass portable text blocks to the appropriate renderer uses a check,
isPortableTextBlock
, that is too lenient in its definition of a block for the context it's used in, and any custom block (e.g.,_type: "testimonial"
) that contains achildren
value that looks like inline blocks or is an empty array will get treated as if they were of_type === "block"
.Definitely:
Fixes #29
Pretty confidently:
Fixes #37
Probably:
Fixes #38