Raw
1 ---
2 sidebar_label: "JSON View"
3 ---
4
5 export const metadata = {
6 title: `JSON View - Admin Components`,
7 }
8
9 # {metadata.title}
10
11 <Note title="Archived" type="warning">
12
13 Medusa exports a `JsonViewSection` component from the `@medusajs/dashboard` package since [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). Use [the exported component](../../components/json-view-section/page.mdx) instead of building your own. This guide is kept for reference only.
14
15 </Note>
16
17 In this guide, you'll learn how to create a JSON view section that matches the Medusa Admin's design conventions.
18
19 Detail pages in the Medusa Admin show a JSON section to view the current page's details in JSON format.
20
21 ![Example of a JSON section in the admin](https://res.cloudinary.com/dza7lstvk/image/upload/v1728295129/Medusa%20Resources/json_dtbsgm.png)
22
23 To create a component that shows a JSON section in your customizations, create the file `src/admin/components/json-view-section.tsx` with the following content:
24
25 ```tsx title="src/admin/components/json-view-section.tsx"
26 import {
27 ArrowUpRightOnBox,
28 Check,
29 SquareTwoStack,
30 TriangleDownMini,
31 XMarkMini,
32 } from "@medusajs/icons"
33 import {
34 Badge,
35 Container,
36 Drawer,
37 Heading,
38 IconButton,
39 Kbd,
40 } from "@medusajs/ui"
41 import Primitive from "@uiw/react-json-view"
42 import { CSSProperties, MouseEvent, Suspense, useState } from "react"
43
44 type JsonViewSectionProps = {
45 data: object
46 title?: string
47 }
48
49 export const JsonViewSection = ({ data }: JsonViewSectionProps) => {
50 const numberOfKeys = Object.keys(data).length
51
52 return (
53 <Container className="flex items-center justify-between px-6 py-4">
54 <div className="flex items-center gap-x-4">
55 <Heading level="h2">JSON</Heading>
56 <Badge size="2xsmall" rounded="full">
57 {numberOfKeys} keys
58 </Badge>
59 </div>
60 <Drawer>
61 <Drawer.Trigger asChild>
62 <IconButton
63 size="small"
64 variant="transparent"
65 className="text-ui-fg-muted hover:text-ui-fg-subtle"
66 >
67 <ArrowUpRightOnBox />
68 </IconButton>
69 </Drawer.Trigger>
70 <Drawer.Content className="bg-ui-contrast-bg-base text-ui-code-fg-subtle !shadow-elevation-commandbar overflow-hidden border border-none max-md:inset-x-2 max-md:max-w-[calc(100%-16px)]">
71 <div className="bg-ui-code-bg-base flex items-center justify-between px-6 py-4">
72 <div className="flex items-center gap-x-4">
73 <Drawer.Title asChild>
74 <Heading className="text-ui-contrast-fg-primary">
75 <span className="text-ui-fg-subtle">
76 {numberOfKeys}
77 </span>
78 </Heading>
79 </Drawer.Title>
80 </div>
81 <div className="flex items-center gap-x-2">
82 <Kbd className="bg-ui-contrast-bg-subtle border-ui-contrast-border-base text-ui-contrast-fg-secondary">
83 esc
84 </Kbd>
85 <Drawer.Close asChild>
86 <IconButton
87 size="small"
88 variant="transparent"
89 className="text-ui-contrast-fg-secondary hover:text-ui-contrast-fg-primary hover:bg-ui-contrast-bg-base-hover active:bg-ui-contrast-bg-base-pressed focus-visible:bg-ui-contrast-bg-base-hover focus-visible:shadow-borders-interactive-with-active"
90 >
91 <XMarkMini />
92 </IconButton>
93 </Drawer.Close>
94 </div>
95 </div>
96 <Drawer.Body className="flex flex-1 flex-col overflow-hidden px-[5px] py-0 pb-[5px]">
97 <div className="bg-ui-contrast-bg-subtle flex-1 overflow-auto rounded-b-[4px] rounded-t-lg p-3">
98 <Suspense
99 fallback={<div className="flex size-full flex-col"></div>}
100 >
101 <Primitive
102 value={data}
103 displayDataTypes={false}
104 style={
105 {
106 "--w-rjv-font-family": "Roboto Mono, monospace",
107 "--w-rjv-line-color": "var(--contrast-border-base)",
108 "--w-rjv-curlybraces-color":
109 "var(--contrast-fg-secondary)",
110 "--w-rjv-brackets-color": "var(--contrast-fg-secondary)",
111 "--w-rjv-key-string": "var(--contrast-fg-primary)",
112 "--w-rjv-info-color": "var(--contrast-fg-secondary)",
113 "--w-rjv-type-string-color": "var(--tag-green-icon)",
114 "--w-rjv-quotes-string-color": "var(--tag-green-icon)",
115 "--w-rjv-type-boolean-color": "var(--tag-orange-icon)",
116 "--w-rjv-type-int-color": "var(--tag-orange-icon)",
117 "--w-rjv-type-float-color": "var(--tag-orange-icon)",
118 "--w-rjv-type-bigint-color": "var(--tag-orange-icon)",
119 "--w-rjv-key-number": "var(--contrast-fg-secondary)",
120 "--w-rjv-arrow-color": "var(--contrast-fg-secondary)",
121 "--w-rjv-copied-color": "var(--contrast-fg-secondary)",
122 "--w-rjv-copied-success-color":
123 "var(--contrast-fg-primary)",
124 "--w-rjv-colon-color": "var(--contrast-fg-primary)",
125 "--w-rjv-ellipsis-color": "var(--contrast-fg-secondary)",
126 } as CSSProperties
127 }
128 collapsed={1}
129 >
130 <Primitive.Quote render={() => <span />} />
131 <Primitive.Null
132 render={() => (
133 <span className="text-ui-tag-red-icon">null</span>
134 )}
135 />
136 <Primitive.Undefined
137 render={() => (
138 <span className="text-ui-tag-blue-icon">undefined</span>
139 )}
140 />
141 <Primitive.CountInfo
142 render={(_props, { value }) => {
143 return (
144 <span className="text-ui-contrast-fg-secondary ml-2">
145 {Object.keys(value as object).length} items
146 </span>
147 )
148 }}
149 />
150 <Primitive.Arrow>
151 <TriangleDownMini className="text-ui-contrast-fg-secondary -ml-[0.5px]" />
152 </Primitive.Arrow>
153 <Primitive.Colon>
154 <span className="mr-1">:</span>
155 </Primitive.Colon>
156 <Primitive.Copied
157 render={({ style }, { value }) => {
158 return <Copied style={style} value={value} />
159 }}
160 />
161 </Primitive>
162 </Suspense>
163 </div>
164 </Drawer.Body>
165 </Drawer.Content>
166 </Drawer>
167 </Container>
168 )
169 }
170
171 type CopiedProps = {
172 style?: CSSProperties
173 value: object | undefined
174 }
175
176 const Copied = ({ style, value }: CopiedProps) => {
177 const [copied, setCopied] = useState(false)
178
179 const handler = (e: MouseEvent<HTMLSpanElement>) => {
180 e.stopPropagation()
181 setCopied(true)
182
183 if (typeof value === "string") {
184 navigator.clipboard.writeText(value)
185 } else {
186 const json = JSON.stringify(value, null, 2)
187 navigator.clipboard.writeText(json)
188 }
189
190 setTimeout(() => {
191 setCopied(false)
192 }, 2000)
193 }
194
195 const styl = { whiteSpace: "nowrap", width: "20px" }
196
197 if (copied) {
198 return (
199 <span style={{ ...style, ...styl }}>
200 <Check className="text-ui-contrast-fg-primary" />
201 </span>
202 )
203 }
204
205 return (
206 <span style={{ ...style, ...styl }} onClick={handler}>
207 <SquareTwoStack className="text-ui-contrast-fg-secondary" />
208 </span>
209 )
210 }
211 ```
212
213 The `JsonViewSection` component shows a section with the "JSON" title and a button to show the data as JSON in a drawer or side window.
214
215 The `JsonViewSection` accepts a `data` prop, which is the data to show as a JSON object in the drawer.
216
217 ---
218
219 ## Example
220
221 Use the `JsonViewSection` component in any widget or UI route.
222
223 For example, create the widget `src/admin/widgets/product-widget.tsx` with the following content:
224
225 ```tsx title="src/admin/widgets/product-widget.tsx"
226 import { defineWidgetConfig } from "@medusajs/admin-sdk"
227 import { JsonViewSection } from "../components/json-view-section"
228
229 const ProductWidget = () => {
230 return <JsonViewSection data={{
231 name: "John",
232 }} />
233 }
234
235 export const config = defineWidgetConfig({
236 zone: "product.details",
237 })
238
239 export default ProductWidget
240 ```
241
242 This shows the JSON section at the top of the product page, passing it the object `{ name: "John" }`.