Skip to content

Commit 07251e6

Browse files
committed
refactor(ui): isolate window hash logic from CodeTabs
1 parent 97b39de commit 07251e6

2 files changed

Lines changed: 60 additions & 40 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use client';
2+
3+
import { useCallback, useState } from 'react';
4+
5+
import CodeTabs from '#ui/Common/CodeTabs';
6+
import { useHashContext } from '#ui/contexts/HashContext';
7+
8+
import type { ComponentProps, FC } from 'react';
9+
10+
type CodeTabsWithHashProps = Omit<ComponentProps<typeof CodeTabs>, 'tabs'> & {
11+
tabs: Array<{ key: string; label: string; anchorId: string }>;
12+
};
13+
14+
const CodeTabsWithHash: FC<CodeTabsWithHashProps> = ({
15+
tabs,
16+
defaultValue,
17+
...props
18+
}) => {
19+
const { hash, setHash } = useHashContext();
20+
const [activeTab, setActiveTab] = useState(defaultValue);
21+
const [prevHash, setPrevHash] = useState(hash);
22+
23+
if (hash !== prevHash) {
24+
setPrevHash(hash);
25+
const matched = tabs.find(t => t.anchorId === hash);
26+
if (matched && matched.key !== activeTab) {
27+
setActiveTab(matched.key);
28+
}
29+
}
30+
31+
const handleValueChange = useCallback(
32+
(value: string) => {
33+
setActiveTab(value);
34+
const matched = tabs.find(t => t.key === value);
35+
if (matched) {
36+
setHash(matched.anchorId);
37+
}
38+
},
39+
[tabs, setHash]
40+
);
41+
42+
return (
43+
<CodeTabs
44+
{...props}
45+
tabs={tabs}
46+
value={activeTab}
47+
onValueChange={handleValueChange}
48+
/>
49+
);
50+
};
51+
52+
export default CodeTabsWithHash;

packages/ui-components/src/MDX/CodeTabs.tsx renamed to packages/ui-components/src/MDX/CodeTabs/index.tsx

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'use client';
2-
31
import * as TabsPrimitive from '@radix-ui/react-tabs';
4-
import { useEffect, useId, useMemo, useState } from 'react';
2+
import { useMemo } from 'react';
53

64
import CodeTabs from '#ui/Common/CodeTabs';
75

86
import type { FC, ReactElement } from 'react';
97

8+
import CodeTabsWithHash from './CodeTabsWithHash';
9+
1010
type MDXCodeTabsProps = {
1111
children: Array<ReactElement<unknown>>;
1212
languages: string;
@@ -27,8 +27,7 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
2727
groupId,
2828
...props
2929
}) => {
30-
const id = useId();
31-
const prefix = groupId ? `tab-${groupId}` : `tab-${id}`;
30+
const prefix = groupId ?? 'tab';
3231

3332
const { tabs, languages } = useMemo(() => {
3433
const occurrences: Record<string, number> = {};
@@ -61,43 +60,12 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
6160
return { tabs, languages };
6261
}, [rawLanguages, rawDisplayNames, prefix]);
6362

64-
const [activeTab, setActiveTab] = useState(() => {
65-
if (typeof window !== 'undefined' && window.location.hash) {
66-
const hash = window.location.hash.slice(1);
67-
const matched = tabs.find(t => t.anchorId === hash);
68-
if (matched) {
69-
return matched.key;
70-
}
71-
}
72-
return tabs[Number(defaultTab)]?.key ?? tabs[0].key;
73-
});
74-
75-
useEffect(() => {
76-
const handleHashChange = () => {
77-
const hash = window.location.hash.slice(1);
78-
const matched = tabs.find(t => t.anchorId === hash);
79-
if (matched) {
80-
setActiveTab(matched.key);
81-
}
82-
};
83-
84-
window.addEventListener('hashchange', handleHashChange);
85-
return () => window.removeEventListener('hashchange', handleHashChange);
86-
}, [tabs]);
87-
88-
const handleValueChange = (value: string) => {
89-
setActiveTab(value);
90-
const matched = tabs.find(t => t.key === value);
91-
if (matched) {
92-
window.history.replaceState(null, '', `#${matched.anchorId}`);
93-
}
94-
};
63+
const Component = groupId ? CodeTabsWithHash : CodeTabs;
9564

9665
return (
97-
<CodeTabs
66+
<Component
9867
tabs={tabs}
99-
value={activeTab}
100-
onValueChange={handleValueChange}
68+
defaultValue={tabs[Number(defaultTab)].key}
10169
{...props}
10270
>
10371
{languages.map((_, index) => (
@@ -109,7 +77,7 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
10977
{codes[index]}
11078
</TabsPrimitive.Content>
11179
))}
112-
</CodeTabs>
80+
</Component>
11381
);
11482
};
11583

0 commit comments

Comments
 (0)