-
Notifications
You must be signed in to change notification settings - Fork 20
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
fix: use stricter type definition for children of <Code> #1619
base: main
Are you sure you want to change the base?
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 |
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.
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). |
Currently, the
<Code>
component accepts any validReactNode
, which means this is works compile time:But breaks runtime, as the Code component expects
children
to 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
children
type to something that can safely be stringified, and also considering thatchildren
can be iterable (e.g.<Code>The number is {42}</Code>
will makechildren
iterable).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
language
was 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: