Add renderText and leafPosition#5850
Conversation
🦋 Changeset detectedLatest commit: db6db77 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
I wonder if it would be better to put this information in a separate property of The property could be named to make it clearer that it refers to the position of the leaf within a decoration, rather than its position within the parent element, for example. |
|
Good point @12joan, I've refactored to use |
| 'data-slate-node': 'text', | ||
| ref: callbackRef, |
There was a problem hiding this comment.
We probably don't want to apply data-slate-node="text" to multiple different layers of the DOM hierarchy, and we certainly don't want to apply the ref twice. I would probably omit attributes from renderText, or possibly leave it as an empty object if we want to preserve the ability to add a distinct ref later without it being a breaking change. I can't think of any reason why that would be necessary, though.
There was a problem hiding this comment.
I took those attributes from the default behavior for backward compatibility.
Unless I'm missing something, there can't be nested renderText.
There was a problem hiding this comment.
textContent also has those attributes, and is passed as children to renderText.
const textContent = (
<span data-slate-node="text" ref={callbackRef}>
{children}
</span>
)
if (renderText) {
return renderText({
text,
children: textContent,
attributes: {
'data-slate-node': 'text',
ref: callbackRef,
},
})
}If renderText is implemented as ({ attributes, children }) => <span {...attributes}>{children}</span>, then the resulting JSX is:
<span data-slate-node="text" ref={refCallback}>
<span data-slate-node="text" ref={refCallback}>
{children}
</span>
</span>There was a problem hiding this comment.
Good catch, I meant to use children instead of textContent there. Updating...
There was a problem hiding this comment.
Got it. Since that's your intent, perhaps we could use the same pattern as renderElement to provide a default renderText that just renders a span with those attributes? That would eliminate the need for an if statement here.
Neat solution with |
`renderText` was introduced in ianstormtaylor#5850, but the prop types didn't get exported with it.
`renderText` was introduced in ianstormtaylor#5850, but the prop types didn't get exported with it.
Description
Add optional
positionproperty to leaves returned byText.decorations()and introduce newrenderTextprop for customizing text node rendering. These two features provide complementary solutions for handling text node customization, particularly when dealing with decorated text that gets split into multiple leaves.Example
Before:
After:
Context
When working with decorated text nodes, it's often necessary to know the exact position of each leaf within the original text. A common use case is rendering an element exactly once after a text node, even when that text node has been split into multiple leaves by decorations.
This PR provides two different approaches to solve this problem:
leafPositionproperty inrenderLeaf:renderTextprop to handle the entire text node:leafPositionprop is only added when a text node is actually split by decorations.renderTextprop offers an alternative approach by letting you customize the container of all leaves within a text node.