diff --git a/packages/app-elements/src/ui/atoms/CodeBlock.tsx b/packages/app-elements/src/ui/atoms/CodeBlock.tsx index 7e08aeae..fcd2d1f2 100644 --- a/packages/app-elements/src/ui/atoms/CodeBlock.tsx +++ b/packages/app-elements/src/ui/atoms/CodeBlock.tsx @@ -1,5 +1,6 @@ import cn from "classnames" import { useEffect, useState } from "react" +import type { JsonObject } from "type-fest" import { CopyToClipboard } from "#ui/atoms/CopyToClipboard" import { Icon } from "#ui/atoms/Icon" import { withSkeletonTemplate } from "#ui/atoms/SkeletonTemplate" @@ -19,9 +20,10 @@ export type CodeBlockProps = Pick & { showSecretAction?: boolean /** * Content to be rendered. - * It only accepts a string and will respect new lines when passing a template literal (backticks). + * Accepts a string (respects new lines with template literals) or a JSON object. + * JSON objects are formatted with 2-space indentation. */ - children: string + children: string | JsonObject } export const CodeBlock = withSkeletonTemplate( @@ -41,16 +43,24 @@ export const CodeBlock = withSkeletonTemplate( setHideValue(showSecretAction) }, [showSecretAction]) + const contentString = + typeof children === "string" + ? children + : JSON.stringify(children, null, 2) + return (
{isLoading === true ? "" - : children.split("\n").map((line, idx) => ( + : 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. {hideValue ? randomHiddenValue() : line} @@ -75,7 +85,10 @@ export const CodeBlock = withSkeletonTemplate( )} {showCopyAction && ( - + )}
) : null} diff --git a/packages/docs/src/stories/atoms/CodeBlock.stories.tsx b/packages/docs/src/stories/atoms/CodeBlock.stories.tsx index f707c6cb..631395d0 100644 --- a/packages/docs/src/stories/atoms/CodeBlock.stories.tsx +++ b/packages/docs/src/stories/atoms/CodeBlock.stories.tsx @@ -56,6 +56,21 @@ Secret.args = { showSecretAction: true, } +/** + * This component can be used to show JSON content. + */ +export const Json: StoryFn = () => { + return ( + + {{ + apiKey: "elyFpGvqXsOSsvEko6ues2Ua4No1_HxaKH_0rUaFuYiX9", + organization: "demo-store", + roles: ["admin", "developer"], + }} + + ) +} + /** * In this example we are using the `children` prop to render a code snippet with multi-line content. * Special characters like `\` need to be escaped with `\\` to be rendered correctly.