Skip to content

Commit de1d09c

Browse files
committed
refactor(ui-components): revert CodeTabs and use derived state in CodeTabsWithHash
1 parent 7bc608a commit de1d09c

2 files changed

Lines changed: 58 additions & 30 deletions

File tree

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
11
'use client';
22

3-
import { useCallback, useState } from 'react';
3+
import * as TabsPrimitive from '@radix-ui/react-tabs';
4+
import { useCallback, useMemo } from 'react';
45

56
import CodeTabs from '#ui/Common/CodeTabs';
67
import { useHashContext } from '#ui/contexts/HashContext';
78

89
import type { ComponentProps, FC } from 'react';
910

10-
type CodeTabsWithHashProps = Omit<ComponentProps<typeof CodeTabs>, 'tabs'> & {
11-
tabs: Array<{ key: string; label: string; id?: string }>;
11+
import MDXCodeTabs from './index';
12+
13+
type CodeTabsWithHashProps = ComponentProps<typeof MDXCodeTabs> & {
14+
groupId: string;
15+
};
16+
17+
const NAME_OVERRIDES: Record<string, string | undefined> = {
18+
mjs: 'ESM',
1219
};
1320

1421
const CodeTabsWithHash: FC<CodeTabsWithHashProps> = ({
15-
tabs,
16-
defaultValue,
22+
languages: rawLanguages,
23+
displayNames: rawDisplayNames,
24+
children: codes,
25+
defaultTab = '0',
26+
groupId,
1727
...props
1828
}) => {
1929
const { hash, setHash } = useHashContext();
20-
const [activeTab, setActiveTab] = useState(defaultValue);
21-
const [prevHash, setPrevHash] = useState(hash);
2230

23-
if (hash !== prevHash) {
24-
setPrevHash(hash);
25-
const matched = tabs.find(t => t.id === hash);
26-
if (matched && matched.key !== activeTab) {
27-
setActiveTab(matched.key);
28-
}
29-
}
31+
const { tabs, languages } = useMemo(() => {
32+
const occurrences: Record<string, number> = {};
33+
const languages = rawLanguages.split('|');
34+
const displayNames = rawDisplayNames?.split('|') ?? [];
35+
36+
const tabs = languages.map((language, index) => {
37+
const base =
38+
displayNames[index]?.trim() ||
39+
NAME_OVERRIDES[language] ||
40+
language.toUpperCase();
41+
42+
const count = occurrences[base] ?? 0;
43+
occurrences[base] = count + 1;
44+
45+
const label = count > 0 ? `${base} (${count + 1})` : base;
46+
47+
return {
48+
key: `${language}-${index}`,
49+
label,
50+
id: `${groupId}-${language}-${index}`.replace(/[^a-zA-Z0-9-_]/g, '-'),
51+
};
52+
});
53+
54+
return { tabs, languages };
55+
}, [rawLanguages, rawDisplayNames, groupId]);
56+
57+
const activeTab =
58+
tabs.find(t => t.id === hash)?.key ?? tabs[Number(defaultTab)].key;
3059

3160
const handleValueChange = useCallback(
3261
(value: string) => {
33-
setActiveTab(value);
3462
const matched = tabs.find(t => t.key === value);
3563
if (matched?.id) {
3664
setHash(matched.id);
@@ -41,12 +69,21 @@ const CodeTabsWithHash: FC<CodeTabsWithHashProps> = ({
4169

4270
return (
4371
<CodeTabs
44-
{...props}
4572
tabs={tabs}
4673
value={activeTab}
4774
onValueChange={handleValueChange}
48-
/>
75+
{...props}
76+
>
77+
{languages.map((_, index) => (
78+
<TabsPrimitive.Content
79+
forceMount
80+
key={tabs[index].key}
81+
value={tabs[index].key}
82+
>
83+
{codes[index]}
84+
</TabsPrimitive.Content>
85+
))}
86+
</CodeTabs>
4987
);
5088
};
51-
5289
export default CodeTabsWithHash;

‎packages/ui-components/src/MDX/CodeTabs/index.tsx‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import CodeTabs from '#ui/Common/CodeTabs';
55

66
import type { FC, ReactElement } from 'react';
77

8-
import CodeTabsWithHash from './CodeTabsWithHash';
9-
108
type MDXCodeTabsProps = {
119
children: Array<ReactElement<unknown>>;
1210
languages: string;
1311
displayNames?: string;
1412
defaultTab?: string;
15-
groupId?: string;
1613
};
1714

1815
const NAME_OVERRIDES: Record<string, string | undefined> = {
@@ -24,7 +21,6 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
2421
displayNames: rawDisplayNames,
2522
children: codes,
2623
defaultTab = '0',
27-
groupId,
2824
...props
2925
}) => {
3026
const { tabs, languages } = useMemo(() => {
@@ -48,19 +44,14 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
4844
return {
4945
key: `${language}-${index}`,
5046
label,
51-
id:
52-
groupId &&
53-
`${groupId}-${language}-${index}`.replace(/[^a-zA-Z0-9-_]/g, '-'),
5447
};
5548
});
5649

5750
return { tabs, languages };
58-
}, [rawLanguages, rawDisplayNames, groupId]);
59-
60-
const Component = groupId ? CodeTabsWithHash : CodeTabs;
51+
}, [rawLanguages, rawDisplayNames]);
6152

6253
return (
63-
<Component
54+
<CodeTabs
6455
tabs={tabs}
6556
defaultValue={tabs[Number(defaultTab)].key}
6657
{...props}
@@ -74,7 +65,7 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
7465
{codes[index]}
7566
</TabsPrimitive.Content>
7667
))}
77-
</Component>
68+
</CodeTabs>
7869
);
7970
};
8071

0 commit comments

Comments
 (0)