Add JSON support to CodeBlock#1137
Conversation
✅ Deploy Preview for commercelayer-app-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
Adds support for passing a JSON object as CodeBlock content so it can be rendered/copyable as a formatted JSON string, and documents the usage in Storybook.
Changes:
- Extend
CodeBlockchildrenprop to accept a JSON object and stringify it with 2-space indentation. - Preserve whitespace when rendering JSON so indentation is visible.
- Add a new Storybook story demonstrating JSON usage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/docs/src/stories/atoms/CodeBlock.stories.tsx | Adds a Storybook example showing how to render an object as JSON in CodeBlock. |
| packages/app-elements/src/ui/atoms/CodeBlock.tsx | Updates CodeBlock to stringify non-string children, preserve whitespace for indentation, and copy the formatted content. |
Comments suppressed due to low confidence (2)
packages/app-elements/src/ui/atoms/CodeBlock.tsx:49
JSON.stringify(children, null, 2)can throw (e.g. circular references,BigIntvalues) which would crash rendering. Since the prop type currently doesn't guarantee JSON-serializable content, consider guarding with a try/catch (and falling back to something likeString(children)or a clear placeholder) to avoid bringing down the whole UI.
const contentString =
typeof children === "string"
? children
: JSON.stringify(children, null, 2)
packages/app-elements/src/ui/atoms/CodeBlock.tsx:67
- New JSON-object rendering path (
contentString+JSON.stringify) isn’t covered by unit tests. SinceCodeBlockalready has a test file, add a test asserting object children are formatted as expected (and that indentation is preserved, e.g. viawhitespace-pre-wrapclass) to prevent regressions.
const contentString =
typeof children === "string"
? children
: JSON.stringify(children, null, 2)
return (
<InputWrapper {...rest} label={label} hint={hint}>
<div className="flex group w-full rounded bg-gray-50 in-[.overlay-container]:bg-gray-200">
<div
className={cn(
"flex flex-col w-full px-4 py-2.5 text-teal text-sm font-mono marker:font-bold border-none break-all",
typeof children !== "string" ? "whitespace-pre-wrap" : "",
)}
data-testid="codeblock-content"
>
{isLoading === true
? ""
: contentString.split("\n").map((line, idx) => (
// biome-ignore lint/suspicious/noArrayIndexKey: Using index as key is acceptable here since the content is static and does not change and the lines are not reordered or filtered.
<span key={idx}>
{hideValue ? randomHiddenValue() : line}
</span>
))}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
643e4de to
0a05b04
Compare
commit: |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
packages/app-elements/src/ui/atoms/CodeBlock.tsx:69
- New behavior adds support for JSON-object children (stringification + whitespace handling), but
CodeBlock.test.tsxonly covers string/secret toggling. Add a unit test asserting JSON-object rendering uses 2-space indentation (and that copy-to-clipboard receives the formatted string) to prevent regressions.
const contentString =
typeof children === "string"
? children
: JSON.stringify(children, null, 2)
return (
<InputWrapper {...rest} label={label} hint={hint}>
<div className="flex group w-full rounded bg-gray-50 in-[.overlay-container]:bg-gray-200">
<div
className={cn(
"flex flex-col w-full px-4 py-2.5 text-teal text-sm font-mono marker:font-bold border-none break-all",
typeof children !== "string" ? "whitespace-pre-wrap" : "",
)}
data-testid="codeblock-content"
>
{isLoading === true
? ""
: contentString.split("\n").map((line, idx) => (
// biome-ignore lint/suspicious/noArrayIndexKey: Using index as key is acceptable here since the content is static and does not change and the lines are not reordered or filtered.
<span key={idx}>
{hideValue ? randomHiddenValue() : line}
</span>
))}
</div>
Closes #1136
Related commercelayer/issues-app/issues/430
What I did
Added JSON support to
CodeBlock.Checklist