-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathCodeTabs.tsx
More file actions
47 lines (40 loc) · 1.11 KB
/
CodeTabs.tsx
File metadata and controls
47 lines (40 loc) · 1.11 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
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 MDXCodeTabs: FC<MDXCodeTabsProps> = ({
languages: rawLanguages,
displayNames: rawDisplayNames,
children: codes,
defaultTab = '0',
...props
}) => {
const languages = rawLanguages.split('|');
const displayNames = rawDisplayNames?.split('|') ?? [];
const tabs = languages.map((language, index) => {
const displayName = displayNames[index];
return {
key: `${language}-${index}`,
label: displayName?.length ? displayName : language.toUpperCase(),
};
});
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;