1 ---
2 generate_toc: true
3 ---
4
5 import { ComponentExample } from "@/components/ComponentExample"
6 import { ComponentReference } from "@/components/ComponentReference"
7
8 export const metadata = {
9 title: `Code Block`,
10 }
11
12 # {metadata.title}
13
14 A component for displaying code snippets with syntax highlighting and copy functionality.
15
16 In this guide, you'll learn how to use the Code Block component.
17
18 <ComponentExample name="code-block-demo" />
19
20 ## Usage
21
22 ```tsx
23 import { CodeBlock } from "@medusajs/ui"
24 ```
25
26 ```tsx
27 <CodeBlock
28 snippets={[
29 {
30 language: "tsx",
31 label: "Label",
32 code: "import { useProduct } from \"medusa-react\"",
33 },
34 ]}
35 >
36 <CodeBlock.Header />
37 <CodeBlock.Body />
38 </CodeBlock>
39 ```
40
41 ---
42
43 ## API Reference
44
45 <ComponentReference mainComponent="CodeBlock" componentsToShow={[
46 "CodeBlock",
47 "CodeBlock.Header",
48 "CodeBlock.Header.Meta",
49 "CodeBlock.Body"
50 ]} />
51
52 ---
53
54 ## Usage Outside Medusa Admin
55
56 If you're using the `CodeBlock` component in a project other than the Medusa Admin, make sure to include the `TooltipProvider` somewhere up in your component tree, as the `CodeBlock.Header` component uses a [Tooltip](../tooltip/page.mdx#usage-outside-medusa-admin):
57
58 ```tsx
59 <TooltipProvider>
60 <CodeBlock
61 snippets={[
62 {
63 language: "tsx",
64 label: "Label",
65 code: "import { useProduct } from \"medusa-react\"",
66 },
67 ]}
68 >
69 <CodeBlock.Header />
70 <CodeBlock.Body />
71 </CodeBlock>
72 </TooltipProvider>
73 ```
74
75 ---
76
77 ## Examples
78
79 ### Single Snippet
80
81 If you want to only show a code sample for one language or API, you can choose to hide the snippet labels:
82
83 <ComponentExample name="code-block-single" />
84
85 ### No Header
86
87 You can also omit the header entirely:
88
89 <ComponentExample name="code-block-no-header" />
90
91 ### No Line Numbers
92
93 <ComponentExample name="code-block-no-lines" />
94
95 ### No Copy Button
96
97 <ComponentExample name="code-block-no-copy" hideFeedback />