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
56import CodeTabs from '#ui/Common/CodeTabs' ;
67import { useHashContext } from '#ui/contexts/HashContext' ;
78
89import 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
1421const 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 - z A - Z 0 - 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-
5289export default CodeTabsWithHash ;
0 commit comments