-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathCodeTabs.tsx
More file actions
61 lines (50 loc) · 1.4 KB
/
CodeTabs.tsx
File metadata and controls
61 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as TabsPrimitive from '@radix-ui/react-tabs';
import type { FC, ReactElement } from 'react';
import CodeTabs from '#ui/Common/CodeTabs';
type MDXCodeTabsProps = {
children: Array<ReactElement<unknown>>;
languages: string;
displayNames?: string;
defaultTab?: string;
};
const NAME_OVERRIDES: Record<string, string | undefined> = {
mjs: 'ESM',
};
const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
languages: rawLanguages,
displayNames: rawDisplayNames,
children: codes,
defaultTab = '0',
...props
}) => {
const languages = rawLanguages.split('|');
const displayNames = rawDisplayNames?.split('|') ?? [];
const occurrences: Record<string, number> = {};
const tabs = languages.map((language, index) => {
const base =
displayNames[index]?.trim() ||
NAME_OVERRIDES[language] ||
language.toUpperCase();
const count = occurrences[base] ?? 0;
occurrences[base] = count + 1;
const label = count === 0 ? base : `${base} (${count})`;
return {
key: `${language}-${index}`,
label: label,
};
});
return (
<CodeTabs
tabs={tabs}
defaultValue={tabs[Number(defaultTab)].key}
{...props}
>
{languages.map((_, index) => (
<TabsPrimitive.Content key={tabs[index].key} value={tabs[index].key}>
{codes[index]}
</TabsPrimitive.Content>
))}
</CodeTabs>
);
};
export default MDXCodeTabs;