fix: use stricter type definition for children of <Code>#1619
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
Thinking some more about this, I'm wondering if we, for now, should instead restore the previous behavior of not stringifying if |
|
@bjoerge it seems like the stringify is an oversight: <LazyRefractor language={language} value={String(children)} />The previous behavior should be restored by just passing children as is? <LazyRefractor language={language} value={children} /> |
|
And of course stringify here: {language && registered && <Refractor inline language={language} value={String(value)} />} |
|
Yeah, that should restore the earlier behavior |
EoinFalconer
left a comment
There was a problem hiding this comment.
Much better testing. I don't know if we want to add tests for the stringifyChildren function?
BREAKING CHANGE: The Code component no longer support JSX as children.
4619cef to
9ac9cc3
Compare
|
Now that #1620 landed this is less of a pressing issue, but still think we should consider this improvement as it removes an inconsistency leading to confusing behavior. I start out by writing this: <Code>
<b>Some code</b>
</Code>This outputs Some code in bold, perhaps as expected. However, if I then add a language: <Code language="typescript">
<b>Some code</b>
</Code>This will now render To me, this feels like very much like a bug not a feature, and I think the right thing is to always stringify children regardless. I don't see the case for using this component to render anything else than source code, but maybe I'm missing something. Note: this would now be a breaking change, so updated the commit to reflect that. |
|
I'm hesitant to do a v3 of |
|
That's fair. I guess you can also argue that it's a bugfix :) I'll adjust the implementation and commit message accordingly (I'm giving this lower priority, so probably won't happen before next week some time). |
|
@bjoerge would you like to resurrect this PR? |
Currently, the
<Code>component accepts any validReactNode, which means this is works compile time:But breaks runtime, as the Code component expects
childrento be stringifiable, coerces it to a string runtime and thus render:This lead to error messages in the studio showing
[object Object]instead of the error message, see for example the bug report over at sanity-io/sanity#8712This fixes the issue by changing the
childrentype to something that can safely be stringified, and also considering thatchildrencan be iterable (e.g.<Code>The number is {42}</Code>will makechildreniterable).Note: incidentally, the above snipped actually used to work prior to lazy loading refractor (783942a), because before then, we'd only stringify if refractor was loaded and
languagewas passed:ui/src/primitives/code/code.tsx
Lines 24 to 26 in 7a0231b
This caused inconsistent behavior, so we should stick with always stringifying children.
After this PR, the above code will give a compile error: