|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { Children, useCallback, useEffect, useMemo, useRef } from "react" |
|
4
|
import { Badge } from "@/components/Badge" |
|
5
|
import { CodeBlockProps, CodeBlockStyle } from "@/components/CodeBlock" |
|
6
|
import { useColorMode } from "@/providers/ColorMode" |
|
7
|
import clsx from "clsx" |
|
8
|
import { CodeBlockActions, CodeBlockActionsProps } from "../CodeBlock/Actions" |
|
9
|
import { CodeBlockHeaderWrapper } from "../CodeBlock/Header/Wrapper" |
|
10
|
import { BaseTabType, useTabs } from "../../hooks/use-tabs" |
|
11
|
|
|
12
|
type CodeTab = BaseTabType & { |
|
13
|
codeProps?: CodeBlockProps |
|
14
|
codeBlock: React.ReactNode |
|
15
|
children?: React.ReactNode |
|
16
|
} |
|
17
|
|
|
18
|
type CodeTabProps = { |
|
19
|
children: React.ReactNode |
|
20
|
className?: string |
|
21
|
group?: string |
|
22
|
blockStyle?: CodeBlockStyle |
|
23
|
} |
|
24
|
|
|
25
|
export const CodeTabs = ({ |
|
26
|
children, |
|
27
|
className, |
|
28
|
group = "client", |
|
29
|
blockStyle = "loud", |
|
30
|
}: CodeTabProps) => { |
|
31
|
const { colorMode } = useColorMode() |
|
32
|
|
|
33
|
const isCodeBlock = ( |
|
34
|
node: React.ReactNode |
|
35
|
): node is |
|
36
|
| React.ReactElement<unknown, string | React.JSXElementConstructor<any>> |
|
37
|
| React.ReactPortal => { |
|
38
|
if (!React.isValidElement(node)) { |
|
39
|
return false |
|
40
|
} |
|
41
|
|
|
42
|
if (node.type === "pre") { |
|
43
|
return true |
|
44
|
} |
|
45
|
|
|
46
|
const typedProps = node.props as Record<string, unknown> |
|
47
|
|
|
48
|
return "source" in typedProps |
|
49
|
} |
|
50
|
|
|
51
|
const getCodeBlockProps = ( |
|
52
|
codeBlock: React.ReactElement< |
|
53
|
unknown, |
|
54
|
string | React.JSXElementConstructor<any> |
|
55
|
> |
|
56
|
): CodeBlockProps | undefined => { |
|
57
|
if (typeof codeBlock.props !== "object" || !codeBlock.props) { |
|
58
|
return undefined |
|
59
|
} |
|
60
|
|
|
61
|
if ("source" in codeBlock.props) { |
|
62
|
return codeBlock.props as CodeBlockProps |
|
63
|
} |
|
64
|
|
|
65
|
if ("children" in codeBlock.props) { |
|
66
|
if ( |
|
67
|
typeof codeBlock.props.children === "object" && |
|
68
|
codeBlock.props.children |
|
69
|
) { |
|
70
|
return getCodeBlockProps( |
|
71
|
codeBlock.props.children as React.ReactElement< |
|
72
|
unknown, |
|
73
|
string | React.JSXElementConstructor<any> |
|
74
|
> |
|
75
|
) |
|
76
|
} else if (typeof codeBlock.props.children === "string") { |
|
77
|
const lang = "lang" in codeBlock.props ? codeBlock.props.lang : "ts" |
|
78
|
return { |
|
79
|
...codeBlock.props, |
|
80
|
source: codeBlock.props.children, |
|
81
|
className: |
|
82
|
"className" in codeBlock.props |
|
83
|
? codeBlock.props.className |
|
84
|
: `language-${lang}`, |
|
85
|
} as CodeBlockProps |
|
86
|
} |
|
87
|
} |
|
88
|
|
|
89
|
return undefined |
|
90
|
} |
|
91
|
|
|
92
|
const tabs: CodeTab[] = useMemo(() => { |
|
93
|
const tempTabs: CodeTab[] = [] |
|
94
|
Children.forEach(children, (child) => { |
|
95
|
if (!React.isValidElement(child)) { |
|
96
|
return |
|
97
|
} |
|
98
|
const typedChildProps = child.props as CodeTab |
|
99
|
if ( |
|
100
|
!typedChildProps.label || |
|
101
|
!typedChildProps.value || |
|
102
|
!React.isValidElement(typedChildProps.children) |
|
103
|
) { |
|
104
|
return |
|
105
|
} |
|
106
|
|
|
107
|
const codeBlock: React.ReactNode = isCodeBlock(typedChildProps.children) |
|
108
|
? typedChildProps.children |
|
109
|
: undefined |
|
110
|
|
|
111
|
if (!codeBlock) { |
|
112
|
return |
|
113
|
} |
|
114
|
|
|
115
|
let codeBlockProps = codeBlock.props as CodeBlockProps |
|
116
|
const showBadge = !codeBlockProps.title |
|
117
|
const originalBadgeLabel = codeBlockProps.badgeLabel |
|
118
|
const parsedCodeBlockProps = getCodeBlockProps(codeBlock) || { |
|
119
|
source: "", |
|
120
|
} |
|
121
|
|
|
122
|
const commonProps = { |
|
123
|
badgeLabel: showBadge ? undefined : originalBadgeLabel, |
|
124
|
hasTabs: true, |
|
125
|
className: clsx("!my-0", parsedCodeBlockProps.className), |
|
126
|
} |
|
127
|
|
|
128
|
if ( |
|
129
|
typeof codeBlock.type !== "string" && |
|
130
|
(("name" in codeBlock.type && codeBlock.type.name === "CodeBlock") || |
|
131
|
"source" in codeBlockProps) |
|
132
|
) { |
|
133
|
codeBlockProps = { |
|
134
|
...codeBlockProps, |
|
135
|
...commonProps, |
|
136
|
} |
|
137
|
} |
|
138
|
|
|
139
|
const modifiedProps: CodeBlockProps = { |
|
140
|
...parsedCodeBlockProps, |
|
141
|
...commonProps, |
|
142
|
} |
|
143
|
|
|
144
|
tempTabs.push({ |
|
145
|
label: typedChildProps.label, |
|
146
|
value: typedChildProps.value, |
|
147
|
codeProps: { |
|
148
|
...modifiedProps, |
|
149
|
badgeLabel: !showBadge ? undefined : originalBadgeLabel, |
|
150
|
}, |
|
151
|
codeBlock: { |
|
152
|
...codeBlock, |
|
153
|
props: { |
|
154
|
...codeBlockProps, |
|
155
|
children: { |
|
156
|
...(typeof codeBlockProps.children === "object" |
|
157
|
? codeBlockProps.children |
|
158
|
: {}), |
|
159
|
props: modifiedProps, |
|
160
|
}, |
|
161
|
}, |
|
162
|
}, |
|
163
|
}) |
|
164
|
}) |
|
165
|
|
|
166
|
return tempTabs |
|
167
|
}, [children]) |
|
168
|
|
|
169
|
const { selectedTab, changeSelectedTab } = useTabs<CodeTab>({ |
|
170
|
tabs, |
|
171
|
group, |
|
172
|
}) |
|
173
|
|
|
174
|
const tabRefs = useRef<(HTMLButtonElement | null)[]>([]) |
|
175
|
const codeTabSelectorRef = useRef<HTMLSpanElement | null>(null) |
|
176
|
const codeTabsWrapperRef = useRef<HTMLDivElement | null>(null) |
|
177
|
|
|
178
|
const bgColor = useMemo( |
|
179
|
() => |
|
180
|
clsx( |
|
181
|
blockStyle === "loud" && "bg-medusa-contrast-bg-base", |
|
182
|
blockStyle === "subtle" && [ |
|
183
|
colorMode === "light" && "bg-medusa-bg-component", |
|
184
|
colorMode === "dark" && "bg-medusa-code-bg-header", |
|
185
|
] |
|
186
|
), |
|
187
|
[blockStyle, colorMode] |
|
188
|
) |
|
189
|
|
|
190
|
const boxShadow = useMemo( |
|
191
|
() => |
|
192
|
clsx( |
|
193
|
blockStyle === "loud" && |
|
194
|
"shadow-elevation-code-block dark:shadow-elevation-code-block-dark", |
|
195
|
blockStyle === "subtle" && "shadow-none" |
|
196
|
), |
|
197
|
[blockStyle] |
|
198
|
) |
|
199
|
|
|
200
|
const changeTabSelectorCoordinates = useCallback( |
|
201
|
(selectedTabElm: HTMLElement) => { |
|
202
|
if (!codeTabSelectorRef?.current || !codeTabsWrapperRef?.current) { |
|
203
|
return |
|
204
|
} |
|
205
|
const selectedTabsCoordinates = selectedTabElm.getBoundingClientRect() |
|
206
|
const tabsWrapperCoordinates = |
|
207
|
codeTabsWrapperRef.current.getBoundingClientRect() |
|
208
|
codeTabSelectorRef.current.style.left = `${ |
|
209
|
selectedTabsCoordinates.left - tabsWrapperCoordinates.left |
|
210
|
}px` |
|
211
|
codeTabSelectorRef.current.style.width = `${selectedTabsCoordinates.width}px` |
|
212
|
if (blockStyle !== "loud") { |
|
213
|
codeTabSelectorRef.current.style.height = `${selectedTabsCoordinates.height}px` |
|
214
|
} |
|
215
|
}, |
|
216
|
[blockStyle] |
|
217
|
) |
|
218
|
|
|
219
|
useEffect(() => { |
|
220
|
if (codeTabSelectorRef?.current && tabRefs.current.length) { |
|
221
|
const selectedTabElm = tabRefs.current.find( |
|
222
|
(tab) => tab?.getAttribute("aria-selected") === "true" |
|
223
|
) |
|
224
|
if (selectedTabElm) { |
|
225
|
changeTabSelectorCoordinates( |
|
226
|
selectedTabElm.parentElement || selectedTabElm |
|
227
|
) |
|
228
|
} |
|
229
|
} |
|
230
|
}, [codeTabSelectorRef, tabRefs, changeTabSelectorCoordinates, selectedTab]) |
|
231
|
|
|
232
|
const actionsProps: CodeBlockActionsProps | undefined = useMemo(() => { |
|
233
|
if (!selectedTab || !selectedTab?.codeProps) { |
|
234
|
return |
|
235
|
} |
|
236
|
|
|
237
|
return { |
|
238
|
source: selectedTab?.codeProps.source, |
|
239
|
blockStyle, |
|
240
|
noReport: selectedTab?.codeProps.noReport, |
|
241
|
noCopy: selectedTab?.codeProps.noCopy, |
|
242
|
inInnerCode: true, |
|
243
|
showGradientBg: false, |
|
244
|
inHeader: true, |
|
245
|
isCollapsed: false, |
|
246
|
} |
|
247
|
}, [selectedTab]) |
|
248
|
|
|
249
|
// Reset tabRefs array before each render |
|
250
|
tabRefs.current = [] |
|
251
|
|
|
252
|
return ( |
|
253
|
<div |
|
254
|
className={clsx( |
|
255
|
"my-docs_1 w-full max-w-full", |
|
256
|
"rounded-docs_lg", |
|
257
|
bgColor, |
|
258
|
boxShadow, |
|
259
|
className |
|
260
|
)} |
|
261
|
data-testid="code-tabs" |
|
262
|
> |
|
263
|
<CodeBlockHeaderWrapper blockStyle={blockStyle} ref={codeTabsWrapperRef}> |
|
264
|
<span |
|
265
|
className={clsx( |
|
266
|
"xs:absolute xs:transition-all xs:duration-200 xs:ease-ease xs:bottom-0", |
|
267
|
blockStyle === "loud" && "bg-medusa-contrast-fg-primary h-px", |
|
268
|
blockStyle === "subtle" && [ |
|
269
|
colorMode === "light" && |
|
270
|
"xs:border-medusa-border-base xs:bg-medusa-bg-base", |
|
271
|
colorMode === "dark" && |
|
272
|
"xs:border-medusa-code-border xs:bg-medusa-code-bg-base", |
|
273
|
] |
|
274
|
)} |
|
275
|
ref={codeTabSelectorRef} |
|
276
|
></span> |
|
277
|
<div className="flex gap-docs_1 items-center"> |
|
278
|
{selectedTab?.codeProps?.badgeLabel && ( |
|
279
|
<Badge |
|
280
|
variant={selectedTab?.codeProps.badgeColor || "code"} |
|
281
|
className="!font-base" |
|
282
|
> |
|
283
|
{selectedTab.codeProps.badgeLabel} |
|
284
|
</Badge> |
|
285
|
)} |
|
286
|
<ul |
|
287
|
className={clsx( |
|
288
|
"!list-none flex gap-docs_0.75 items-center", |
|
289
|
"p-0 mb-0" |
|
290
|
)} |
|
291
|
> |
|
292
|
{Children.map(children, (child, index) => { |
|
293
|
if (!React.isValidElement(child)) { |
|
294
|
return <></> |
|
295
|
} |
|
296
|
|
|
297
|
return ( |
|
298
|
<child.type |
|
299
|
{...(typeof child.props === "object" ? child.props : {})} |
|
300
|
changeSelectedTab={changeSelectedTab} |
|
301
|
pushRef={(tabButton: HTMLButtonElement | null) => |
|
302
|
tabRefs.current.push(tabButton) |
|
303
|
} |
|
304
|
blockStyle={blockStyle} |
|
305
|
isSelected={ |
|
306
|
!selectedTab |
|
307
|
? index === 0 |
|
308
|
: selectedTab.value === (child.props as CodeTab).value |
|
309
|
} |
|
310
|
/> |
|
311
|
) |
|
312
|
})} |
|
313
|
</ul> |
|
314
|
</div> |
|
315
|
{actionsProps && <CodeBlockActions {...actionsProps} />} |
|
316
|
</CodeBlockHeaderWrapper> |
|
317
|
{selectedTab?.codeBlock} |
|
318
|
</div> |
|
319
|
) |
|
320
|
} |